How to Install NFS on Ubuntu

January 9, 2025

Introduction

Network File System (NFS) is a file system that allows local access to remote files from multiple locations within a network. For this access, NFS utilizes the standard client/server architecture, which supports sharing between Linux machines, regardless of their distribution.

In this tutorial, you will learn how to install and configure the NFS server and clients on Ubuntu.

How to install the NFS server on Ubuntu - a tutorial.

Prerequisites

  • Two or more Ubuntu machines (in this tutorial, we use Ubuntu 24.04).
  • Access to the command line/terminal.
  • Sudo privileges on all machines.

How to Install NFS Server

This section covers the NFS installation and setup on Ubuntu and other Debian-based distributions. Other Linux distributions, such as Fedora and CentOS or Rocky Linux, feature slightly different command syntax since they use a different package manager. However, the process follows the same pattern.

Follow the steps below to install the NFS server.

Step 1: Install NFS Kernel Server

To start setting up NFS, install the NFS kernel server on the host machine. Follow the steps below:

1. Refresh the package repository index to make sure you get the latest available version:

sudo apt update

2. Then, install the NFS kernel server with the following command:

sudo apt install nfs-kernel-server -y
Installing NFS Kernel Server on Ubuntu.

Wait for the installation to complete.

Step 2: Configure Shared Directory

After the installation completes, you need to create and configure a shared directory on the host machine:

1. Create a directory you want to share with the client system:

sudo mkdir -p /mnt/nfsdir

2. Change the owner user and group to nobody and nogroup. This setting makes the folder public:

sudo chown nobody:nogroup /mnt/nfsdir

3. Set permissions to 777, so everyone can read, write, and execute files in this folder:

sudo chmod 777 /mnt/nfsdir

Step 3: Edit NFS Export File to Grant Server Access to Clients

Permission to access the host server machine is granted in the exports file located in the /etc directory. Follow the steps below to edit the file and grant access to the clients:

1. Open the file with a text editor of your choice. This tutorial uses nano:

sudo nano /etc/exports

2. For each client you want to grant access to, add this line to the file:

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

Replace clientIP with the actual IP address of the client you want to grant access to.

Editing the /etc/exports file with nano to grant access to clients.

3. If you need to add more clients within the same subnet, type:

/mnt/nfsdir subnetIP/24(rw,sync,no_subtree_check)

The options in the brackets have the following functions:

  • rw provides clients with read and write access to directories on the server.
  • sync forces NFS to write changes before responding to the client. This option ensures the state of the host is accurately presented to clients.
  • no_subtree_check disables subtree checking. The subtree process may cause problems when users rename files.

4. Exit the file and save the changes.

Step 4: Export Shared Directory

After you make the necessary edits in /etc/exports, use the exportfs command to export all shared directories you registered in that file:

sudo exportfs -a

Next, restart the NFS Kernel Server to apply the changes to the configuration:

sudo systemctl restart nfs-kernel-server

Step 5: Allow Clients Through Firewall

If you use UFW, you need to allow clients to access the server:

sudo ufw allow from [clientIP or clientSubnetIP] to any port nfs

The output confirms the new rule addition:

Allowing clients to access the NFS server in UFW firewall.

To make sure you successfully completed the operation, type:

sudo ufw status

Search the output for the IP address you added:

Checking UFW firewall status to confirm the clients can access NFS.

Note: Learn about NFS Docker Volumes and see how to create and use them.

How to Install NFS Client

Perform the following steps on all the machines you wish to set up as clients for sharing.

Step 1: Install NFS Common

Install the NFS common package on every client machine you want to use. Follow the steps below:

1. Update the package index to ensure you install the latest version:

sudo apt update

2. Install NFS with:

sudo apt install nfs-common -y
Install NFS with sudo apt install nfs-common -y

Step 2: Set up a Mount Point

The client machine needs a mount point for the shared directory exported by the server.

Create a directory by running:

sudo mkdir -p /mnt/nfsdir_client

Step 3: Mount Shared Directory

To mount the shared directory on the mount point, use the following syntax:

sudo mount host_IP:/mnt/nfsdir /mnt/nfsdir_client

Replace host_IP with the actual IP address of the host machine. For example:

Mounting the shared directory on the mount point.

Check if you mounted the folder successfully with:

df -h
Checking if the NFS shared folder was mounted successfully.

When you do not need the shared folder any more, unmount it by running:

sudo umount /mnt/nfsdir_client

Note: The correct command is umount, not "unmount."

Step 4: Mount NFS Shared Directories on OS Boot

If you want the folders to stay mounted even after you restart the machine, add them to the /etc/fstab file. Follow the steps below:

1. Edit the /etc/fstab file with:

sudo nano /etc/fstab

2. Copy the following line to the bottom of the file and replace host_IP with the actual IP address of the host:

host_IP:/mnt/nfsdir /mnt/nfsdir_client nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
Editing the /etc/fstab file to mount the NFS directory on boot.

Add this line for every directory you need. This way, all the directories on the list will be mounted automatically on every boot.

Step 5: Test the Connection

Create or copy a file in the export folder of the NFS host server. After you create the file, open the mount directory on the client machine, and you should be able to access the same file in that directory.

If you need a more powerful NFS solution with API-driven dedicated servers, check out the video below to learn how to deploy and manage the Bare Metal Cloud network file storage:

Conclusion

NFS is often the best solution for remote access to data. It is easy to set up and performs well, especially in scenarios that involve smaller networks. After reading this article, you should be able to set up an NFS network on your Ubuntu machines, both on the server and the client side.

Next, see how to connect to a remote server via SSH or learn about the essential server security practices.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
Introduction to the Linux File System
October 24, 2024

A file system is a set of processes that controls how, where and when data is stored and retrieved from a...
Read more
Vim Commands Cheat Sheet
July 22, 2020

Vim is an open-source text editor found in most Linux distributions. This article lists all the commonly used...
Read more
How to Set Up a Firewall with UFW on Ubuntu 18.04
September 12, 2024

UFW is a simple interface implemented on top of iptables that provides an easier way to configure a firewall...
Read more
How to Use SSH to Connect to a Remote Server in Linux or Windows
October 24, 2024

In this tutorial, Find out How To Use SSH to Connect to a Remote Server in Linux or Windows. Get started with...
Read more