How to setup an NFS server on CentOS 7

NFS (Network File System) is a way to share a filesystem between Unix systems. So you can have one server with a large amount of disk and by using NFS on that server other client machines can use that server to access and store files etc.  To the client machine it appears as though the disk space being used is local to that client even though it is actually on the NFS server.

In this tutorial we will use two machines

NFS server –  dc-nfs-server  –  172.30.12.2
NFS client  –   dc-nfs-client  –  172.16.1.193

 

Step 1

Centos 7 does not come with NFS installed by default so we need to install the required NFS package using the yum utility

yum install nfs-utils

this will need to be installed on both the server and the client machines.

 

Step 2

Once the package has been installed on the NFS server machine you then need to enable and start the relevant NFS services using the systemctl commands.

First enable the services with the following commands.

systemctl enable rpcbind
systemctl enable nfs-server
systemctl enable nfs-lock
systemctl enable nfs-idmap

and then start those services

systemctl start rpcbind
systemctl start nfs-server
systemctl start nfs-lock
systemctl start nfs-idmap

 

Step 3

We now have NFS running on the server but we have not yet shared out any filesystem which the client can use. So we are going to create a directory that the client machine can use as file storage.

On the NFS server

mkdir -p /export/data<em>   </em>

and then modify the owner and permissions of that directory

chown nfsnobody:nfsnobody /export/data
chmod -R 755 /export/data

 

Step 4

The final step on the NFS server is to share the “/export/data” directory across the network. Using whichever editor you are comfortable with, you need to add this line to the “/etc/exports” file

/export/data 172.16.1.193(rw,sync,no_root_squash,no_all_squash)

Then run this command to read the exports file and export the directories in it

exportfs -r

N.B – You could simply restart the NFS server which would have the same effect – systemctl restart nfs-server)

In the above line that we have used the IP address of the NFS client (172.16.1.193).  This means only that client will be able to use this NFS share. You can of course use multiple IPs or you can simply have this line

/export/data *(rw,sync,no_root_squash,no_all_squash)

where the “*” symbol means allow any IP address to use this file share but there are obvious security risks with doing this.

To see how this works on the NFS client run this command on the client to see what file systems have been shared by the NFS server

[root@dc-nfs-client mnt]# showmount -e 172.30.12.2
Export list for 172.30.12.2:
/export/data 172.16.1.193

You can see from the above output the “/export/data” shared directory we created on the NFS server.

We can then mount that filesystem on the client. To do this we need somewhere to mount the filesystem and in our case it will be “/mnt/tmp” so the command is

mount -t nfs 172.30.12.2:/export/data /mnt/tmp

On the client if you run a “df -kh” you should see the mounted filesystem

[root@dcp-nfs-client mnt]# df -kh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_jontest-lv_root
29G 12G 16G 42% /
tmpfs 499M 0 499M 0% /dev/shm
/dev/vda1 477M 30M 422M 7% /boot
172.30.12.2:/export/secondary
663G 6.2G 623G 1% /mnt/tmp

 

you can now use “/mnt/tmp” as you would any other filesystem on your local machine but all files are stored on the NFS server.

In the above example we manually mounted the filesystem on the client but this can be done automatically on boot up as long as the NFS server is up and available.

Finally all of the above was done without any firewall running on the client or the server. If you have a firewall or iptables running on your server then you would need to allow the relevant ports through but that is beyond the scope of this document.