How to install & configure NFS Server on Ubuntu 20.04

Share this article

The network file system (NFS) is a distributed file system protocol and developed by Sun Microsystems in 1984. As the name implies, it is a filesystem that allows files access over the network. NFS provides the following benefits to the users

  • It is a cross-platform file or directory sharing protocol.
  • The NFS enables Linux machines to mount the filesystem from the NFS server into its own filesystem. Gives the impression to users they are accessing files in their local system.
  • Provide central file management.

NFS server: It is physical server/storage or virtual server which offers storage to NFS clients.

NFS client: This is user system, it make mount request to the NFS server.

Ubuntu 20.04 support NFSv3 and NFSv4. In this article, we will see how to install the default version which is NFSv4 in Ubuntu.

Our Setup

  • Virtual machine: Ubuntu 20.04
  • IP address: 192.168.122.227
  • Storage for NFS: 50 G
  • Need sudo rights to install the packages.

NFS server Setup

Install the NFS server

To set up the NFS server update the package so that we can install the latest packages and we will install nfs-kernel-server package.

sudo apt update
sudo apt install nfs-kernel-server

Check the status of nfs-server service

Once you have installed the nfs-kernel-server package. nfs-server service is started automatically. You can use the following command to check the status of nfs-server service.

sudo service  nfs-server status

Check the status of nfs-server service if it is running properly or not. If you face any issues hosting nfs-server status command will list the error but for more information go to the log file.

Below mention snapshot shows the status of the nfs-server after installation.

Also, to check the nfs status use the rpcinfo command shown in the following code section. Here output show that it is compatible with version 3 and 4. For more information when you visit /etc/nfsmount.conf file you can see the default version is 4.

root@server-foreman:~# rpcinfo -p | grep nfs
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100003    3   udp   2049  nfs
root@server-foreman:~# 

Configuration Files

  • /etc/default/nfs-common: provide configuration options to force ports.
  • /etc/default/nfs-kernel-server : This file is responsible for main configuration file for the NFS daemons and other utilities.
  • /etc/exports: lists the directories that are shared over the network with client. Basically these are the NFS share and information about their clients.

Create file system to share/export

In this section, we will create the folder which we want to share with the client and do the necessary configuration for the exporting.

Create a directory to share

sudo mkdir -p  /mnt/export/demo
sudo chown -R nobody: /mnt/export/demo
sudo chmod -R 777  /mnt/export/demo

NFS client entries in /etc/exports

We have to tell the NFS server which client will be mounted to the above-created directory. In /etc/exports file add the below line. It will allow NFS client with 192.168.122.1 IP address to mount /mnt/export/demo folder with the parameters mentioned in brackets.

/mnt/export/demo    192.168.122.1(rw,sync,no_all_squash,root_squash)

Export parameters:

  • rw: To give read and write access
  • sync: All I/O to the filesystem should be done synchronously.
  • no_all_squash: This parameter map all the UIDs and GIDs from client machine to identical UIDs and GIDs on the NFS server.
  • root_squash: This option disables client root user to access mount file system as a root.

At last, run the following command to export the above file system.

sudo exportfs -arv

The above command will basically export all the entries in /etc/exports and files in /etc/exports.d/ directory. Options with exports commands are for the following purposes:

  • -a: export or unexport all directories.
  • -r: Reexport all directories, synchronizing /var/lib/nfs/etab with /etc/exports and files under /etc/exports.d.
  • -v: Be verbose. When exporting or unexporting, show what’s going on.

For other options with exportfs refer to the manpage.

The above command will show the following output.

root@server-foreman:~# exportfs -arv
exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' specified for export "192.168.122.1:/mnt/".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exporting 192.168.122.1:/mnt/export/demo
root@server-foreman:~# 

Firewall configuration

Ubuntu 20.04 runs a firewall service. Therefore, you need to allow nfs traffic on the firewall. You can allow by running the following commands.

sudo ufw allow from <IP_address of the client> to any port nfs

An example of the above command is shown in the following code section. This rule is responsible for allowing traffic to 2049 port (nfs port) from all the IPs in 192.168.122.0/24 subnet. You can also mention single IP address also.

sudo ufw allow from 192.168.122.0/24 to any port nfs

See the screenshot of the firewall entries where you can see that the above command allowed traffic from 192.168.122.0/24 to the nfs port.

Setup NFS client

Refer to the following articles for a detailed explanation of how to install an NFS client:

Summary

In this guide, we installed and configured the NFS server on Ubuntu 20.04. If you face any issues and have any questions regarding the article, don’t hesitate to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *