NFS (Network File System) is a method of file sharing solution that can be used to easily share files between servers. NFS provides a medium of central management and can allow any users to access their files from any server transparently.
This guide outlines the basic steps required to setup NFS share between two Linux servers in-order to share files/folders among them. Global Private Networking is recommended for setting up NFS share as this helps in the sharing of files securely.
NFS Server should be installed on the server in which the files/folders to be shared resides.
For installing the NFS server, execute the below command of each Linux OS.
# yum -y install nfs-utils
# apt-get -y install nfs-kernel-server nfs-common
Create the folder that needs to be shared between the servers. An already existing folder can also be shared using NFS.
In the context of this guide, the folder named layerstackshare is created in
# mkdir /layerstackshare
In-order to share this folder, the below relevant entry should now need to be made in the
/etc/exports file. This is the main configuration file of NFS.
/etc/exports file using any editor of choice:
# vi /etc/exports
Enter the below line at the bottom of the file:
# /layerstackshare 192.168.2.120 (rw,sync,no_subtree_check)
This line basically means that the folder named layerstackshare under the
/ partition is being shared with another server (client) with IP
192.168.2.120 with read and write
NOTE: The IP
192.168.2.120must be replaced by the original private IP of the NFS client.
Restart the NFS Server by executing the below command of each Linux OS.
# systemctl enable --now nfs-server rpcbind
# service nfs-kernel-server restart
Once the NFS server has been configured, the next step involves setting up the NFS client.
The first step is to install the required common NFS utilities in the NFS client server. For this, execute the below commands of each Linux OS.
# yum install nfs-utils -y
# sudo apt install nfs-common
The server on which the shared folder from the NFS server needs to be mounted is termed as NFS client. For mounting the folder on the client server, execute the below commands.
Create the folder at the client side to which the shared folder is to be mounted:
# mkdir /mnt/lsclientshare
Mount the shared folder at the NFS client server.
# mount 192.168.2.110:/layerstackshare /mnt/lsclientshare
NOTE: In the context of this guide, the IP address
192.168.2.110is the private IP address of the NFS server and needs to be replaced with the original IP address of the NFS server.
The below command can be executed to check and confirm that the mount operation was successful.
# df -h
The working of the NFS server setup can easily be tested by creating a test file in the shared folder layerstackshare at the server side and then checking its availability at the NFS client side.
At the NFS server end, create a test file in the shared folder. In this context, the test file named ls_nfstest.txt has been created inside layerstackshare.
# cat > /layerstackshare/ls_nfstest.txt
Test file to verify NFS Server setup:
At the NFS client side, navigate to the mounted shared folder and check the availability of the shared file as well as the content inside the file.
# ll /mnt/lsclientshare # cat /mnt/lsclientshare/ls_nfstest.txt**