NFS stands for Network File Share.
It is a robust cross-platform client/server protocol by means of which files between clients machines are accessed and shared with the NFS Server under a network.
Here at LinuxAPT, as part of our Server Support Services, we regularly help our customers to perform Software and Packages Installations on their Servers.
In this context, we shall look into how to configure NFS Server on CentOS 8 and RHEL 8 Machines.
More about Network File Share (NFS) Server?
With NFS, Client machine can mount the filesystems locally from the NFS Server and easily access files as well as directories same as if it was mounted locally.
NFS version 3 (v3) and Version 4 (v4) are both supported on CentOS 8 and RHEL 8 machines respectively.
NFS v3 allows secure asynchronous write and supports 64- bit file sizes as well as offset while NFS v4 functions via firewall and supports Access Control List (ACL) which does not requires rpcbind service for it to work.
Steps to Set up NFS Server and Client?
To get started, we are going to use the setup information as per below to simulate how NFS protocol functions as a client and Server.
For the Server Machine, the following applies;
Server IP address: 192.168.2.104
Machine: CentOS 8
For the Client Machine, the following applies;
Client IP address: 192.168.2.105
Machine: CentOS 8
1. Now we can get started with NFS installation on the Server Machine.
i. NFS Installation and Configuration on CentOS 8 / RHEL 8 Server
Start by installing NFS Server package known as "nfs-utils" which serves as the NFS daemon. Log into your Server via an SSH tool such as putty and run the command below to install nfs-utils package;
sudo dnf install nfs-utils -y
After the installation process, start and enable the nfs-server service to start automatically on boot by running the commands below;
sudo systemctl start nfs-server.service
sudo systemctl enable nfs-server.service
To confirm if the NFS Service is running correctly, run the command below;
sudo systemctl status nfs-server.service
Now the NFS server should be running as active.
To verify the version of NFS Server installed, run the command below;
rpcinfo -p | grep nfs
The version of NFS is usually indicated in the second column in the output.
The NFS Server configuration file is located at the "/etc/nfs.conf" file while the NFS daemon configuration file (NFS mount) is located at the "/etc/nfsmount.conf" file.
ii. How to create and export NFS share files or directory?
Now, we are going to create a file system which will be shared between the Server and Client Systems.
In this guide, we will create a directory in the Server home at "/home/nfs_share/docs" as shown below by running the command below;
sudo mkdir -p /home/nfs_share/docs
To make this directory accessible and to remove unnecessary restrictions and permissions on the NFS share directory, you need to configure the directory ownership by running the command below;
sudo chown -R nobody: /home/nfs_share/docs
Furthermore, you can adjust the directory ownership permission and assign a read, write, and execute permissions on the NFS share folder with the command below;
sudo chmod -R 777 /home/nfs_share/docs
To make the changes made to take effect, run the command below;
sudo systemctl restart nfs-utils.service
Now let us configure the "/etc/exports" file to enable Export of the NFS share for the client systems to access it. To allow multiple clients access the share, you can specify a subnet as seen below;
Additionally, you can specify other clients on a seperate line in the "/etc/exports" file as shown below;
Basically, to give access to our client machine "192.168.2.105", edit the "/etc/exports" file with the command below;
sudo vi /etc/exports
And then add the following line to this file as shown below;
Now you can save, exit this file and confirm the entry made with the command below;
From the attributes declared in the etc/exports file;
"rw" means read/write permissions granted to the NFS share.
"sync" stands for the parameter required for the writing of the changes on the disk before the operation is done.
"no_all_squash" helps to map all the UIDs & GIDs from the client request to identical UIDs and GIDs.
"root_squash" helps to map requests from the root user on the client-side to an anonymous UID / GID.
Now, to export the above assigned folder, you can use the "exportfs" command as shown below;
sudo exportfs -arv
"-a" option in the command above, signifies that all the directories will be exported.
"-r" means re-exporting all directories.
"-v" is the flag displayed as verbose.
Now, to display the export list, run the command below;
sudo exportfs -s
Next, allow firewall for the NFS Services.
iii. How to configure the firewall rules for NFS Server?
To configure the Server for NFS services access via the firewall on CentOS 8 Server Machine, you can run the "nfs" , "rpc-bind", "mountd" commands respectively as shown below;
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --permanent --add-service=rpc-bind
sudo firewall-cmd --permanent --add-service=mountd
Next, reload the firewall to make the changes to take effect by running the command below;
sudo firewall-cmd --reload
Now we can proceed with setting up NFS shares access on the Client's system.
2. Now we can get started with NFS installation on the Client Machine.
On the Client's machine (CentOS 8), follow the steps below;
i. You can install the NFS packages to allow access to NFS share on the Server machine.
Run the command below;
sudo dnf install nfs-utils nfs4-acl-tools -y
For Ubuntu and Debian System, you can run the command below;
sudo apt install nfs-common nfs4-acl-tools -y
Now you can show the mounted NFS shares on the server with the "showmount" command as shown below;
showmount -e 192.168.2.104
ii. Remote NFS share mounting on the Server
Now, we can mount the remote NFS share directory which is at the local client system.
To begin, start by creating a directory to mount the NFS share with the command below;
sudo mkdir p /home/client_share
Then run the command below to mount the NFS share where the Server IP address is 192.168.2.104;
sudo mount -t nfs 192.168.2.104:/home/nfs_shares/docs /home/client_share
To verify that the remote NFS share has been mounted, run the command below;
sudo mount | grep -i nfs
To make the mount share persistent upon reboot of the server, you need to modify the "etc/fstab" file and append the line as shown below;
192.168.2.104:/home/nfs_shares/docs /home/client_share nfs defaults 0 0
After this implementation, save and exit the file.
3. How to Test the NFS Server and Client Setup?
Now that we have completed the NFS Server and Client configuration, you need to test it to ensure that it is working correctly.
Start by creating a test file in the NFS server share directory and see if it will available in the client's NFS mounted directory. So run the command below;
sudo touch /home/nfs_shares/docs/server_nfs_file.txt
Next check the Client system nfs share mount directory for this file by running the command below;
ls -l /home/client_share/
If everything is working fine, you will see the file here.
Next we will create a file on the NFS client system as well and see if it will be available on the NFS Server.
Now run the command below to create a file on the Client's system;
sudo touch /home/client_share/client_nfs_file.txt
Now, check if this file is available on the Server and if it is working correctly, you will see it in the NFS share directory of the server by running the command below;
ls -l /home/nfs_shares/docs
Now you can see and access the created file.