NFS Sharing Between CentOS Servers

NFS Sharing Between CentOS Servers

In this blog, I’m going to describe how we can share folders between CentOS servers using Network File System (NFS). For this purpose, I’m setting up two CentOS 6.5 Minimal servers named as CentOS-1 and CentOS-2. The IP addresses of CentOS-1 and CentOS-2 are 172.16.5.5 and 172.16.5.6 respectively. I’m going to create a folder in CentOS-1 machine and then share it with CentOS-2 machine.

NFS Share Topology
NFS Share Topology

Before getting into folder sharing process, I suggest you to go through my previous blog articles on CentOS so that we are on same page here.

Preparing NFS Server

Once I had prepared my both CentOS servers, I could get into the folder sharing procedure. First, I had to make sure if NFS utilities were available in my CentOS system or not. So, I ran a query statement with RPM to check the availability of nfs-utils.

[root@CentOS-1 ~]# rpm -q nfs-utils

Running above command gave me an empty result, which meant nfs-utils wasn’t already installed in my CentOS machine. This was expected because I’ve been using a minimal server and minimal versions don’t come up with more than basic packages. So, I installed nfs-utils in both of my CentOS machines.

[root@CentOS-1 ~]# yum install nfs-utils
[root@CentOS-2 ~]# yum install nfs-utils

After installing nfs-utils in both of my servers, I had to run it, in order to create NFS sharing. First I checked if it was already running or not. And if it wasn’t running already, I manually started it. I also ensured that it’ll run every time my servers restart by the use of chkconfig command.

[root@CentOS-1 ~]# service nfs status
[root@CentOS-1 ~]# service nfs start
[root@CentOS-1 ~]# chkconfig nfs on

Now that I had my NFS server running, I needed to create or specify a folder which I wanted to share with other machines. So, I first created a folder called Test in /opt directory. Then I added an export entry for this folder in /etc/exports file. This is the configuration file which is referred by NFS for sharing.

[root@CentOS-1 ~]# mkdir /opt/Test
[root@CentOS-1 ~]# vi /etc/exports
/opt/Test    172.16.5.6(rw)

The above export entry tells the system to share /opt/Test folder with the host 172.16.5.6 and allow Read/Write privilege to it. If I wanted to share this folder to all hosts, I would’ve put instead of 172.16.5.6. And if I were to share it with multiple specific IP addresses, I would’ve written my above entry like /opt/Test 172.16.5.6(rw) 192.168.1.5(ro).

After I had defined my folder sharing entries in exports file, all I needed was to export the shared folder.

[root@CentOS-1 ~]# /usr/sbin/exportfs -a

I ran the following command to make sure my share was working properly.

[root@CentOS-1 ~]# /usr/sbin/exportfs

It displayed the shared folder and IP address of sharing host, which meant my sharing was working properly. In case the above command returns empty result or incomplete results, it means the folder isn’t being shared. In that case, we need to check our exports file and make sure nfs is installed and running.

Mounting Shared Folder

Now at the client/receiver side, I created a folder  called Test and mounted the shared folder into it. You can name your folder whatever you wish. Note that it doesn’t have to be same as the folder shared in server side.

[root@CentOS-2 ~]# mkdir /root/Test
[root@CentOS-2 ~]# mount 172.16.5.5:/opt/Test /root/Test

After executing above mount command, I waited for some time to let it complete, but the mount didn’t go as I expected. It just hanged for some time and timed out. At this point, I’m sure you’ve caught my mistake here. Well, you got me! 🙂 I just forgot about my Iptables firewall at my CentOS-1 server, which wasn’t configured to allow NFS requests to it.

Setting Up NFS Ports and IPtables

I said I had to allow NFS connections in my iptables, but I never mentioned how to know about connection ports and types used by NFS. The good thing is we can specify these ports for NFS inside its configuration file itself. To make it simple, you can simply append port entries to this config file like this:

[root@CentOS-1 ~]# {
echo “LOCKD_TCPPORT=32803”
echo “LOCKD_UDPPORT=32769”
echo “MOUNTD_PORT=892”
echo “RQUOTAD_PORT=875”
echo “STATD_PORT=662”
echo “STATD_OUTGOING_PORT=2020”
} >> /etc/sysconfig/nfs

After defining ports for NFS, I restarted NFS process to implement the change.

[root@CentOS-1 ~]# service nfs restart
[root@CentOS-1 ~]# service rpcsvcgssd restart

Then, I added firewall statements in iptables config file to allow above ports in NFS server. To do this, I opened up /etc/sysconfig/iptables file in vi and added following statements inside it above REJECT statements.

[root@CentOS-1 ~]# vi /etc/sysconfig/iptables
# NFS Sharing
-A INPUT -s 172.16.5.0/24 -m state –state NEW -p udp –dport 111 -j ACCEPT
-A INPUT -s 172.16.5.0/24 -m state –state NEW -p tcp –dport 111 -j ACCEPT
-A INPUT -s 172.16.5.0/24 -m state –state NEW -p tcp –dport 2049 -j ACCEPT
-A INPUT -s 172.16.5.0/24 -m state –state NEW -p tcp –dport 32803 -j ACCEPT
-A INPUT -s 172.16.5.0/24 -m state –state NEW -p udp –dport 32769 -j ACCEPT
-A INPUT -s 172.16.5.0/24 -m state –state NEW -p tcp –dport 892 -j ACCEPT
-A INPUT -s 172.16.5.0/24 -m state –state NEW -p udp –dport 892 -j ACCEPT
-A INPUT -s 172.16.5.0/24 -m state –state NEW -p tcp –dport 875 -j ACCEPT
-A INPUT -s 172.16.5.0/24 -m state –state NEW -p udp –dport 875 -j ACCEPT
-A INPUT -s 172.16.5.0/24 -m state –state NEW -p tcp –dport 662 -j ACCEPT
-A INPUT -s 172.16.5.0/24 -m state –state NEW -p udp –dport 662 -j ACCEPT

Finally, I restarted the iptables service to apply new firewall rules.

[root@CentOS-1 ~]# service iptables restart

Setting Up Permanent Folder Sharing

After setting up NFS and Iptables at my NFS server, I returned to my second machine and once again tried the mount command. This time, it didn’t take any unnecessary time and the shared folder was immediately mounted. I checked the availability of my sharing by entering this command:

[root@CentOS-2 ~]# mount 172.16.5.5:/opt/Test /root/Test
[root@CentOS-2 ~]# df -h

And as expected, it displayed the shared folder being mounted to the local folder in second server. Then, I wanted to keep this share mounted permanently, so I added my mount statement in fstab file.

[root@CentOS-2 ~]# echo “172.16.5.5:/opt/Test /root/Test nfs” >> /etc/fstab

Doing this allowed my CentOS-2 server to always have access to a shared folder in CentOS-1 server, even after servers restart. In case you would want to unmount this folder, you can use following command:

[root@CentOS-2 ~]# umount /root/Test

This is a quick guide on how to create NFS share between Linux servers. I hope you’ve found it informative and helpful. Please let me know in the comments section if you’ve any query or suggestion. Thank you for reading!

Comments are closed.