How to Configure NFS(Network File System) in Linux
To Setup NFS (Network File System)
Network File System is the most commonly used network services. It is based on the Remote procedure call. It allows the client to mount automatically on remote file systems. It provides transparent access to it as if the file system is local. Setup NFS (Network File System) on RHEL/CentOS/Fedora and Debian/Ubuntu is explained in this article.
Uses of NFS
- To share out data on a central server
- Can be secured with Kerberos and Firewalls.
- Newer version of NFS supports pseudo, acl root mounts.
- No manual refresh needed for new files.
- USB storage devices can be used by other machines on the network.
- Users get their data irrespective of physical location.
- With the help of NFS we can configure centralized storage solutions.
- With NFS it is not necessary that both machines run on the same OS.
- It uses standard client/server architecture for file sharing between all *nix based machines.
- NFS permits local access to remote files.
Services of NFS
Its a V-launched service of the System. The NFS server package has three facilities, which are included in the nfs-utils and portmap packages.
- nfs translates remote file sharing requests into the local file system requests.
- Portmap maps calls from other machines to the correct RPC service (not required with NFSv4).
- rpc.mountd service is responsible for mounting and unmounting of file systems.
Files Necessary for the Configuration of NFS
/etc/exports : Its a major NFS, all exported files and directories configuration files are defined in this file at the NFS Server end.
/etc/sysconfig/nfs : NFS Configuration file to control on which port rpc and other services are listening.
/etc/fstab : For mounting an NFS directory on your computer over the reboots, we have to make an entry in /etc/fstab.
Setting up and Configuration of NFS Mounts on Linux Server
For setting up NFS mounts, we require at least two Unix/Linux machines.
NFS Client IP : 192.168.5.148
NFS Server IP : 192.168.5.147
To Install NFS Server
Install NFS Server by executing the following command in NFS server machine.
root@linuxhelp1:~# sudo apt install nfs-kernel-server
To Install NFS Client
Install NFS Client by executing the following command in NFS client machine.
root@linuxhelp:~# sudo apt-get install nfs-common
To Set Up the NFS Server
To Configure Export directory
To make our directory shareable in the network, make an entry in “ /etc/exports” and restart the services .
root@linuxhelp1:~# vim /etc/exports # /etc/exports: the access control list for filesystems which may be exported . . . # /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check) # /home 192.168.5.148(rw,sync,no_root_squash)
Now, if you specify any particular IP then it will share the folder only with the specified client. If you mention " *" then it is an open access to everyone.
Various options that can be used in “ /etc/exports” file for file sharing are as follows.
no_root_squash is a phrase, which permits root to connect to the specified directory.
no_subtree_check is an option that prevents the subtree checking.
ro - provides read only permission. Client will be able to read only the shared files.
rw - Provides the client server to both read and write permission in the shared directory.
sync, which confirms requests to the shared directory after the changes have been committed.
Now restart the service.
root@linuxhelp1:~# sudo systemctl restart nfs-kernel-server.service
To Set Up the NFS Client
After the configuration of the NFS server, mount the partition or shared directory in the client server. Now we have to mount the directory in our server at the NFS client end, to access it locally. For that, find out that shares on the NFS Server or the remote server.
root@linuxhelp:~# showmount -e 192.168.5.147 Export list for 192.168.5.147: /home 192.168.5.148
Before that I am going to show you the partitions that are mounted in the client system
root@linuxhelp:~# df -h Filesystem Size Used Avail Use% Mounted on udev 476M 0 476M 0% /dev tmpfs 99M 5.5M 93M 6% /run /dev/sda3 11G 4.1G 6.4G 39% / tmpfs 493M 156K 492M 1% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 493M 0 493M 0% /sys/fs/cgroup /dev/sda2 269M 53M 198M 22% /boot cgmfs 100K 0 100K 0% /run/cgmanager/fs tmpfs 99M 48K 99M 1% /run/user/1000
Mount Shared NFS Directory
Use the following command, to mount the shared NFS directory temporarily.
root@linuxhelp:~# mount -t nfs 192.168.5.147:/home /mnt
Now verify it by using the following command.
root@linuxhelp:~# mount | grep nfs 192.168.5.147:/home on /mnt type nfs4 (rw,relatime,vers=4.0,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.5.148,local_lock=none,addr=192.168.5.147)
For mounting permanently an NFS directory on your computer over the reboots, make an entry in “ /etc/fstab“ .
After that I am going to show you the partitions that are mounted in the client system.
root@linuxhelp:~# df -h Filesystem Size Used Avail Use% Mounted on udev 476M 0 476M 0% /dev tmpfs 99M 5.5M 93M 6% /run /dev/sda3 11G 4.1G 6.4G 39% / tmpfs 493M 156K 492M 1% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 493M 0 493M 0% /sys/fs/cgroup /dev/sda2 269M 53M 198M 22% /boot cgmfs 100K 0 100K 0% /run/cgmanager/fs tmpfs 99M 48K 99M 1% /run/user/1000 192.168.5.147:/home 11G 4.1G 6.4G 40% /mnt
root@linuxhelp:~# vim /etc/fstab Insert the below new line : # /etc/fstab: static file system information. . . . /dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0 192.168.5.147:/home /mnt nfs defaults 0 0
To Test the Working of NFS Setup
By creating a test file on the server side, check whether it is available in nfs client side.
In nfsserver side
In the shared directory, Create a new text file called “ file1”
root@linuxhelp1:/home# touch file1
In nfsclient side
Since client have read write permission so edit the file in client side
root@linuxhelp:/mnt# ls testmail user1 root@linuxhelp:/mnt# ls file1 testmail user1 root@linuxhelp:/mnt# vim file1 hello
In Server Side
You can be able to view the modified file from the client side in the server.
root@linuxhelp1:/home# ls file1 testmail user1 root@linuxhelp1:/home# cat file1 hello
To Remove the NFS Mount
To unmount the shared directory from your server after the completion of file sharing, unmount that specific directory using the following command.
root@linuxhelp:~# umount /mnt
Look at the filesystem once again to verify.
root@linuxhelp:~# df -h Filesystem Size Used Avail Use% Mounted on udev 476M 0 476M 0% /dev tmpfs 99M 5.5M 93M 6% /run /dev/sda3 11G 4.1G 6.4G 39% / tmpfs 493M 156K 492M 1% /dev/shm tmpfs 5.0M 8.0K 5.0M 1% /run/lock tmpfs 493M 0 493M 0% /sys/fs/cgroup /dev/sda2 269M 53M 198M 22% /boot cgmfs 100K 0 100K 0% /run/cgmanager/fs tmpfs 99M 48K 99M 1% /run/user/1000
Important commands for NFS:
exportfs -r - Refresh the server’ s list after modifying /etc/exports
exportfs -u - Unexports all shares listed in /etc/exports, or given name
exportfs -a - Exports all shares listed in /etc/exports, or given name
exportfs -v - Displays a list of shares files and options on a server
showmount -d - Lists all the sub directories
showmount -e : Lists the available shares at the remote server
showmount -e - Shows the available shares on your local machine