Red Hat / CentOS

How to Install and Configure an NFS Server on CentOS 8

How to Install and Configure an NFS Server on CentOS 8

How to Install and Configure an NFS Server on CentOS 8

This guide will explain how to install NFS server on RHEL 8 / CentOS 8 Linux server. NFS stands for Network File System. It enables client systems to access files that are stored on a remote shared server over a network and make use of those file systems as if they are locally mounted. NFS is a client-and-server file system(FS).

By using NFS shared storage, system administrators can consolidate resources onto centralized servers on the network. Files are easily shared between multiple systems on the same network. A client system can access the remote share with (read, write) privileges and do not have access to the underlying block storage.

Supported NFS versions

Below are the versions of NFS supported by RHEL 8.

NFS version 3 (NFSv3)

  • Has support for safe asynchronous writes and is more robust at error handling than the previous NFSv2
  • Supports 64-bit file sizes and offsets, allowing clients to access more than 2 GB of file data.

NFS version 4 (NFSv4)

  • Works through firewalls and on the Internet
  • No longer requires rpcbind service
  • Supports Access Control Lists (ACLs)
  • Utilizes stateful operations.

In this guide, we will setup NFSv4.2 on our RHEL/CentOS system.

Prerequisites

We’re assuming that you have a server running CentOS 8 on which we will set up the NFS server and other machines that will act as NFS clients. The server and the clients should be able to communicate with each other over a private network. If your hosting provider doesn’t offer private IP addresses, you can use the public IP addresses and configure the server firewall to allow traffic on port 2049 only from trusted sources.

The machines in this example have the following IPs:
NFS Server IP: 10.20.20.8
NFS Client IP: 10.20.20.9

+----------------------+          |          +----------------------+
| [    NFS Server    ] |10.20.20.8|10.20.20.9| [    NFS Client    ] |
|      10.20.20.8      +----------+----------+       10.20.20.9     |
|                      |                     |                      |
+----------------------+                     +----------------------+

Install and Configure an NFS Server on CentOS 8

Update server and set hostname

# dnf -y update
# hostnamectl set-hostname server.example.com --static

Set Up the NFS Server

This section explains how to install the necessary packages, create and export the NFS directories, and configure the firewall.

1. Installing the NFS server

The “nfs-utils” package provides the NFS utilities and daemons for the NFS server. To install it run the following command:

# 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 nfsd, nfs-idmapd, rpcbind, rpc.mountd, lockd, rpc.statd, rpc.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. Create the export filesystem using the mkdir command:

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

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.
/mnt/nfs_shares/Human_Resource 10.20.20.0/24(rw,sync)
/mnt/nfs_shares/Finance 10.20.10.0/24(rw,sync)
/mnt/nfs_shares/Marketing 10.20.30.0/24(rw,sync)
/mnt/backups 10.20.20.9/24(rw,sync,no_all_squash,root_squash)

For basic options of exports

Option Description
rw Allow both read and write requests on a NFS volume.
ro Allow only read requests on a NFS volume.
sync Reply to requests only after the changes have been committed to stable storage. (Default)
async This option allows the NFS server to violate the NFS protocol and reply to requests before any changes made by that request have been committed to stable storage.
secure This option requires that requests originate on an Internet port less than IPPORT_RESERVED (1024). (Default)
insecure This option accepts all ports.
wdelay Delay committing a write request to disc slightly if it suspects that another related write request may be in progress or may arrive soon. (Default)
no_wdelay This option has no effect if async is also set. The NFS server will normally delay committing a write request to disc slightly if it suspects that another related write request may be in progress or may arrive soon. This allows multiple write requests to be committed to disc with the one operation which can improve performance. If an NFS server received mainly small unrelated requests, this behaviour could actually reduce performance, so no_wdelay is available to turn it off.
subtree_check This option enables subtree checking. (Default)
no_subtree_check This option disables subtree checking, which has mild security implications, but can improve reliability in some circumstances.
root_squash Map requests from uid/gid 0 to the anonymous uid/gid. Note that this does not apply to any other uids or gids that might be equally sensitive, such as user bin or group staff.
no_root_squash Turn off root squashing. This option is mainly useful for disk-less clients.
all_squash Map all uids and gids to the anonymous user. Useful for NFS exported public FTP directories, news spool directories, etc.
no_all_squash Turn off all squashing. (Default)
anonuid=UID These options explicitly set the uid and gid of the anonymous account. This option is primarily useful for PC/NFS clients, where you might want all requests appear to be from one user. As an example, consider the export entry for /home/joe in the example section below, which maps all requests to uid 150.
anongid=GID Read above (anonuid=UID)

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

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

7. Next, if you have the firewalld service running, you need to allow traffic to the necessary NFS services (mountd, nfs, rpc-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

SELinux boolean may need to be enabled.

# setsebool -P nfs_export_all_rw 1

How to Install and Configure an NFS Server on CentOS 8

Set Up the NFS Clients

Now that the NFS server is setup and shares are exported, the next step configure the clients and mount the remote file systems.

You can also mount the NFS share on macOS and Windows machines, but we will focus on Linux systems

8. Install NFS client on CentOS Fedora and Debian/Ubuntu

# dnf install nfs-utils nfs4-acl-tools         [On CentOS/RHEL]
$ sudo apt install nfs-common nfs4-acl-tools   [On Debian/Ubuntu]

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

How to Install and Configure an NFS Server on CentOS 8
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

Verify that the remote file systems are mounted successfully using either the mount or dfcommand:

df -sh

10. To make the mounts permanent on reboot, open the /etc/fstab file:

# echo "10.20.20.8:/mnt/backups     /mnt/backups  nfs     defaults">>/etc/fstab
# cat /etc/fstab

How to Install and Configure an NFS Server on CentOS 8

Testing NFS Access

11. 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]

Unmounting NFS File System

12. If you no longer need the remote NFS share, you can unmount it as any other mounted file system using the umount command. For example, to unmount the /backups share you would run:

# umount /mnt/backups

If the mount point is defined in the /etc/fstab file, make sure you remove the line or comment it out by adding # at the beginning of the line.

end How to Install and Configure an NFS Server on CentOS 8

Conclusion

In this tutorial, we have shown you how to set up an NFS server and how to mount the remote file systems on the client machines. If you’re implementing NFS in production and sharing sensible data, it is a good idea to enable kerberos authentication.

Feel free to leave a comment if you have any questions.

About the author

Avatar

jon snow

Jon is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, and currently a content creator for ERRORHAT who loves working with computers and strongly believes in sharing knowledge.

Add Comment

Click here to post a comment