How to Setup NFS Server on CentOS 8

Introduction to Setup NFS server

Setup NFS Server, This is a quick and easy guide for any newbie. The NFS or Network File System requirement is common across all kind of development or production environment. Mostly, this requirement will come whenever we need a temporary solution to back up something in production or non-production environment.

NFS will come into picture when we need to share a common filesystem across multiple Linux clients. Let’s focus on simple setup, for now. Just set up a fresh NFS server and share the filesystem as NFS shares to mount on a Linux client.

 +--------------------------------------------------------------------+
 |                         www.linuxsysadmins.com                     |
 |                         +--------------+          +--------------+ |
 |  +-------------+        |              |          |              | |
 |  |             |        |   Client 1   |          | 192.168.0.43 | |
 |  |             +------->+ 192.168.0.56 |          |   Some other | |
 |  |                      |              |          |    client    | |
 |  |             |        +--------------+          +--------------+ |
 |  | NFS Server  |                                         |         |
 |  |192.168.0.55 +-----------------------------------------+         |
 |  |             |                                         |         |
 |  |             |                                  +------v-------+ |
 |  |             |         +--------------+         |              | |
 |  |             +-------->+              |         | 192.168.0.44 | |
 |  |             |         | 192.168.0.42           |  Some other  | |
 |  +-------------+         |  Some other  |         |    client    | |
 |                          |    client    |         +--------------+ |
 |                          +--------------+                          |
 +--------------------------------------------------------------------+

My Server Setup

SERVER IP ADDRESSCLIENTS IP ADDRESS
192.168.0.55192.168.0.56, 192.168.0.42-45

Setup NFS Server

Let’s start to setup the nfs server by installing below package.

# dnf install nfs-utils -y        # RHEL 8, CentOS Linux 8, Oracle Linux 8                     
# yum install nfs-utils -y        # RHEL 7, CentOS Linux 7, Oracle Linux 7
# apt install nfs-kernel-server   # Debian and Ubuntu

Once we run yum or dnf it will pull the required packages and it’s dependencies.

===============================================================================================================
 Package                         Architecture         Version                       Repository            Size
===============================================================================================================
Installing:
 nfs-utils                       x86_64               1:2.3.3-31.el8                BaseOS               468 k
Installing dependencies:
 gssproxy                        x86_64               0.8.0-15.el8                  BaseOS               118 k
 keyutils                        x86_64               1.5.10-6.el8                  BaseOS                63 k
 libevent                        x86_64               2.1.8-5.el8                   BaseOS               253 k
 libverto-libevent               x86_64               0.3.0-5.el8                   BaseOS                16 k
 quota                           x86_64               1:4.04-10.el8                 BaseOS               214 k
 quota-nls                       noarch               1:4.04-10.el8                 BaseOS                94 k
 rpcbind                         x86_64               1.2.5-7.el8                   BaseOS                70 k

Transaction Summary
===============================================================================================================
Install  8 Packages

Start and Enable NFS service

Right after installing the packages, enable the service to start persistently and start the service.

Setup NFS server
# systemctl enable rpcbind nfs-server
# systemctl start nfs-server

While checking for the status we should see similar to below.

[root@nfsserver ~]# systemctl status rpcbind nfs-server
● rpcbind.service - RPC Bind
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-09-04 12:03:36 +04; 2min 39s ago
     Docs: man:rpcbind(8)
 Main PID: 13884 (rpcbind)
    Tasks: 1 (limit: 11331)
   Memory: 1.6M
   CGroup: /system.slice/rpcbind.service
           └─13884 /usr/bin/rpcbind -w -f

Sep 04 12:03:36 nfsserver.linuxsysadmins.local systemd[1]: Starting RPC Bind...
Sep 04 12:03:36 nfsserver.linuxsysadmins.local systemd[1]: Started RPC Bind.

● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
   Active: active (exited) since Fri 2020-09-04 12:03:36 +04; 2min 38s ago
 Main PID: 13915 (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 11331)
   Memory: 0B
   CGroup: /system.slice/nfs-server.service

Sep 04 12:03:36 nfsserver.linuxsysadmins.local systemd[1]: Starting NFS server and services...
Sep 04 12:03:36 nfsserver.linuxsysadmins.local systemd[1]: Started NFS server and services.
[root@nfsserver ~]#

Firewall for NFS

Back in RHEL 6 version’s we require to write iptables manually, however, in current RHEL 7 and 8 based Linux distributions, it’s super easy to enable firewall rules by running below commands.

# firewall-cmd --add-service={nfs,nfs3,rpc-bind,mountd} --permanent
# firewall-cmd --reload

By running the above commands it will allow the ports 2049, 111 and 20048.

[root@nfsclient ~]# cat /usr/lib/firewalld/services/nfs.xml 
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>NFS4</short>
  <description>The NFS4 protocol is used to share files via TCP networking. You will need to have the NFS tools installed and properly configure your NFS server for this option to be useful.</description>
  <port protocol="tcp" port="2049"/>
</service>
[root@nfsclient ~]# 
[root@nfsclient ~]# cat /usr/lib/firewalld/services/nfs3.xml 
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>NFS3</short>
  <description>The NFS3 protocol is used to share files. You will need to have the NFS tools installed and properly configure your NFS server for this option to be useful.</description>
  <port protocol="tcp" port="2049"/>
  <port protocol="udp" port="2049"/>
</service>
[root@nfsclient ~]# 
[root@nfsclient ~]# cat /usr/lib/firewalld/services/rpc-bind.xml 
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>rpc-bind</short>
  <description>Remote Procedure Call Bind</description>
  <port protocol="tcp" port="111"/>
  <port protocol="udp" port="111"/>
</service>
[root@nfsclient ~]# 
[root@nfsclient ~]# cat /usr/lib/firewalld/services/mountd.xml 
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>mountd</short>
  <description>NFS Mount Lock Daemon</description>
  <port protocol="tcp" port="20048"/>
  <port protocol="udp" port="20048"/>
</service>
[root@nfsclient ~]#

SELinux Configuration

If you need to allow all exports to have read and write access enable the below Boolean for SELinux.

# setsebool -P nfs_export_all_rw 1

Let’s create a share now.

Create and Export NFS share

Create a nfs share for the clients. For demonstration purpose let’s create the mount point /nfsshare.

# mkdir /nfsshare

If you need to create a share for Kubernetes or OpenShift storage, make sure to change the permission and ownership as well.

# chmod -R 777 /nfsshare
# chown -R nobody:nobody /nfsshare

To share the required nfs shares we need to put inside the exports file.

# vim /etc/exports
/nfsshare 192.168.0.0/24(rw,sync,no_root_squash,no_all_squash,no_wdelay))         # NFS share for a whole subnet.
/nfspub *(rw,no_root_squash)                        # NFS share for public use.
/nfslimit 192.168.0.[42-44]/24(rw,no_root_squash)   # NFS share for specific range of hosts.

Save and exit the exports file using wq!. By following, run the below command to export and make it to available for the clients.

# exportfs -a

NFS share Options

For a successful NFS setup, we need to understand the options available in it.

Refer the exports, look into the man page for more information regarding nfs exports options.

That’s it, we have completed with the server side setup.

NFS Client Setup

To start with the client-side setup, first of all, install the same package which we installed in server-side, this applied for both RHEL 7 and 8 version and its family OS CentOS or Oracle Linux.

# dnf install nfs-utils -y        # RHEL 8, CentOS Linux 8, Oracle Linux 8
# yum install nfs-utils -y        # RHEL 7, CentOS Linux 7, Oracle Linux 7
# apt install nfs-common -y       # Debian and Ubuntu 

Enable and Start client Service

Enable and start the nfs-client service by running systemctl command.

# systemctl enable nfs-client.target 
# systemctl start nfs-client.target 

Listing the NFS share

To know the list of NFS shares in the client side we can use list by running

# showmount -e 192.168.0.55

The option -e determines --exports It will show the NFS server’s export list.

[root@nfsclient ~]# showmount -e 192.168.0.55
Export list for 192.168.0.55:
/nfspub   *
/nfslimit 192.168.0.[42-44]/24
/nfsshare (everyone)
[root@nfsclient ~]# 

Mounting the NFS share

Let’s mount the filesystem now under anyone of the directory. The NFS share /nfsshare exported for the whole subnet, let me mount it under /nfsmount.

# mkdir /nfsmount

It’s not mandatory to use -t or –types the new operating system will recognize the type of files we are trying to mount and use the required types/protocols.

# mount -t nfs 192.168.0.55:/nfsshare /nfsmount/

Let’s list the mounted file system.

[root@nfsclient ~]# df -hP /nfsmount/
Filesystem              Size  Used Avail Use% Mounted on
192.168.0.55:/nfsshare   50G  1.9G   49G   4% /nfsmount
[root@nfsclient ~]# 

Let’s test the mount file-system by creating some files under it.

[root@nfsclient ~]# cd /nfsmount/
[root@nfsclient nfsmount]# 
[root@nfsclient nfsmount]# touch test.txt
[root@nfsclient nfsmount]# 
[root@nfsclient nfsmount]# ls -lthr test.txt 
-rw-r--r--. 1 root root 0 Sep  4  2020 test.txt
[root@nfsclient nfsmount]# 

Still now, we are good with mounting and creating the files under new NFS share.

Mounting NFS Shares Persistently

To mount the NFS shares permanently we need to put it in FSTAB entry. Get the entry from /etc/mtab and modified as per our requirement.

[root@nfsclient ~]# cat /etc/mtab | tail -n 1
192.168.0.55:/nfsshare /nfsmount nfs4 rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.0.56,local_lock=none,addr=192.168.0.55 0 0
[root@nfsclient ~]# 

Edit the FSTAB entry and add it as follows.

# vim /etc/fstab

NFS_SERVER_IPA:/NFS_SHARES /MOUNT_POINT FILETYPE DEFAULT_MOUNT_OPTIONS 0 0

192.168.0.55:/nfsshare  /nfsmount               nfs4    defaults,_netdev,vers=4.2,sec=sys 0 0

The mount options we used are

defaultsdefault options are used while mounting this file system.
_netdevWhile adding this option, prevent the system from attempting to mount these filesystems until the network has been enabled on the system.
vers=4.2The version of NFS need to be used to mount the filesystem.
sec=syswhich uses local Unix UIDs and GIDs by using AUTH_SYS to authenticate NFS operations.
0Exclude the file system from the backup.
0Exclude from running the fsck

Mounting an un-allowed NFS Share

We have seen above how to mount an NFS share from the same subnet, let’s see what happen when we try to mount an NFS share which not allowed for our host/network.

# mount -t nfs 192.168.0.55:/nfslimit /nfsmount/

We will get the below error as No such file or directory.

[root@nfsclient ~]# mount -t nfs 192.168.0.55:/nfslimit /nfsmount/
mount.nfs: mounting 192.168.0.55:/nfslimit failed, reason given by server: No such file or directory
[root@nfsclient ~]# 

That’s it, we have successfully completed with setting up a NFS share.

Conclusion

To share a file system over the network, we can stick with NFS. It’s very simple and easy to complete the setup in few steps. Will keep posted with more content related to network filesystem in upcoming guides. Subscribe to our newsletter to receive the updates.

Exit mobile version