How to setup an NFS server on CentOS 7
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
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.
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
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
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
Then run this command to read the exports file and export the directories in it
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
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:
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
29G 12G 16G 42% /
tmpfs 499M 0 499M 0% /dev/shm
/dev/vda1 477M 30M 422M 7% /boot
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.