How to install NFS Server on Ubuntu 20.04

Estimated reading: 6 minutes 143 views

1. NFS Introduction

NFS or Network File System is a distributed file system protocol that allows you to share directories over a network. With NFS, you can mount remote directories on your system and work with the files on the remote machine as if they were local files.
By default, the NFS protocol is not encrypted and does not provide user authentication. Access to the server is restricted by the client’s IP addresses or hostnames.
This article explains how to set up an NFSv4 Server on Ubuntu 20.04. We’ll also show you how to mount an NFS file system on the client machine.
Prerequisites
We’ll use two machines, one running Ubuntu 20.04, which will act as an NFS server, and another one running any other Linux distribution on which we will mount the share. The server and the clients should be able to communicate with each other over a private network. You can use 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:

NFS Server IP: 192.168.2.66
NFS Clients IPs: From the 192.168.2.0/24 range

2. Set Up the NFS Server

The first step is to set up the NFS server. We’ll install the necessary packages, create and export the NFS directories, and configure the firewall.

Step 1: Installing the NFS server

-The NFS server package provides user-space support needed to run the NFS kernel server. To install the package, run:

$ sudo apt update
$ sudo apt install nfs-kernel-server

-This installs additional packages such as keyutils, nfs-common, rpcbind, and other dependencies required for the NFS server to function as expected.
You can verify if the nfs-server service is running as shown

$ sudo systemctl status nfs-server

Create an NFS directory share

Step 2: Create an NFS directory share

-The next step will be to create an NFS directory share. This is the directory in which we will place files to be shared across the local area network. We will create it in the /mnt/ directory as shown below. Here, our NFS share directory is called /server_shares. Feel free to assign any name to your directory.

$ sudo mkdir /mnt/server_shares

-Since we want all the files accessible to all clients, we will assign the following directory ownership and permissions.

$ sudo chown nobody:nogroup /mnt/server_shares
$ sudo chmod -R 777 /mnt/server_shares

-These permissions are recursive and will apply to all the files and sub-directories that you will create.

Step 3: Grant the NFS Server access to clients

-After creating the NFS directory share and assigning the required permissions and ownership, we need to allow client systems access to the NFS server. We will achieve this by editing the /etc/exports file which was created during the installation of the nfs-kernel-server package.
So, open the /etc/exports file.

$ sudo vi /etc/exports

-To allow access to a single client, add the line below and replace the client-IP parameter with the client’s actual IP.

/mnt/server_shares client-IP(rw,sync,no_subtree_check)

-To add more clients to the list, simply specify more lines as shown:

/mnt/server_shares client-IP-1(rw,sync,no_subtree_check)
/mnt/server_shares client-IP-2(rw,sync,no_subtree_check)
/mnt/server_shares client-IP-3(rw,sync,no_subtree_check)

-Additionally, you can specify an entire subnet a shown

/mnt/server_shares 192.168.2.0/24 (rw,sync,no_subtree_check)

-This allows all clients in the 192.168.0.0 subnet access to the server. In our case, we will grant all clients access to the NFS server as shown

/mnt/server_shares 192.168.2.0/24(rw,sync,no_subtree_check)

-Let’s briefly brush through the permissions and what they stand for.

  • rw (Read and Write )
  • sync (Write changes to disk before applying them)
  • no_subtree_check (Avoid subtree checking )

Step 4 : Export the shared directory

-To export the directory and make it available, invoke the command:

$ sudo exportfs -a

Step 5: Configure the firewall rule for NFS Server >> if you have enable firewall you may execute below commands

-If you are behind a UFW firewall, you need to allow NFS traffic across the firewall using the syntax shown.

$ sudo ufw allow from [client-IP or client-Subnet-IP] to any port nfs

-In our case, the command will appear as follows:

$ sudo ufw allow from 192.168.2.0/24 to any port nfs

Verify the change:

$ sudo ufw status

-We are all good now with configuring the NFS Server. The next step is to configure the client and test if your configuration works. So, let’s proceed and configure the client.

Step 6: Configure the Client system

-Now log in to the client system and update the package index as shown.

$ sudo apt update

-Next, install the nfs-common package as shown.

$ sudo apt install nfs-common

-Then create a directory in the /mnt folder on which you will mount the NFS share from the server.

$ sudo mkdir -p /mnt/client_shared_folder

Finally, mount the remote NFS share directory to the client directory as follows.

$ sudo mount 192.168.2.66:/mnt/server_shares /mnt/client_shared_folder

Step 7: Testing the NFS Share setup

-To test if our configuration is working, we are going to create a test file in the NFS directory as shown

$ cd /mnt/server_shares
$ touch nfs_share.txt

-Now, let’s get back to our client and see if we can see the file in our mounted directory

$ ls /mnt/client_shared_folder/

-And voila! There goes our file as shown in the snippet below. This is confirmation that our setup was successful.
-That’s it for today. We hope this guide was beneficial to you and that you can comfortably share files using NFS on your network.

3. Unmounting NFS File System

-If the remote NFS share is no longer needed, you can unmount it as any other mounted file system using the umount command.
For example, to unmount the /backup share, you would run:

$ sudo umount /backups

-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

Good Luck !

Leave a Comment