Setup NFS Server and NFS Client on CentOS 7
In this tutorial we will learn how to set up NSF Server and NFS Client on CentOS 7 operating system, setting up or managing a NFS server or a NFS client is not quite a complex task at all, we’ll do our best to explain this entire process in the most simple possible way. We can use the set up from this tutorial for various small network sharing applications like setting up a backup server or maybe a simple file server etc., either way NFS package is always a good candidate to start with. Network File System (NFS) has been developed by Sun Microsystems back in 1984 and since we can say that is still one of the most trusted and used software for sharing files and folders over the network for small applications, as we said previously is quite easy to set up and it takes only a few minutes to install.
Table of Contents
NFS Environment
EPEL repository installation
NFS package installation
CentOS system configuration
Setting up NFS Server on CentOS
Setting up NFS Clients on CentOS
NFS Environment
In this small step by step guide we’ll presume that we have to install and configure NFS server to a server called here nfsserver-01
and NFS client on four clients called web-01
, web-02
, mysql-01
and mysql-02
. The naming convention used on this NFS tutorial isn’t quite elaborate, we’ll talk about this topic another time on a completely different tutorial but now for simplicity we’ll use simple and easy to remember host names.
Let’s have a quick look to the network level configuration for our small NFS environment, we said small because in this particular example we’ll have only five servers, you may expand this according to your stack.
Host name | Network | IP Address | NFS Role |
---|---|---|---|
nfsserver-01 | 10.1.1.0/24 | 10.1.1.1 | NFS Server |
web-01 | 10.2.2.0/24 | 10.2.2.1 | NFS Client |
web-02 | 10.2.2.0/24 | 10.2.2.2 | NFS Client |
mysql-01 | 10.3.3.0/24 | 10.3.3.1 | NFS Client |
mysql-02 | 10.3.3.0/24 | 10.3.3.2 | NFS Client |
EPEL repository installation
We will begin our short NFS server and client tutorial by installing EPEL (Extra Packages for Enterprise Linux) repository, this reputable repository maintained by Fedora Project will facilitate our package installation in the next step where we’ll have to perform NFS package installation but now let’s kick off epel-release
repo installation across all five servers from our environment like shown in the example below:
$ yum install -y epel-release
NFS package installation
Having now epel-release
repo installed across of all our servers we can proceed further with nfs-utils
package installation. Please note that there is no individual package for NFS server or for the client, both packages are wrapped into nfs-utils
package which will install NFS and all its dependencies needed in terms of software, let’s start the installation with the next command once again on all five servers:
$ yum install -y nfs-utils
CentOS system configuration
On this step we will make sure that our CentOS 7 operating system is properly configured for our NFS server and clients setup by having all necessary folders in place. Before moving forward with this step please make sure that the firewall allows the communication between NFS server and NFS clients and also ensure that your selinux
configuration allows this setup to work properly, you can change its default value enabled
with permissive
option.
Please avoid using
/var/
system folder as sharing endpoint for NFS server because this specific folder requires some special permissions, our recommendation is to use/mnt/
location on both, NFS server and clients this location being designed for attaching additional filesystems like CD-ROM, hard disk drives, USB drives, NFS shares etc..
Let’s start this step by creating two folders on NFS server first, one folder for each existing tier, web
and respectively mysql
in our tutorial as we don’t wan’t to mix those two tiers and on top of that we would like to present these network shares as individual shares.
$ mkdir /mnt/{web,mysql}-share
Ideally, if we have a very intensive NFS workload we could attach and mount distinct disks for each share that we are presenting over the network but in this guide we will resume to a single disk (OS Disk in this case) purely for demonstration purpose. In a production environment we would use individual disks for each share where possible.
Now let’s move to our NFS clients, on each pair of servers we’ll have to create specific folders easily identifiable, we’ll start with both web servers so please create the folder on both web servers, web-01
and web-02
:
$ mkdir /mnt/web-share
And now using the same mkdir
command but this time on both mysql servers, mysql-01
and respectively mysql-02
:
$ mkdir /mnt/mysql-share
Congratulations, we’re done with CentOS configuration and we can now focus on the next step where we’ll have to set up NFS server, nothing to worry about, setting up a NFS server is quite easy and straight forward.
Setting up NFS Server on CentOS
Having nfs-utils
package already installed we can proceed by setting up NFS server, first we need to start by editing exports
file were we’ll define the access for NFS clients and their permissions so let’s start editing.
$ vi /etc/exports
Knowing the network range for each tier that we’ve previously defined we can safely add the next two lines to exports
:
/mnt/web-share 10.2.2.0/24(rw,sync,no_root_squash,no_all_squash)
/mnt/mysql-share 10.3.3.0/24(rw,sync,no_root_squash,no_all_squash)
That’s all that we need to specify on exports
file, we can now safely save our changes and close this file as we’ll move to the next step with our NFS server set up guide.
We’ll have to enable nfs-server
service to start automatically each time the server gets restarted / rebooted for various reasons (ie. kernel updates, patching etc.) and also start the service like shown in the example below:
$ systemctl enable nfs-server
$ systemctl start nfs-server
At this stage we can say that we have a fully configured and functional NFS server, let’s have a look if our network shares are presented across the network by executing exportfs
command that comes with nfs-utils
package:
$ exportfs
A successful output for the above command should look similar to this one listed below where we can see that our NFS server has loaded exports
file configuration, is aware of our shares and is presenting these across the network:
/mnt/web-share
10.2.2.0/24
/mnt/mysql-share
10.3.3.0/24
If you are not able to see a similar output like the one above after executing exportfs
command then try to add -a
argument after the command, with this argument in place the configuration file should be reloaded if no typos or other errors are encountered. So please execute exportfs -a
for reloading the configuration file and once again exportfs
with no arguments to check if the configuration has been picked up.
Setting up NFS Clients on CentOS
Setting up NFS clients is quite easy, all we need to do is just to amend fstab
file on each of NFS clients, we can mount these shares directly but we want to make sure that each time a server gets restarted we’ll get back our shares automatically, let’s prepare this file for editing on each servers, web
and mysql
ones:
$ vi /etc/fstab
On web tier (web-01
and web-02
) servers we’ll have to add the next line into fstab
file making reference to our NFS web share folder remotely and locally as well:
10.1.1.1:/mnt/web-share /mnt/web-share nfs defaults,hard 0 2
On mysql tier (mysql-01
and mysql-02
) servers we’ll do pretty much the same as we did for web tier but this time we’ll be changing the share to endpoint to reflect mysql one:
10.1.1.1:/mnt/mysql-share /mnt/mysql-share nfs defaults,hard 0 2
We can now safely save and close fstab
file, we’re done with it and now we will try to mount those NFS shares across all four NFS clients by using mount
command.
$ mount -a
We can check if mount
command was successful by invoking df
command to see our systems mounts like shown in the example below:
$ df -h
A successful console output for example on one of the web server will look similar to this one listed below:
...
10.1.1.1:/mnt/web-share 10G 32M 10G 1% /mnt/web-share
...
Perform the same check across all servers, once completed please take a short break and relax, we’re done, we’ve managed to successfully set up a NFS server and four NFS clients.