Network File System (NFS) also known as client/server file system is a popular, cross-platform and distributed file system protocol used to export local file systems over the network so that clients can share directories and files with others over a network and interact with them as though they are mounted locally.

In CentOS/RHEL 8, the supported NFS version are NFSv3 and NFSv4 and the default NFS version is 4.2 which features support for Access Control Lists (ACLs), server-side copy, sparse files, space reservation, labeled NFS, layout enhancements, and much more.

In this article, you will learn how to install and configure the NFS server and NFS client on CentOS/RHEL 8 Linux distributions.

Prerequisites:

  1. CentOS 8 Installation Guide
  2. RHEL 8 Minimal Installation
  3. Enable RHEL Subscription in RHEL 8
  4. Set a Static IP Address in CentOS/RHEL 8

Our Testing Environment:

NFS Server IP:	10.130.4.220
NFS Client IP:	10.130.4.200

Setting Up NFS Server on CentOS 8

1. First, start by installing the required packages on the NFS server. The packages are nfs-utils which provides a daemon for the kernel NFS server and related tools such as the contains the showmount program.

Run the following command to install the package on the NFS server (use sudo if you are administering the system as a non-root user).

sudo dnf install nfs-utils

2. Once the installation is complete, start the nfs-server service, enable it to automatically start at system boot, and then verify its status using the systemctl commands.

systemctl start nfs-server.service
systemctl enable nfs-server.service
systemctl status nfs-server.service

Note that the other services that are required for running an NFS server or mounting NFS shares such as nfsdnfs-idmapdrpcbindrpc.mountdlockdrpc.statdrpc.rquotad, and rpc.idmapd will be automatically started.

The configuration files for the NFS server are:

  • /etc/nfs.conf – main configuration file for the NFS daemons and tools.
  • /etc/nfsmount.conf – an NFS mount configuration file.

3. Next, create the file systems to export or share on the NFS server. For this guide, we will create four file systems, three of which are used by staff from three departments: human resource, finance and marketing to share files and one is for root user backups.

Membuat folder, misalnya :

# mkdir -p  /mnt/nfs_shares/{Human_Resource,Finance,Marketing}
# mkdir  -p /mnt/backups
# ls -l /mnt/nfs_shares/
mkdir  -p /var/www/html

4. Then export the above file systems in the NFS server /etc/exports configuration file to determine local physical file systems that are accessible to NFS clients.

/var/www/html  	10.20.20.0/24(rw,sync)

Here are some of the exports options (read man exports for more information and export options):

  • rw – allows both read and write access on the file system.
  • sync – tells the NFS server to write operations (writing information to the disk) when requested (applies by default).
  • all_squash – maps all UIDs and GIDs from client requests to the anonymous user.
  • no_all_squash – used to map all UIDs and GIDs from client requests to identical UIDs and GIDs on the NFS server.
  • root_squash – maps requests from root user or UID/GID 0 from the client to the anonymous UID/GID.

5. To export the above file system, run the exportfs command with the -a flag means export or unexport all directories, -r means reexport all directories, synchronizing /var/lib/nfs/etab with /etc/exports and files under /etc/exports.d, and -v enables verbose output.

exportfs -arv
[root@dev-20 ~]# exportfs -arv
exporting 10.130.4.0/24:/var/www/html
[root@dev-20 ~]#

6. To display the current export list, run the following command. Note that the exports table also applies some of the default exports options that are not explicitly defined as shown in the following screenshot.

exportfs  -s
[root@dev-20 ~]# exportfs  -s
/var/www/html  10.130.4.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
[root@dev-20 ~]#

7. Next, if you have the firewalld service running, you need to allow traffic to the necessary NFS services (mountdnfsrpc-bind) via the firewall, then reload the firewall rules to apply the changes, as follows.

firewall-cmd --permanent --add-service=nfs
firewall-cmd --permanent --add-service=rpc-bind
firewall-cmd --permanent --add-service=mountd
firewall-cmd --reload

Setting Up NFS Client on Client Systems

8. Now on the client node(s), install the necessary packages to access NFS shares on the client systems. Run the appropriate command for your distribution:

# dnf install nfs-utils nfs4-acl-tools         [On CentOS/RHEL]
$ sudo apt install nfs-common nfs4-acl-tools   [On Debian/Ubuntu]
root@masadmin:~# sudo apt install nfs-common nfs4-acl-tools
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
nfs-common is already the newest version (1:2.6.1-1ubuntu1.2).
The following packages were automatically installed and are no longer required:
  libflashrom1 libftdi1-2
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  nfs4-acl-tools
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 27.5 kB of archives.
After this operation, 82.9 kB of additional disk space will be used.
Do you want to continue? [Y/n] n
Abort.
root@masadmin:~#
root@masadmin:~# sudo apt remove nfs-common
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libflashrom1 libftdi1-2 libnfsidmap1 rpcbind
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  nfs-common
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 906 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 110172 files and directories currently installed.)
Removing nfs-common (1:2.6.1-1ubuntu1.2) ...
Processing triggers for man-db (2.10.2-1) ...
root@masadmin:~#
root@masadmin:~# sudo apt install nfs-common nfs4-acl-tools
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libflashrom1 libftdi1-2
Use 'sudo apt autoremove' to remove them.
Suggested packages:
  watchdog
The following NEW packages will be installed:
  nfs-common nfs4-acl-tools
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 268 kB of archives.
After this operation, 989 kB of additional disk space will be used.
Get:1 http://id.archive.ubuntu.com/ubuntu jammy-updates/main amd64 nfs-common amd64 1:2.6.1-1ubuntu1.2 [241 kB]
Get:2 http://id.archive.ubuntu.com/ubuntu jammy/universe amd64 nfs4-acl-tools amd64 0.3.7-1 [27.5 kB]
Fetched 268 kB in 1s (232 kB/s)
Selecting previously unselected package nfs-common.
(Reading database ... 110092 files and directories currently installed.)
Preparing to unpack .../nfs-common_1%3a2.6.1-1ubuntu1.2_amd64.deb ...
Unpacking nfs-common (1:2.6.1-1ubuntu1.2) ...
Selecting previously unselected package nfs4-acl-tools.
Preparing to unpack .../nfs4-acl-tools_0.3.7-1_amd64.deb ...
Unpacking nfs4-acl-tools (0.3.7-1) ...
Setting up nfs-common (1:2.6.1-1ubuntu1.2) ...
auth-rpcgss-module.service is a disabled or a static unit not running, not starting it.
nfs-idmapd.service is a disabled or a static unit not running, not starting it.
nfs-utils.service is a disabled or a static unit not running, not starting it.
proc-fs-nfsd.mount is a disabled or a static unit not running, not starting it.
rpc-gssd.service is a disabled or a static unit not running, not starting it.
rpc-statd-notify.service is a disabled or a static unit not running, not starting it.
rpc-statd.service is a disabled or a static unit not running, not starting it.
rpc-svcgssd.service is a disabled or a static unit not running, not starting it.
rpc_pipefs.target is a disabled or a static unit not running, not starting it.
var-lib-nfs-rpc_pipefs.mount is a disabled or a static unit not running, not starting it.
Setting up nfs4-acl-tools (0.3.7-1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning candidates...
Scanning linux images...

Restarting services...
Service restarts being deferred:
 /etc/needrestart/restart.d/dbus.service
 systemctl restart networkd-dispatcher.service
 systemctl restart systemd-logind.service
 systemctl restart unattended-upgrades.service
 systemctl restart [email protected]

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
root@masadmin:~#

9. Then run the showmount command to show mount information for the NFS server. The command should output the exported file system on the client as shown in the screenshot.

showmount -e 10.20.20.8
root@masadmin:~# showmount -e 10.130.4.220
Export list for 10.130.4.220:
/var/www/html 10.130.4.0/24
root@masadmin:~#

9. Next, create a local file system/directory for mounting the remote NFS file system and mount it as an ntf file system.

# mkdir -p /mnt/backups
# mount -t nfs  10.20.20.8:/mnt/backups /mnt/backups

10. Then confirm that the remote file system has been mounted by running the mount command and filter nfs mounts.

mount | grep nfs
root@masadmin:~# mount | grep nfs
10.130.4.220:/var/www/html on /data/nfs-share type nfs4 (rw,relatime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.130.4.200,local_lock=none,addr=10.130.4.220)
root@masadmin:~#

11. To enable the mount to persistent even after a system reboot, run the following command to enter the appropriate entry in the /etc/fstab.

# echo "10.20.20.8:/mnt/backups     /mnt/backups  nfs     defaults 0 0">>/etc/fstab
# cat /etc/fstab
Permanently Mount NFS Share on Client System
Permanently Mount NFS Share on Client System

12. Lastly, test if NFS setup is working fine by creating a file on the server and check if the file can be seen in the client.

# touch /mnt/backups/file_created_on_server.text     [On NFS Server]
# ls -l /mnt/backups/file_created_on_server.text     [On NFS client]
Test NFS Setup from Client
Test NFS Setup from Client

Then do the reverse.

# touch /mnt/backups/file_created_on_client.text     [On NFS Client]
# ls -l /mnt/backups/file_created_on_client.text     [On NFS Server]
Test NFS Setup from Server
Test NFS Setup from Server

13. To unmount the remote file system on the client-side.

# umount /mnt/backups

Note that you can not unmount the remote file system if you are operating within it as shown in the following screenshot.

NFS Mount Error
NFS Mount Error

That’s it! In this guide, we showed how to install and configure an NFS server and client in CentOS/RHEL 8. If you have any thoughts to share or questions, use the comment form below to get back to us.

Leave a Reply

Your email address will not be published. Required fields are marked *