How To Install And Configure An NFS Server On CentOS 8
In this tutorial, you’ll go through the steps necessary to set up an NFSv4 Server on CentOS 8. We’ll also show you how to mount an NFS file system on the client.
NFS protocol is not encrypted by default, and unlike Samba, it does not provide user authentication. Access to the server is restricted by the clients’ IP addresses or hostnames.
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:
Set Up the NFS Server
This section explains how to install the necessary packages, create and export the NFS directories, and configure the firewall.
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:
Once the installation is complete, enable and start the NFS service by typing:
By default, on CentOS 8 NFS versions 3 and 4.x are enabled, version 2 is disabled. NFSv2 is pretty old now, and there is no reason to enable it. To verify it run the following cat command:
NFS server configuration options are set in /etc/nfsmount.conf and /etc/nfs.conf files. The default settings are sufficient for our tutorial.
Creating the file systems
When configuring an NFSv4 server, it is a good practice is to use a global NFS root directory and bind mount the actual directories to the share mount point. In this example, we will use the /srv/nfs4 directory as NFS root.
To better explain how the NFS mounts can be configured, we’re going to share two directories (/var/www and /opt/backups) with different configuration settings.
The /var/www/ is owned by the user and group apache and /opt/backups is owned by root.
Create the export filesystem using the mkdir command:
Mount the actual directories:
To make the bind mounts permanent, add the following entries to the /etc/fstab file:
Exporting the file systems
The next step is to define the file systems that will be exported by the NFS server, the shares options and the clients that are allowed to access those file systems. To do so open the /etc/exports file:
Export the www and backups directories and allow access only from clients on the 126.96.36.199/27/27 network:
The first line contains fsid=0 which defines the NFS root directory /srv/nfs4. Access to this NFS volume is allowed only to the clients from the 188.8.131.52/27/27 subnet. The crossmnt option is required to share directories that are sub-directories of an exported directory.
The second line shows how to specify multiple export rules for one file system. It exports the /srv/nfs4/backups directory and allows only read access to the whole 184.108.40.206/27/27 range, and both read and write access to 192.168.33.3. The sync option tells NFS to write changes to the disk before replying.
The last line should be self-explanatory. For more information about all the available options, type man exports in your terminal.
Save the file and export the shares:
You need to run the command above each time you modify the /etc/exports file. If there are any errors or warnings, they will be shown on the terminal.
To view the current active exports and their state, use:
The output will include all shares with their options. As you can see, there are also options that we haven’t define in the /etc/exports file. Those are default options, and if you want to change them, you’ll need to set those options explicitly.
root_squash is one of the most important options concerning NFS security. It prevents root users connected from the clients to have root privileges on the mounted shares. It will map root UID and GID to nobody/nogroup UID/GID.
For the users on the client machines to have access, NFS expects the client’s user and group ID’s to match with those on the server. Another option is to use the NFSv4 idmapping feature that translates user and group IDs to names and the other way around.
That’s it. At this point, you have set up an NFS server on your CentOS server. You can now move to the next step and configure the clients and connect to the NFS server.
FirewallD is the default firewall solution on Centos 8 .
The NFS service includes predefined rules for allowing access to the NFS server.
The following commands will permanently allow access from the 220.127.116.11/27 subnet:
Set Up the NFS Clients
Now that the NFS server is setup and shares are exported, the next step is to 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.
Installing the NFS client
On the client’s machines, install the tools required to mount remote NFS file systems.
Install NFS client on Debian and Ubuntu
The name of the package that includes programs for mounting NFS file systems on Debian based distributions is nfs-common. To install it run:
Install NFS client on CentOS and Fedora
On Red Hat and its derivatives install the nfs-utils package:
Mounting file systems
We’ll work on the client machine with IP 192.168.33.110, which has read and write access to the /srv/nfs4/www file system and read-only access to the /srv/nfs4/backups file system.
Create two new directories for the mount points. You can create these directories at any location you want.
Mount the exported file systems with the mount command:
Where 192.168.33.148 is the IP of the NFS server. You can also use the hostname instead of the IP address, but it needs to be resolvable by the client machine. This is usually done by mapping the hostname to the IP in the /etc/hosts file.
When mounting an NFSv4 filesystem, you need to omit the NFS root directory, so instead of /srv/nfs4/backups you need to use /backups.
Verify that the remote file systems are mounted successfully using either the mount or df command:
The command will print all mounted file systems. The last two lines are the mounted shares:
To make the mounts permanent on reboot, open the /etc/fstab file:
and add the following lines:
To find more information about the available options when mounting an NFS file system, type man nfs in your terminal.
Another option to mount the remote file systems is to use either the autofs tool or to create a systemd unit.
Testing NFS Access
Let’s test the access to the shares by creating a new file in each of them.
First, try to create a test file to the /backups directory using the touch command:
The /backup file system is exported as read-only, and as expected you will see a Permission denied error message:
Next, try to create a test file to the /srv/www directory as a root using the sudo command:
Again, you will see Permission denied message.
The /var/www directory is owned by the apache user, and this share has root_squash option set, which maps the root user to the nobody user and nogroup group that doesn’t have write permissions to the remote share.
Assuming that a user apache exists on the client machine with the same UID and GID as on the remote server (which should be the case if, for example, you installed apache on both machines), you can test to create a file as user apache with:
The command will show no output, which means the file was successfully created.
To verify it list the files in the /srv/www directory:
The output should show the newly created file:
Unmounting NFS File System
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 /backup share you would run:
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.
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.