This article covers a version of CentOS that is no longer supported. If you are currently operating a server running CentOS 6, we highly recommend upgrading or migrating to a supported version of CentOS.
Reason: CentOS 6 reached end of life (EOL) on November 30th, 2020 and no longer receives security patches or updates. For this reason, this guide is no longer maintained.
This guide might still be useful as a reference, but may not work on other CentOS releases. If available, we strongly recommend using a guide written for the version of CentOS you are using.
NFS mounts work to share a directory between several servers. This has the advantage of saving disk space, as the home directory is only kept on one server, and others can connect to it over the network. When setting up mounts, NFS is most effective for permanent fixtures that should always be accessible.
An NFS mount is set up between at least two servers. The machine hosting the shared network is called the server, while the ones that connect to it are called ‘clients’.
This tutorial requires 2 servers: one acting as the server and one as the client. We will set up the server machine first, followed by the client. The following IP addresses will refer to each one:
The system should be set up as root. You can access the root user by typing
yum install nfs-utils nfs-utils-libSubsequently, run several startup scripts for the NFS server:
chkconfig nfs on service rpcbind start service nfs start
The next step is to decide which directory we want to share with the client server. The chosen directory should then be added to the /etc/exports file, which specifies both the directory to be shared and the details of how it is shared.
Suppose we wanted to share the directory, /home.
We need to export the directory:
Add the following lines to the bottom of the file, sharing the directory with the client:
These settings accomplish several tasks:
Once you have entered in the settings for each directory, run the following command to export them:
Start off by using apt-get to install the nfs programs.
yum install nfs-utils nfs-utils-lib
Once the programs have been downloaded to the the client server, create the directory that will contain the NFS shared files
mkdir -p /mnt/nfs/home
Then go ahead and mount it
mount 220.127.116.119:/home /mnt/nfs/home
You can use the df -h command to check that the directory has been mounted. You will see it last on the list.
Filesystem Size Used Avail Use% Mounted on /dev/sda 20G 783M 18G 5% / 18.104.22.1689:/home 20G 785M 18G 5% /mnt/nfs/home
Additionally, use the mount command to see the entire list of mounted file systems.
Your list should look something like this:
/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) 22.214.171.1249:/home on /mnt/nfs/home type nfs (rw,noatime,nolock,bg,nfsvers=2,intr,tcp,actimeo=1800,addr=126.96.36.1999)
Once you have successfully mounted your NFS directory, you can test that it works by creating a file on the Client and checking its availability on the Server.
Create a file in the directory to try it out:
You should then be able to find the files on the Server in the /home.
You can ensure that the mount is always active by adding the directory to the fstab file on the client. This will ensure that the mount starts up after the server reboots.
188.8.131.529:/home /mnt/nfs/home nfs auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0
You can learn more about the fstab options by typing in:
After any subsequent server reboots, you can use a single command to mount directories specified in the fstab file:
You can check the mounted directories with the two earlier commands:
Should you decide to remove a directory, you can unmount it using the umount command:
cd sudo umount /directory name
You can see that the mounts were removed by then looking at the filesystem again.
You should find your selected mounted directory gone.
If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.