How to Setup NFS Server and Mount NFS Share in Ubuntu

If you store all your files, videos and data on one computer, and you wanted to access the files on another computer, setting up a NFS server is one of the easiest way to share files over the network.

What is NFS?

NFS stands for Network File System and it is a distributed file system protocol that allow a client computer to access files on the host computer over the network.

Here is the guide to setup NFS server and mount NFS share on Ubuntu. Even though this tutorial is for Ubuntu, the instruction is applicable for most Linux distros too.

For this guide, you will need to have access to two (or more) Ubuntu computers and sudo permission to install software. Both the computers have to be connected to the internet, and preferably on the same local network. One of the computer will be used as the host server, while the other as a client.

Setup NFS Server on Host

Before we start, open a terminal and update the system:

sudo apt update && sudo apt -y upgrade

After the update/upgrade, install the NFS server:

sudo apt install nfs-kernel-server

Next, we are going to create a folder to be shared over the network. This can be a folder in the user or root directory.

sudo mkdir -p /mnt/NFSHostFolder

Remove the folder permission so the clients can access and make changes to it:

sudo chown nobody:nogroup /mnt/NFSHostFolder
sudo chmod 777 /mnt/NFSHostFolder

For the client to be able to access this NFS server, we need to specify the client’s IP address in the “exports” file.

Open the “exports” file:

sudo nano /etc/exports

and add this line to the end of the file

/mnt/NFSHostFolder clientIP(rw,sync,no_subtree_check)

Replace “clientIP” with the actual IP address of the client computer. Press Ctrl + O to save and Ctrl + X to exit. You can add multiple lines if you have multiple clients to connect to.

Refresh the export:

sudo exportfs -a
sudo systemctl restart nfs-kernel-server

Add Firewall to allow access to client

If you have a firewall setup for your host computer, now is the time to open the NFS port for access from the client’s IP address:

sudo ufw allow from clientIP to any port nfs

Change the “clientIP” to the actual client’s IP address.

Mount NFS Shares in Client

In the client Ubuntu computer, first update/upgrade the computer and install the NFS files:

sudo apt update && sudo apt -y upgrade
sudo apt install nfs-common

Next, create a new folder to mount the NFS share. This folder can be in the user or root directory:

mkdir -p /mnt/NFSClientFolder

Lastly, mount the NFS share:

sudo mount serverIP:/mnt/NFSHostFolder /mnt/NFSClientFolder

You might also want to add the entries to “/etc/fstab” so it will automount when it boot up.

Open the “/etc/fstab” file:

sudo nano /etc/fstab

Add the following line to the end of the file:

serverIP:/mnt/NFSHostFolder    /mnt/NFSClientFolder  nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0

Replace “serverIP” with the actual IP address of the host computer. Press Ctrl + O to save and Ctrl + X to exit.

That’s it.