How-to

How to Install NFS Server and Client on Ubuntu 20.04

What 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 Ubuntu 20.04

Prerequisites

For this tutorial, make sure you have got a Ubuntu 20.04 server with root privileges.

Below is the tutorial about the Ubuntu 20.04 server installation.

Install Ubuntu 20.04 Server

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

sudo su

Step 1 – Install NFS Server Packages on Ubuntu 20.04

First, you will install the NFS server kernel package to the Ubuntu system.

Update your Ubuntu repository index and install the NFS kernel server using the apt command below.

apt update
apt -y install nfs-kernel-server

As a result, you’ve installed the NFS server kernel package to the Ubuntu system, and you’re ready to set up your NFS server.

Step 2 – Create an NFS Share Directory

After installing the NFS server kernel package, 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’ and ‘nogroup’.

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

As a result, you’ve created a new NFS shared directory.

Create NFS Shared directory

Step 3 – Export NFS Share Directory

To 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 172.16.0.0/12(rw,sync,no_subtree_check,crossmnt,fsid=0)
/srv/shared2 172.16.0.0/12(ro,sync,no_subtree_check) 172.16.0.6(rw,sync,no_subtree_check)
/srv/backup 172.16.0.6(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

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.

Configure NFS Server on Ubuntu 20.04

Step 4 – Set Up NFS Clients

In this step, you will set up an NFS client using the Ubuntu 20.04.

To set up NFS clients on the Ubuntu system, install the ‘nfs-common’ package using the apt command below.

sudo apt install nfs-common

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=4 172.16.0.5:/srv/backup /mnt/backup
mount -t nfs -o vers=4 172.16.0.5:/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 Ubuntu 20.04

Step 5 – 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).Set Up NFS Client

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.

172.16.0.5:/srv/backup /mnt/backups nfs defaults,timeo=900,retrans=5,_netdev 0 0
172.16.0.5:/srv/shared2 /mnt/shared2 nfs defaults,timeo=900,retrans=5,_netdev 0 0

Save and close.

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

Step 6 – Testing Write

To test the NFS server and client installation, you will make sure that you’re able to write to the mount directory from your NFS local 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

Insert new word to the ‘test.txt’ file.

echo "This is client Ubuntu 20.04" > test.txt

Testing Write

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

ls -lah /srv/backup

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

Testing Write

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

Write A Comment