Install NFS Server on Ubuntu

November 10, 2020

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 standard client/server architecture, supporting 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.

Install NFS Server on Ubuntu

Prerequisites

  • Two or more Ubuntu machines
  • Access to the command line/terminal
  • Sudo privileges on all machines

Set Up the NFS Host Side

The steps of this tutorial cover the NFS installation and setup on Ubuntu and other Debian based distributions. Other Linux distributions, such as Fedora and CentOS/RHEL, feature slightly different command syntax. However, the process follows the same pattern.

Install NFS Kernel Server

Start setting up NFS by choosing a host machine.

Next, update the package repository:

sudo apt update

Then, install the NFS kernel server on the machine you chose with the following command:

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

Type “y” and press ENTER to start the installation.

Configure Shared Directory

On the host machine, create a directory you want to share with the client system. Choose any name you want.

sudo mkdir -p /mnt/nfsdir

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

sudo chown nobody:nogroup /mnt/nfsdir

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

sudo chmod 777 /mnt/nfsdir

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 /etc directory. Open the file with a text editor of your choice, this tutorial uses Vi:

sudo vi /etc/exports

For each client you want to grant access to, add this line to the file:
/mnt/nfsdir clientIP(rw,sync,no_subtree_check)

Editing the /etc/exports file in Vim editor

Exit the file and save the changes

Tip: 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” option 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.

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 configuration:

sudo systemctl restart nfs-kernel-server

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 addition of the IP address:

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 status of UFW firewall to confirm the addition of the exception

Setting up the NFS Client Side

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

Install NFS Common

To enable NFS on client machines, install the NFS common package:

sudo apt update
sudo apt install nfs-common
Installing NFS Common on client machines


Type “y” and press ENTER to start the installation.

Set up a Mount Point

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

Create a directory by typing:

sudo mkdir -p /mnt/nfsdir_client

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

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

Use the df -h command to check if you mounted the folder successfully:

Checking that the NFS shared folder was mounted successfully


When you do not need the shared folder anymore, unmount it by typing:

sudo umount /mnt/nfsdir

Note: The correct command is umount, not “unmount.”

Mount NFS Shared Directories on OS Boot

If you want the folders to stay mounted even after you restart the machine, you will need to add them to the /etc/fstab file.

To edit the /etc/fstab file, enter:

sudo vim /etc/fstab

Copy the following line to the bottom of the file, replacing “host_IP” with the actual IP address of the host:
host_IP:/mnt/nfsdir /mnt/nsfdir_client nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0

Editing the /etc/fstab file in Vim editor


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

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.

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 21, 2020

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
December 30, 2019

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
September 24, 2018

In this tutorial, Find out How To Use SSH to Connect to a Remote Server in Linux or Windows. Get started with...
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.