In this article, I am going to show you how to configure NFS server and clients on CentOS 8. So, let’s get started.
In this article, 3 CentOS 8 machines are used. They are connected as in figure 1.
nfs-server will be configured as an NFS file server.
nfs-client1 and nfs-client2 will be configured as NFS client. They will mount the shared filesystem path from the NFS server nfs-server.
nfs-server network configuration:
nfs-client1 network configuration:
nfs-client2 network configuration:
Configuring the Server:
First, you have to set up a static IP address on the nfs-server CentOS 8 machine. If you need any help on that, check the article Configuring Static IP on CentOS 8.
Now, SSH into your nfs-server machine.
Update the DNF package repository cache with the following command:
Install the nfs-utils package with the following command:
To confirm the installation, press Y and then press <Enter>.
nfs-utils package should be installed.
Now, add the nfs-server and rpcbind services to the system startup with the following command:
Now, start the nfs-server and rpcbind services with the following command:
The nfs-server and rpcbind services should be active (running).
Now, you can share any directory path on your server using NFS.
In this article, I am going to show you how to make partitions, format the partition, mount them to specific directory path and share it using NFS. If the directory path you want to share is ready, you can skip ahead.
First, find the storage device name using the following command:
In my case, the name of the SSD I will use is nvme0n2. It will be different for you. So, make sure to replace it with yours from now on.
Now, run cfdisk as follows:
If you don’t have a partition table already, cfdisk will show you this window. Select gpt and press <Enter>.
Now, select the Free space, navigate to [ New ] and press <Enter>.
Type in the partition size and press <Enter>.
NOTE: Use M for MiB, G for GiB and T for TiB disk size unit.
A new partition /dev/nvme0n2p1 should be created. Now, select [ Write ] and press <Enter>.
Now, type in yes and press <Enter>.
The changes should be written to the partition table.
Now, select [ Quit ] and press <Enter>.
As you can see, a new partition nvme0n2p1 is created.
Now, create a filesystem on the nvme0n2p1 partition with the following command:
Now, make a directory (in my case /nfs-share) where you want to mount the newly created partition with the following command:
Now, to automatically mount the partition when your nfs-server boots, you have to add an entry to the /etc/fstab file.
To edit the /etc/fstab file, run one of the following commands:
Now, add the following line to the file and save the file.
Now, you can easily mount the newly created partition to the /nfs-share directory as follows:
As you can see, the partition is mounted to the /nfs-share directory.
Now, to share the /nfs-share directory with NFS, edit the /etc/exports configuration file with one of the following commands:
Now, you have to add the following line to the /etc/exports file.
The format of the line is:
In this article, the share_directory_path is /nfs-share
After specifying the share directory, you can add one or more hosts and access options for each host.
Here, the host is 192.168.20.0/24. So, everyone on the subnet, 192.168.20.1 to 192.168.20.254 will be able to access this share.
The options are rw and no_root_squash.
rw – allows read and write to the share
no_root_squash – does not allow NFS server to map any user or group id to anonymous user or group id.
There are many more options which you can use. To learn more about it, check the manpage of exports.
Now, to enable the share without restarting the server, run the following command:
If you have SELinux enabled, run the following command:
Now, to allow access to the NFS ports from the NFS clients, configure the firewall with the following command:
Now, for the firewall changes to take effect, run the following command:
Configuring the Client:
Now, to mount the NFS share /nfs-share from the nfs-server to nfs-client1 machine, you need to install the nfs-utils package on nfs-client1 machine as well.
First, update the DNF package repository cache as follows:
Now, install the nfs-utils package as follows:
Now, press Y and then press <Enter>.
nfs-utils should be installed.
Now, to confirm whether the NFS share is accessible from the client machine, run the following command:
Here, 192.168.20.178 is the IP address of nfs-server machine.
As you can see, /nfs-share is accessible from the nfs-client1 machine.
Now, make a mount point for the NFS share as follows:
Now, you can mount the NFS share /nfs-share from the nfs-server machine to the /mnt/nfs-share directory of the nfs-client1 machine with the following command:
The NFS share should be mounted.
If you want to mount the NFS share when your nfs-client1 machine boots, you have to add an entry to the /etc/fstab file.
Edit the /etc/fstab file with one of the following commands:
Now, add the following line to the file.
nfs has a lot of mount options. I’ve used the defaults mount option here. But, if you have specific requirements, you may check the manpage of nfs.
Now, let’s create a new file hello.txt to the NFS share from the nfs-client1 machine.
As you can see, the file hello.txt is also created in the nfs-server.
The contents of the hello.txt file read from the nfs-server machine.
The same way you can configure nfs-client2 and access the NFS share from there.
Install nfs-utils package on nfs-client2.
Edit /etc/fstab file.
Add the following line to it.
Create a mount point.
Mount the share.
Access the files from the share. Very simple.
This is how you configure NFS server and client on CentOS 8. Thanks for reading this article.