Setting up NFS server and client on centos 7
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.
Services:
rpcbind : The rpcbind server converts RPC program numbers into universal addresses.
nfs-server : It enables the clients to access NFS shares.
nfs-lock / rpc-statd : NFS file locking. Implement file lock recovery when an NFS server crashes and reboots.
nfs-idmap : It translates user and group ids into names, and to translate user and group names into ids
rpc.mountd: This service is responsible for mounting and unmounting of file systems
I will be using two systems which are running with CentOS 7. The same steps are applicable for RHEL and Scientific Linux 7 distributions.
server host/ip: lampblogs.com/192.168.0.13
client host/ip : node2/192.168.0.50
Server side:
Step 1: verify package and install
[root@lampblogs ~]# rpm -qa | grep nfs
If nfs packages are not installed,then install them by below command
[root@lampblogs ~]# yum install nfs-utils
Once the packages are installed, enable and start NFS services.
[root@lampblogs ~]# systemctl start nfs-server rpcbind
[root@lampblogs ~]# systemctl enable nfs-server rpcbind
Step 2: Create NFS share
Now, let’s create a directory to share with the NFS client.
[root@lampblogs ~]# mkdir /nfsshare
Allow NFS client to read and write to the created directory.
[root@lampblogs ~]# chmod 777 /nfsshare
Now we need to make an entry in “/etc/exports” and restart the services to make our directory shareable in the network.
[root@lampblogs ~]# vi /etc/exports
Create a NFS share something like below.
/nfsshare 192.168.0.50(rw,sync,no_root_squash)
NFS options:
/nfsshare: shared directory
192.168.0.50: IP address of client machine. We can also use the hostname instead of an IP address. It is also possible to define the range of clients with subnet like 192.168.0.1/24.
ro: With the help of this option we can provide read only access to the shared files i.e client will only be able to read.
rw: Writable permission to shared folder
sync: All changes to the according filesystem are immediately flushed to disk; the respective write operations are being waited for.
async : specifies that the server does not have to wait.
wdelay : NFS server delays committing write requests when it suspects another write request is imminent.
no_wdelay : use this option to disable to the delay. no_wdelay option can only be enabled if default sync option is enabled.
no_root_squash: By default, any file request made by user root on the client machine is treated as by user nobody on the server. If no_root_squash is selected, then root on the client machine will have the same level of access to the files on the system as root on the server.
root_squash : prevent root users connected remotely from having root access. Effectively squashing remote root privileges.
You can get to know all the option in the man page man exports or here
Export the shared directories using the following command.
[root@lampblogs ~]# exportfs -r
After configuring NFS server, we need to mount that shared directory in the NFS client.
Client side (192.168.0.50):
Install NFS packages on NFS client to mount a remote NFS share if you don't have them in your system
yum install -y nfs-utils
Check nfs share
[root@node2 ~]# showmount -e 192.168.0.13
Export list for 192.168.0.13:
/nfsshare 192.168.0.50
As per the output, the /nfsshare is available on the NFS server (192.168.0.13) for the NFS client (192.168.0.50).
Note:
showmount -e : Shows the available shares on your local machine (NFS Server).
showmount -e <server-ip or hostname>: Lists the available shares on the remote server
showmount -d: Lists all subdirectories
Step 3: Mount NFS share
Now, create a directory on NFS client to mount the NFS share /nfsshare which we have created in the NFS server.
[root@node2 ~]# mkdir /mnt/nfsshare
Use below command to mount a NFS share on NFS client.
[root@node2 ~]# mount 192.168.0.13:/nfsshare /mnt/nfsshare
Verify the mounted share on the NFS client using mount command
[root@node2 ~]# mount | grep nfs
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
192.168.0.13:/nfsshare on /mnt/nfsshare type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.0.50,local_lock=none,addr=192.168.0.13)
Also, you can use the df -Th command to check the mounted NFS share.
[root@node2 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos_node2-root xfs 28G 3.7G 24G 14% /
devtmpfs devtmpfs 728M 0 728M 0% /dev
tmpfs tmpfs 745M 0 745M 0% /dev/shm
tmpfs tmpfs 745M 9.2M 736M 2% /run
tmpfs tmpfs 745M 0 745M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 157M 858M 16% /boot
tmpfs tmpfs 149M 12K 149M 1% /run/user/42
tmpfs tmpfs 149M 0 149M 0% /run/user/0
192.168.0.13:/nfsshare nfs4 22G 3.7G 18G 18% /mnt/nfsshare
Create a file on the mounted directory to verify the read and write access on NFS share.
[root@node2 ~]# touch /mnt/nfsshare/abc
Now its working perfectly.
Automount NFS shares:
To mount the shares automatically on every reboot, you would need to modify /etc/fstab file of your NFS client.
[root@node2 ~]# vi /etc/fstab
Add an entry something like below.
#
# /etc/fstab
# Created by anaconda on Tue Jul 23 16:38:10 2019
#
# 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_node2-root / xfs defaults 0 0
UUID=b9098bc4-6586-4e91-a350-53bfb70dc8c5 /boot xfs defaults 0 0
/dev/mapper/centos_node2-swap swap swap defaults 0 0
192.168.0.13:/nfsshare /mnt/nfsshare nfs nosuid,rw,sync,hard,intr 0 0
Save and close the file.
Reboot the client machine and check whether the share is automatically mounted or not.
[root@node2 ~]# reboot
Verify the mounted share on the NFS client using mount command.
[root@node2 ~]# mount | grep nfs
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
192.168.0.13:/nfsshare on /mnt/nfsshare type nfs4 (rw,nosuid,relatime,sync,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.0.50,local_lock=none,addr=192.168.0.13)
If you want to unmount that shared directory from your NFS client
[root@node2 ~]# umount /mnt/nfsshare
Firewalld services to be active and disable selinux also on server side
# firewall-cmd --add-service=nfs --zone=internal --permanent
# firewall-cmd --add-service=mountd --zone=internal --permanent
# firewall-cmd --add-service=rpc-bind --zone=internal --permanent
# firewall-cmd --reload
Cheers, now we have a successfully configured NFS-server over CentOS 7 :)