Linux

How to set up NFS Server on CentOS

How to set up NFS Server on CentOS

Why NFS? NFS is widely used for low-cost central file management systems. This avoids the need for additional software and disk space on individual user systems. This is easy to use, allowing users to instantly access files on a remote client the same way they access local files on a computer. Let’s set it on CentOS.

NFS or Network File System is a distributed file protocol that allows you to share files or even entire directories with others over the network. It builds a server-client environment, where client machines can access data shared by NFS servers as if they were installed locally.

In this tutorial, we will discuss detailed installation instructions for managing an NFS Server on CentOS. Step by step guide will cover how to set up an NFS server on the server side and also the client side. Let’s get started.

Setting up NFS Server on CentOS

Our Testing Environment
NFS Server IP address: 10.0.2.15 
NFS Client IP address: 10.0.2.16

To get started, here is a step-by-step guide on setting up an NFS server on your CentOS 8 system.

Step 1: Install NFS Server

First, we need to install nfs-utils on our system. This is an NFS server package that functions as an NFS daemon. To install it, you must enter the following command in the terminal:

$ sudo dnf install nfs-utils -y

1

Installing NFS server

We are running a clean installation of CentOS 8 and nfs-utils is already installed on our system. Probably the same on your machine too. If not, then the package will be installed, following which we proceed to the next step.

Now that you have installed nfs-utils on your system, you must start the nfs-server service, configure it to start automatically at system boot, and finally verify its status to check that everything is functioning as it should.

To do this, you must enter the following command in your terminal:

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

The output must be the same as, as shown in the figure below:

2

Checking NFS server Status

Active alerts mean that the NFS server is functioning properly. You should note that all related services needed to run an NFS server or install NFS shares are also activated via the above command. These services include nfsd, nfs-idmapd, rpcbind, rpc.mountd, locked, rpc.statd, rpc.rquotad, and rpc.idmapd.

Also, if you are interested, here are the addresses for the NFS server configuration file:

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

Step 2: Create and Export NFS File Systems

Now that you have set up your NFS server, it is time to create a shared file system on the server that will be accessible from the client system. For this tutorial, we will create the / mnt / nfs_share / docs directory using the following command:

$ sudo mkdir -p /mnt/nfs_share/docs

Next, to avoid file restrictions on the NFS shared directory, we will configure ownership of the directory using the following command:

$ sudo chown -R nobody: /mnt/nfs_share/docs

This allows the client system to create any file in the shared directory without facing permission issues. Also, if you want to activate all permissions – read, write, and run to the NFS shared folder, then you can do it using the following command:

$ sudo chmod -R 777 /mnt/nfs_share/docs

3

NFS shared folder

For the changes to occur, you must restart the NFS daemon using this command:

$ sudo systemctl restart nfs-utils.service

It’s time to export it so that the client system can have access to it. To do this, we need to edit the / etc / export file. When editing a file, you can give access to just one client, or several clients, and all subnets.

If you want to give access to all subnets, then you must use the following syntax:

/mnt/nfs_share/docs subnet (rw,sync,no_all_squash,root_squash)

Here “subnet” needs to be replaced by a system subnet. If not, you can specify multiple clients one at a time, on separate lines using this syntax:

/mnt/nfs_share/docs client_IP_1 (rw,sync,no_all_squash,root_squash)
/mnt/nfs_share/docs client_IP_1 (rw,sync,no_all_squash,root_squash)

The following is a look at the other parameters used in the command and what they mean:

  • rw – This parameter gives read and write permission to the NFS shared file / folder.
  • synchronization – This parameter requires that changes to the disk be made before applying it.
  • no_all_squash – This parameter will map all UIDs & GIDs of client requests that are identical to UIDS and GIDs on the NFS server.
  • root_squash – The parameter will map requests from the client-side root user to an anonymous UID / GID.

Using our settings, we will provide access to our client machines with IP 10.0.2.16. To do this, you must first open the / etc / export file using this command.

$ sudo vi /etc/exports

Next, you need to add the following lines in the file:

/mnt/nfs_share/docs 10.0.2.16(rw,sync,no_all_squash,root_squash)

4

Modify the / etc / export file

When finished, save and exit the file. Now, using the cat command, confirm that the entry has been registered. To do this, enter the following command in your terminal:

cat /etc/exports

You will see something similar to the following image:

5

Confirm changes

Now it’s time to export the folder created above, so that it is available for the client system. To do this, we need to use the following command:

$ sudo exportfs -arv

Here, the -a option means that all directories will be exported. The -r option will re-export all directories. And finally, the -v option will display verbose output.

However, just to make sure that the export list is okay, we will run this command as well:

$ sudo exportfs -s

And there he is! We have successfully created and exported files with our NFS. Now it’s time to move on to the next step.

Step 3: Set Firewall Rules for NFS Servers

Our NFS server setup is complete. The only thing left to do is configure the firewall rules in such a way as to enable NFS services.

This includes the following three services – nfs, rpc-bind, and mountd. And to allow this service through a firewall, we need to enter the following command in the terminal:

$ sudo firewall-cmd --permanent --add-service=nfs
$ sudo firewall-cmd --permanent --add-service=rpc-bind
$ sudo firewall-cmd --permanent --add-service=mountd

When finished, make sure to reload the firewall for the changes to take effect. To reload the firewall, use this command:

$ sudo firewall-cmd --reload

6

Configure CentOS firewall

With a firewall configured, it is time to set up the NFS client system.

How to Set Up an NFS Client System on CentOS 8

Following are the step-by-step procedures on how to set up an NFS client system on CentOS 8.

Step 1: Install the NFS Client Package

First, we need to install the packages needed to access the NFS share folder on the NFS server. To do this, enter the following command in the terminal:

$ sudo dnf install nfs-utils nfs4-acl-tools -y

Next, we will display the NFS shared folder installed on the server using this command:

$ showmount -e 10.0.2.15

7

NFS shared folder installed

Step 2: Install the remote NFS share on the server

We need to create a directory that we will use to mount NFS shares. To do this, use the following command:

$ sudo mkdir p /mnt/client_share

To install the remote shared NFS directory on the local client system, we need to use the following command.

$ sudo mount -t nfs 10.0.2.15 :/mnt/nfs_shares/docs /mnt/client_share

Where 10.0.2.15 is the IP address of the NFS server. To verify that remote NFS sharing has been installed, you can use the following command:

$ sudo mount | grep -i nfs

Finally, to make the mount share stable even after rebooting, we need to edit the / etc / fstab file and then add the following entry:

192.168.2.102:/mnt/nfs_shares/docs /mnt/client_share nfs defaults 0 0

When finished, save and close the file, and you are ready to do it.

Step 3: Test NFS Server & Client Settings

After going through all the steps above, we have successfully configured the client-server connection using NFS. However, we need to check if the settings work before we can call it a day.

To do this, we first need to create a test file in the NFS server share directory and then check to see if it is in the directory that the NFS client has installed. So first, let’s create a test file in the NFS server directory using the following command:

$ sudo touch /mnt/nfs_shares/docs/server_nfs_file.txt

Head to the client side and enter this command in the terminal to check whether a test file is available.

$ ls -l /mnt/client_share/

If you see the file, the NFS connection is functioning correctly.

Conclusion

That is our in-depth guide on how to set up an NFS server on CentOS 8. We hope this tutorial is useful and helps you share files and directories with your client system. You might be interested in setting up an FTP server on your CentOS machine.

Related posts

How to integrate the ONLYOFFICE editor with the Nuxeo Platform

Linux

Using the ‘ldd’ command on Linux

Linux

How to install Logstash Logfile Analytics Software on Ubuntu 20.04

Linux

Linux Pinky Command Tutorial for Beginners (8 Examples)

Linux

How to Install PostgreSQL on Ubuntu 20.04

Linux

How to Install the SNMP Cacti Monitoring Tool on Debian 10

Linux

How to Install Apache Maven on Ubuntu 20.04

Linux

How to Install Steam on Ubuntu 20.04

Linux

How to Change SFTP Port

Linux