Install and configure NFS server on Ubuntu 20.04 / 18.04 and Debian 10/9

Network File System (NFS) is a distributed file system protocol that enables client computers to access remote storage and retrieve data and files across a shared network in the same way as local access. This guide will explain how to install and configure an NFS server on Ubuntu 20.04 / 18.04 and Debian 10/9 Linux servers.

One thing to note is that NFS is the client and server file system (FS). On a network computer where NFS storage is to be installed, a client application is required to access its directories and files.

Configure NFS server Debian / Ubuntu

By using NFS shared storage, system administrators can consolidate resources onto centralized servers on the network. The client system can use (Read, write) Permission and no access to the underlying block storage.

Supported NFS versions

Here are the supported NFS versions for Ubuntu 20.04 / 18.04 and Debian 10/9:

NFS version 3 (NFSv3)

  • Supports secure asynchronous writes and is more powerful than previous NFSv2 in terms of error handling
  • Supports 64-bit file sizes and offsets, allowing clients to access file data in excess of 2 GB.

NFS version 4 (NFSv4)

  • Works through firewalls and the Internet
  • No longer need rpcbind service
  • Support for access control lists (ACLs)
  • Use status operations.

In this guide, we will follow these steps to set up NFSv on Ubuntu 20.04 / 18.04 and Debian 10/9 systems. We will do a separate guide for NFS client configuration.

Step 1: Update the server and set the host name

Your server should have a static IP address and a static host name that can be restarted.

sudo apt update && sudo apt upgrade
sudo hostnamectl set-hostname nfs-server.example.com --static

Step 2: Install NFS server on Ubuntu 20.04 / 18.04 and Debian 10/9

The next step is to install the NFS server package on Ubuntu 20.04 / 18.04 and Debian 10/9 systems.

sudo apt -y install nfs-kernel-server

After installation, start and enable the nfs-server service.

sudo systemctl enable --now nfs-server

Status should read “Run“.

Install and configure NFS server on Ubuntu 20.04 / 18.04 and Debian 10/9

Step 3: Export NFS Share on Ubuntu 20.04 / 18.04 and Debian 10/9

There are two ways to configure exports on the NFS server.

  1. Edit manually /etc/exports Configuration file
  2. To use exportfs Utilities on the command line

of /etc/exports Files control which file systems are exported to remote hosts and specify options. It follows the following syntax rules:

  • Blank lines are ignored.
  • To add a comment, use the pound sign (#).
  • You can use a backslash ().
  • Each exported file system should be on its own separate line.
  • Any authorized host list placed after the exported file system must be separated by a space character.
  • Option
    For each host, you must
    Host identifier without any spaces between the host and the first host
    parenthesis.

Prepare NFS storage path

For this setup, I added a secondary disk to the server with a capacity of 20 GB/ dev / sdb. We will partition this disk and create a file system on it for use as an NFS share.

$ lsblk  | grep sdb
sdb             8:16   0   20G  0 disk 

# Create partition and file system

sudo parted -s -a optimal -- /dev/sdb mklabel gpt
sudo parted -s -a optimal -- /dev/sdb mkpart primary 0% 100%
sudo parted -s -- /dev/sdb align-check optimal 1
sudo mkfs.xfs /dev/sdb1

We install it to /data table of Contents.

sudo mkdir /data
echo "/dev/sdb1 /data xfs defaults 0 0" | sudo tee -a /etc/fstab
sudo mount -a

Let’s check the settings to confirm.

$ df -hT | grep /data
/dev/sdb1             xfs        20G  176M   20G   1% /data

I will create the directory at / data / nfshare Will be exported to the NFS client.

sudo mkdir  /data/nfshare

Configure NFS export on Ubuntu / Debian server

Now we need to modify / etc / exports to configure the NFS share. The structure is:

export host(options)

You can also specify multiple hosts, as well as specific options for each host, as shown below.

export host1(options1) host2(options2) host3(options3)

where:

  • Export Is the directory to export
  • host Host or network to which the share is exported
  • Option List of options used by the host

In my setup, I will give read and write permissions to the exported file system to allow remote hosts to Variety Data shared on the file system. My host will be a network 172.20.100.0/24.

So a line on my / etc / exports file would be.

/data/nfshare           172.20.100.0/24(rw,no_root_squash)

If you do not want to restrict access to specific networks, use the asterisk ( *). If you have multiple subnets that should access storage in the local network, this method is less secure, but the configuration is more reasonable.

/data/nfshare    *(rw,no_root_squash)

The no_root_squash option disables root compression-giving remote root users root privileges. This is usually required for VM mounts on NFS shares.

To learn more about the options available, use:

$ man exports

Once set up, use exportfs The utility selectively exports directories without restarting the NFS service.

$ sudo exportfs -rav
exporting 172.20.100.0/24:/data/nfshare
  • [R[R -Export all directories listed in / etc / exports by constructing a new export list in / etc / lib / nfs / xtab
  • A kind All directories are exported or not, depending on other options passed to exportfs
  • v – Detailed operation – show what is happening

You can also restart the NFS server instead of using exportfs command.

sudo systemctl restart nfs-server 

Step 4: Mount the NFS share on the client computer

Now that we have completed the NFS server configuration, the rest is to mount the NFS share on the client system. The client can be a remote system, a virtual machine on the same server, or the server itself.

How to configure NFS client on Ubuntu 20.04 / 18.04 and Debian 10/9

More storage-related guidelines:

Install and configure NFS server on RHEL 8 / CentOS 8

How to configure NFS client on CentOS / RHEL 8

Sidebar