NFS stands for Network File System, which helps you to share files and folders between Linux / Unix systems. NFS enables you to mount a remote share locally.
Benefits of NFS
File / Folder sharing between *nix systems
Allows to mount remote filesystems locally
Can be acted as Centralized Storage system
It can be used as a Storage Domain ( Datastore) for VMware and other Virtualization platforms.
Allows applications to share configuration and data files with multiple nodes.
Allows having updated files across the share.
Here, I will use CentOS 7 for this demo
Configure NFS Server
Install NFS Server
Install the below package for the NFS server using the yum command.
yum install -y nfs-utils
Once the packages are installed, enable and start NFS services.
systemctl start nfs-server rpcbind
systemctl enable nfs-server rpcbind
Create NFS Share
Now, let’s create a directory to share with the NFS client. Here I will be creating a new directory named home in the / partition.
mkdir /home
Allow the NFS client to read and write to the created directory.
chmod 777 /home/
We have to modify the/etc/exports file to make an entry of the directory /home that you want to share.
vi /etc/exports
and add this
/home 192.168.100.187(rw,sync,no_root_squash)
192.168.100.187: 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 subnets like 192.168.100.0/24.
rw: Writable permission to a shared folder
sync: All changes to the according filesystem are immediately flushed to disk; the respective write operations are being waited for.
no_root_squash: By default, any file request made by the user root on the client machine is treated as by the user nobody on the server. (Exactly which UID the request is mapped to depends on the UID of user “nobody” on the server, not the client.) If no_root_squash is selected, then the root on the client machine will have the same level of access to the files on the system as the root on the server.
Export the shared directories using the following command.
exportfs -r
After configuring the NFS server, we need to mount that shared directory in the NFS client.
Configure Firewall
We need to configure the firewall on the NFS server to allow the NFS client to access the NFS share. To do that, run the following commands on the NFS server.
firewall-cmd --permanent --add-service mountd
firewall-cmd --permanent --add-service rpc-bind
firewall-cmd --permanent --add-service nfs
firewall-cmd --reload
Configure NFS client
Install NFS Client
We need to install NFS packages on the NFS client to mount a remote NFS share.
yum install -y nfs-utils
Check NFS Share
Before mounting the NFS share, I request you to check the NFS shares available on the NFS server by running the following command on the NFS client.
showmount -e 192.168.100.186
Mount NFS Share
Now, create a directory on the NFS client to mount the NFS share /home which we have created in the NFS server.
mkdir /mnt/home
Use the below command to mount an NFS share /home from NFS server 192.168.100.186 in /mnt/home on the NFS client.
mount 192.168.100.186:/home /mnt/home
Verify the mounted share on the NFS client using the mount command.
mount | grep nfs
OUTPUT:
[root@RMA3 ~]# mount | grep nfs
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
192.168.100.186:/home on /mnt/home type nfs4 (rw,nosuid,relatime,sync,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.100.188,local_lock=none,addr=192.168.100.186)
Also, you can use the df -hT command to check the mounted NFS share.
df -hT
Automount NFS Shares
To mount the shares automatically on every reboot, you would need to modify /etc/fstab file of your NFS client.
vi /etc/fstab
Add an entry something like below.
192.168.100.186:/home /mnt/home nfs nosuid,rw,sync,hard,intr 0 0
[root@rma2 rma2]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jul 13 15:32:36 2023
#
# 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 0 0
UUID=1b5f2893-aa43-4137-b7c9-bb06a599339d /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
192.168.100.186:/home /mnt/home 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.
reboot
Verify the mounted share on the NFS client using the mount command.
mount | grep nfs
Conclusion
You have set up NFS Server and NFS Client on CentOS 7 / RHEL 7 successfully.