CentOS

How to Install NFS Server and Client on CentOS 8

Whats is NFS?

NFS or Network File System is a distributed file system built by Sun Microsystems that allows you to share directory over the network.

NFS designed as a client-server architecture, allows you to share file systems (directory or disk) to multiple clients. You can mount directories or disk from the NFS server to the NFS client machines.

What we will do?

In this tutorial, you will learn how to install the NFS server and client using the CentOS 8

Prerequisites

For this tutorial, make sure you have got a CentOS 8 server with root privileges.

Below is the tutorial about the CentOS 8 server installation.

Install CentOS 8 Server

Before going any further, log in to your CentOS server and type the sudo command to get the root privileges on your system.

sudo su

Step 1 – Install NFS Server Package on CentOS 8

First, you will install the nfs-utils package to the CentOS system, it’s available by default on the CentOS BaseOS repository.

Install the nfs-utils package using the dnf command below.

dnf install nfs-utils

Once the installation is complete, start rpcbind and nfs-server service, and add bot services to the system boot.

systemctl enable --now rpcbind nfs-server

As a result, you’ve installed NFS packages for CentOS system. Also, the rpcbind and nfs-server services are up and running.

Step 2 – Create an NFS Share Directory

After installing NFS packages, you will create a new NFS shared directory, which will be shared with NFS clients.

Create a new NFS shared directory using the following command (for this example ‘/srv/backup’ and ‘/srv/shared2’ directories).

mkdir -p /srv/backup
mkdir -p /srv/shared2

After that, change the owner of shared directories to ‘nobody’.

chown -R nobody:nobody /srv/backup
chown -R nobody:nobody /srv/shared2

As a result, you’ve created new NFS shared directories (the ‘/srv/backup’ and ‘/srv/shared2’).

Create NFS Shared Directory

Step 3 – Export Shared Directory

o share the NFS shared directories or disk, you must edit the NFS exports configuration ‘/etc/exports’.

The ‘/etc/exports’ file is an NFS server export table. It contains the configuration of which directories and disks that will be accessible by NFS clients.

Edit the ‘/etc/exports’ file using vim editor.

vim /etc/exports

Change details about the directory, client IP address, and subnet with your own, then paste into it.

/srv/backup 10.5.5.0/24(rw,sync,no_subtree_check,crossmnt,fsid=0)
/srv/shared2 10.5.5.0/24(ro,sync,no_subtree_check) 10.5.5.3(rw,sync,no_subtree_check)
/srv/backup 10.5.5.25(rw,sync,no_subtree_check)

Save and close.

After that, apply the new changes of the NFS export file using the command below.

exportfs -ra

Then restart the rpcbind and nfs-server services.

systemctl restart rpcbind nfs-server

As a result, your NFS shared directories and disks are accessible to NFS clients.

Check the NFS shared directory using the following command.

exportfs -v

As can be seen, the ‘/srv/backup’ and ‘/srv/shared2’ directory is accessible from NFS clients, and you’ve successfully set up and NFS server.

Export NFS Shared Directories

Step 4 – NFS Server Behind Firewall

By default, the Firewalld service is enabled on the CentOS 8 system.

To make the NFS server accessible from NFS client machines, add some ports used by the NFS server to the Firewalld.

Run the following commands to add NFS ports to the firewalld.

firewall-cmd --permanent --add-port=111/tcp
firewall-cmd --permanent --add-port=54302/tcp
firewall-cmd --permanent --add-port=20048/tcp
firewall-cmd --permanent --add-port=2049/tcp
firewall-cmd --permanent --add-port=46666/tcp
firewall-cmd --permanent --add-port=42955/tcp
firewall-cmd --permanent --add-port=875/tcp

After that, reload the firewalld service.

firewall-cmd --reload

As a result, you’ve added some NFS ports to the Firewalld.

NFS Behind Firewall CentOS System

Step 5 – Set Up NFS Clients

In this step, you will set up an NFS client using the CentOS 8.

To set up NFS clients on the CentOS system, install the ‘nfs-utils’ package using the dnf command below.

dnf install nfs-utils

After that, create a new directory that will be used to mount the shared directory from the NFS server.

For this example, you will mount two different directories ‘/srv/backup’ and ‘/srv/shared2’ to the ‘/mnt’ on the client machine.

Create a new directory ‘backup’ and ‘shared2’ on the ‘/mnt’ directory using the command below.

mkdir -p /mnt/{backup,shared2}

Next, mount the NFS shared directory to the client machine using the following command.

mount -t nfs -o vers=3 10.5.5.15:/srv/backup /mnt/backup
mount -t nfs -o vers=3 10.5.5.15:/srv/shared2 /mnt/shared2

Now make sure there is no error, check the mounted NFS shared directories using the df command below.

df -h

As a result, you’ve mounted the ‘/srv/backup’ and ‘/srv/shared2’ directory to the NFS client machine.

Setup NFS Client CentOS

Step 6 – Setup NFS Auto-Mount on System Boot

In this step, you will set up auto-mount NFS shared directory on the system boot (startup boot).

You will set up auto-mount NFS shared directory through the ‘/etc/fstab’ configuration file.

Edit the ‘/etc/fstab’ file using vim editor.

vim /etc/fstab

Now change details with your info and paste the configuration into it.

10.5.5.15:/srv/backup /mnt/backup nfs rw,sync,hard,intr 0 0
10.5.5.15:/srv/shared2 /mnt/shared2 nfs rw,sync,hard,intr 0 0

Next, verify the fstab configuration using the mount command below, and make sure there is no error.

mount -a

As a result, you’ve successfully set up auto-mount NFS using the ‘/etc/fstab’ config file.

Setup Auto-Mount NFS Server

Step 7 – Testing Write

To test the NFS server and client installation, you will make sure that you’re able to write to the NFS shared directories from your NFS client machine.

Now back to the NFS client machine, go to the ‘/mnt/backup’ directory and create a new file ‘tetst.txt’ on it.

cd /mnt/backup
touch test.txt
echo "from client" > test.txt

Write Test fron Client Machine

Next, back again to the NFS server and check the ‘test.txt’ file on the ‘/srv/backup’ directory.

ls -lah /srv/backup
cat /srv/backup/test.txt

As a result, your NFS server and client installation are correct, and you’re able to read and write on the NFS shared directory.

Write Test fron Client Machine

And finally, you’ve successfully installed the NFS Server and Client on CentOS 8 server.

Write A Comment