NFS, stands for Network File System, is a server-client protocol used for sharing files between linux/unix to unix/linux systems. NFS enables you to mount a remote share locally. You can then directly access any of the files on that remote share.
Scenario
In this how-to, I will be using two systems which are running with CentOS 7. The same steps are applicable for RHEL and Scientific Linux 7 distributions.
Here are mt testing nodes details.
NFS Server Hostname: server.unixmen.local NFS Server IP Address: 192.168.1.101/24
NFS Client Hostname: client.unixmen.local NFS Client IP Address: 192.168.1.102/24
Server Side Configuration
Install NFS packages in your Server system by using the following command:
yum install nfs-utils nfs-utils-lib
Enable and start NFS services:
systemctl enable rpcbind systemctl enable nfs-server systemctl enable nfs-lock systemctl enable nfs-idmap
systemctl start rpcbind systemctl start nfs-server systemctl start nfs-lock systemctl start nfs-idmap
Now, let us create some shared directories in server.
Create a shared directory named ‘/var/unixmen_share’ in server and let the client users to read and write files in that directory.
mkdir /var/unixmen_share chmod 777 /var/unixmen_share/
Export shared directory on NFS Server:
Edit file /etc/exports,
vi /etc/exports
Add the following line:
/var/unixmen_share/ 192.168.1.0/24(rw,sync,no_root_squash,no_all_squash)
where,
/var/unixmen_share – shared directory 192.168.1.0/24 – IP address range of clients rw – Writable permission to shared folder sync – Synchronize shared directory no_root_squash – Enable root privilege no_all_squash - Enable user’s authority
Restart the NFS service:
systemctl restart nfs-server
Client Side Configuration
Install NFS packages in your client system by using the following command:
yum install nfs-utils nfs-utils-lib
Enable and start NFS services:
systemctl enable rpcbind systemctl enable nfs-server systemctl enable nfs-lock systemctl enable nfs-idmap
systemctl start rpcbind systemctl start nfs-server systemctl start nfs-lock systemctl start nfs-idmap
Mount NFS shares On clients
Create a mount point to mount the shared folder ‘var/unixmen_share’ which we’ve created before in the server.
mkdir /var/nfs_share
Mount the share from server to client as shown below
mount -t nfs 192.168.1.101:/var/unixmen_share/ /var/nfs_share/
Sample Output:
mount.nfs: Connection timed out
Probably, it will show a connection timed out error which means that the firewall is blocking our NFS server. To access NFS shares from remote clients, we must allow the following nfs ports in the NFS server iptables/firewall.
If you don’t know which ports to allow through firewall, run the following command:
rpcinfo -p
Sample output:
program vers proto port service 100000 4 tcp 111 portmapper 100000 3 tcp 111 portmapper 100000 2 tcp 111 portmapper 100000 4 udp 111 portmapper 100000 3 udp 111 portmapper 100000 2 udp 111 portmapper 100024 1 udp 60985 status 100024 1 tcp 54302 status 100005 1 udp 20048 mountd 100005 1 tcp 20048 mountd 100005 2 udp 20048 mountd 100005 2 tcp 20048 mountd 100005 3 udp 20048 mountd 100005 3 tcp 20048 mountd 100003 3 tcp 2049 nfs 100003 4 tcp 2049 nfs 100227 3 tcp 2049 nfs_acl 100003 3 udp 2049 nfs 100003 4 udp 2049 nfs 100227 3 udp 2049 nfs_acl 100021 1 udp 46666 nlockmgr 100021 3 udp 46666 nlockmgr 100021 4 udp 46666 nlockmgr 100021 1 tcp 42955 nlockmgr 100021 3 tcp 42955 nlockmgr 100021 4 tcp 42955 nlockmgr 100011 1 udp 875 rquotad 100011 2 udp 875 rquotad 100011 1 tcp 875 rquotad 100011 2 tcp 875 rquotad
You should allow the above ports.
To do that, go to the NFS server, and run the following commands:
firewall-cmd --permanent --add-port=111/tcp firewall-cmd --permanent --add-port=54302/tcp firewall-cmd --permanent --add-port=20048/tcp firewall-cmd --permanent --add-port=2049/tcp firewall-cmd --permanent --add-port=46666/tcp firewall-cmd --permanent --add-port=42955/tcp firewall-cmd --permanent --add-port=875/tcp
Restart firewalld service to take effect the changes:
firewall-cmd --reload
Again mount the share in client system with command:
mount -t nfs 192.168.1.101:/var/unixmen_share/ /var/nfs_share/
Now the NFS share will mount without any connection timed out error.
Verifying NFS Shares On Clients
Verify the share from the server is mounted or not using ‘mount’ command.
mount
Sample output:
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel) devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=309620k,nr_inodes=77405,mode=755) securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000) tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755) tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,seclabel,mode=755) cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd) pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime) cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset) cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu) cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory) cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices) cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer) cgroup on /sys/fs/cgroup/net_cls type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls) cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio) cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event) cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb) configfs on /sys/kernel/config type configfs (rw,relatime) /dev/mapper/centos-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota) selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime) systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=32,pgrp=1,timeout=300,minproto=5,maxproto=5,direct) hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel) mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel) debugfs on /sys/kernel/debug type debugfs (rw,relatime) /dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime) sunrpc on /proc/fs/nfsd type nfsd (rw,relatime) 192.168.1.101:/var/unixmen_share on /var/nfs_share type nfs4 (rw,relatime,vers=4.0,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.102,local_lock=none,addr=192.168.1.101)
Auto mount NFS Shares
To mount the shares automatically instead of mounting them manually on every reboot, add the following lines shown in bold in the ‘/etc/fstab’ file of your client system.
vi /etc/fstab
# # /etc/fstab # Created by anaconda on Tue Aug 19 12:15:24 2014 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 1 1 UUID=2ba8d78a-c420-4792-b381-5405d755e544 /boot xfs defaults 1 2 /dev/mapper/centos-swap swap swap defaults 0 0 192.168.1.101:/var/unixmen_share/ /var/nfs_share/ nfs rw,sync,hard,intr 0 0
Reboot the client system and check the share whether it is automatically mounted or not.
mount
Sample output:
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel) devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=309620k,nr_inodes=77405,mode=755) securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000) tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755) tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,seclabel,mode=755) cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd) pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime) cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset) cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu) cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory) cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices) cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer) cgroup on /sys/fs/cgroup/net_cls type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls) cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio) cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event) cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb) configfs on /sys/kernel/config type configfs (rw,relatime) /dev/mapper/centos-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota) selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime) systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=32,pgrp=1,timeout=300,minproto=5,maxproto=5,direct) hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel) mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel) debugfs on /sys/kernel/debug type debugfs (rw,relatime) /dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime) sunrpc on /proc/fs/nfsd type nfsd (rw,relatime) 192.168.1.101:/var/unixmen_share on /var/nfs_share type nfs4 (rw,relatime,vers=4.0,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.102,local_lock=none,addr=192.168.1.101)
Thats it. Now NFS server is ready to use.