NFS stands for Network File System and they are typically used to share directories between several servers. It helps in optimizing the disk space usage by keeping home directory on one server and others connecting it through the network. The machine which hosts the shared network is known as the server and the machines that connect to it are called clients.
This tutorial will guide you in setting up NFS mounts. First we will set up the server machine and then the client. For example, let’s assume the IP addresses of server and client are 18.104.22.1689 and 22.214.171.1245. Also, both the machines should be set up as root. You can access the root user using the sudo su command.
NFS Server Set Up
First of all, we will be downloading the required software. You can start by installing nfs programs using the apt-get command as shown below:
yum install nfs-utils nfs-utils-lib
After that, run the scripts for NFS server using the commands:
chkconfig nfs on service rpcbind start service nfs start
Once it is done, we can proceed with the export of shared directory.
For that, first decide which directory you would like to share with client. Then, that directory should be added to /etc/exports file which has the specification of directory as well as how it is to be shared. Suppose, you want to share /home directory. Export the directory and add the following lines of code to the end of the file.
Here is an explanation of the terms used in the above command:
- rw option allows the client machines to both read and write within the shared directory.
- Sync will confirm requests to shared directory once changes are committed.
- No_subtree_check option is for preventing the subtree checking. NFS has a feature where it scans each and every directory above it whenever a shared directory happens to be a subdirectory of a larger file system. The no_subtree_check option increases the reliability but reduces security.
- no_root_squash option allows the root to make connection with the designated directory.
Now, you can export the directory using:
NFS Client Set Up
To start with, install the nfs programs using apt-get command:
yum install nfs-utils nfs-utils-lib
After that, create a directory for NFS shared files as shown below:
mkdir -p /mnt/nfs/home
mount 126.96.36.1999:/home /mnt/nfs/home
Verify whether the directory has been mounted using the df –h command. You should see it on the list displayed:
Filesystem Size Used Avail Use% Mounted on /dev/sda 20G 783M 18G 5% / 188.8.131.529:/home 20G 785M 18G 5% /mnt/nfs/home
You can also use the mount command to view all the mounted file systems:
/dev/sda on / type ext4 (rw,errors=remount-ro) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) nfsd on /proc/fs/nfsd type nfsd (rw) 184.108.40.2069:/home on /mnt/nfs/home type nfs (rw,noatime,nolock,bg,nfsvers=2,intr,tcp,actimeo=1800,addr=220.127.116.119)
Verify the NFS Mount
Once you have mounted the NFS directory, you can verify it by creating a file on the client and checking it from the server.
Create a sample file:
If you check it on the server /home directory, you should be able to find that file:
In order to ensure that the mount is active always, you can add the directory to the fstab file on your client. Then, whenever server reboots, mount will start up.
18.104.22.1689:/home /mnt/nfs/home nfs auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0
The following command comes handy for mounting directories specified in fstab file:
Removing NFS Mount
In case if you wish to remove a directory, you can do that using the umount command:
cd sudo umount /directory name
Verify the filesystem using df –h command to see if thee mounts were removed.