This guide will explain how you install and configure NFS server on Ubuntu 20.04/18.04 & Debian 10/9 Linux server. Network File System (NFS) is a distributed file system protocol which enables client machines to access a remote storage and retrieve data and files across a shared network in the same way they are accessed locally.
One thing to note is that NFS is a client-and-server file system(FS). A client application is required on the network machines where an NFS storage is to be mounted to access its directories and files.
By using NFS shared storage, system administrators can consolidate resources onto centralized servers on the network. A client system can access the remote share with (read, write) privileges and do not have access to the underlying block storage.
Supported NFS versions
Below are the versions of NFS supported by Ubuntu 20.04/18.04 & Debian 10/9:
NFS version 3 (NFSv3)
- Has support for safe asynchronous writes and is more robust at error handling than the previous NFSv2
- Supports 64-bit file sizes and offsets, allowing clients to access more than 2 GB of file data.
NFS version 4 (NFSv4)
- Works through firewalls and on the Internet
- No longer requires rpcbind service
- Supports Access Control Lists (ACLs)
- Utilizes stateful operations.
In this guide, we will setup NFSv on Ubuntu 20.04/18.04 & Debian 10/9 system by following the following few steps. We will do a separate guide on the configuration of NFS client.
Step 1: Update server and set hostname
Your server should have a static IP address and static hostname that persists reboots.
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 & Debian 10/9
Next is the installation of the NFS server packages on Ubuntu 20.04/18.04 & Debian 10/9 system.
sudo apt -y install nfs-kernel-server
After the installation, start and enable nfs-server service.
sudo systemctl enable --now nfs-server
Status should show “running“.
Step 3: Exporting NFS Shares on Ubuntu 20.04/18.04 & Debian 10/9
There are two ways to configure exports on an NFS server.
- Manually editing the
- Using the
exportfsutility on the command line
/etc/exports file controls which file systems are exported to remote hosts and specifies options. It follows the following syntax rules:
- Blank lines are ignored.
- To add a comment, start a line with the hash mark (#).
- You can wrap long lines with a backslash (\).
- Each exported file system should be on its own individual line.
- Any lists of authorized hosts placed after an exported file system must be separated by space characters.
- Options for each of the hosts must be placed in parentheses directly after the host identifier, without any spaces separating the host and the first parenthesis.
Preparing NFS Storage Path
For this setup, I added a secondary disk to my server with a capacity of 20 GB – /dev/sdb. We will partition this disk and create file system on it for use as 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’re going to mount it to /data directory.
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’ll create directory on /data/nfshare that will be exported to NFS clients.
sudo mkdir /data/nfshare
Configuring NFS exports on Ubuntu / Debian Server
Now we need to modify /etc/exports to configure NFS share. The structure is:
It is also possible to specify multiple hosts, along with specific options for each host, like below.
export host1(options1) host2(options2) host3(options3)
- export is the directory being exported
- host is the host or network to which the export is being shared
- options List of options to be used for the host
In my setup, I’ll give the exported file system is read & write permissions to allow remote hosts to make changes to the data shared on the file system. My host will be a network 172.20.100.0/24.
So my line on /etc/exports file will be.
If you don’t want to restrict access to specific network, use asterisk( *) instead. This is less secure but more sane configuration if you have multiple subnets which should access storage in your local network.
The no_root_squash option disables root squashing – enables remote root user to have root privileges. This is usually required for VM installations on NFS share.
To learn more about available options, use:
$ man exports
Once you’re done with the settings, use the exportfs utility to selectively export directories without restarting the NFS service.
$ sudo exportfs -rav exporting 172.20.100.0/24:/data/nfshare
- r – Causes all directories listed in /etc/exports to be exported by constructing a new export list in /etc/lib/nfs/xtab
- a – All directories are exported or unexported, depending on what other options are passed to exportfs
- v – Verbose operation – Show what’s going on
You can also restart NFS server instead of using exportfs command.
sudo systemctl restart nfs-server
Step 4: Mounting NFS Shares on Client Machines
Now that we’re done with NFS server configurations, the remaining part is mounting NFS shares on a client system. A client can be a remote system, a Virtual Machine on the same server or the server itself.
More storage related guides: