LinuxTutorialsUbuntu

How To Install NFS Server on Ubuntu 20.04 LTS

Install NFS Server on Ubuntu 20.04

In this tutorial, we will show you how to install NFS Server on Ubuntu 20.04 LTS. For those of you who didn’t know, NFS or Network File System is a distributed file system protocol. It can help you share files and entire directories with others over a network. It allows programs on remote systems to share and access information with each other as if it were available on a local machine. This technology gives you the flexibility of sharing a resource over multiple systems.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the NFS Server on Ubuntu 20.04 (Focal Fossa). You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint.

We use two machines, one running Ubuntu 20.04, which will act as an NFS server, and another one running any other Linux distribution on which we will mount the share. The machines in this example have the following IPs:

NFS Server IP: 192.168.77.20
NFS Clients IPs: From the 192.168.77.0/24 range

Install NFS Server on Ubuntu 20.04 LTS Focal Fossa

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade

Step 2. Installing NFS Server on Ubuntu 20.04.

Now we run the following command to install the NFS kernel server package:

sudo apt install nfs-kernel-server

You can verify if the nfs-server service is running as shown:

sudo systemctl status nfs-server

Step 3. Creating the Shared Directories on the Host.

Creating a directory that will be shared among client systems:

sudo mkdir /var/nfs/general -p

Since we want the client systems to have full access to this shared directory, we need to remove all directory permissions that are restricting access:

sudo chown nobody:nogroup /var/nfs/general

Step 4. Configure NFS on the Host.

We’ll dive into the NFS configuration file to set up the sharing of these resources:

sudo nano /etc/exports

Add the following lines:

/var/nfs/general client_ip(rw,sync,no_subtree_check)
/home client_ip(rw,sync,no_root_squash,no_subtree_check)

For basic options of exports:

Option Description
rw Allow both read and write requests on an NFS volume.
ro Allow only read requests on an NFS volume.
sync Reply to requests only after the changes have been committed to stable storage. (Default)
async This option allows the NFS server to violate the NFS protocol and reply to requests before any changes made by that request have been committed to stable storage.
secure This option requires that requests originate on an Internet port less than IPPORT_RESERVED (1024). (Default)
insecure This option accepts all ports.
wdelay Delay committing a written request to a disc slightly if it suspects that another related write request may be in progress or may arrive soon. (Default)
no_wdelay This option has no effect if async is also set. The NFS server will normally delay committing a written request to a disc slightly if it suspects that another related write request may be in progress or may arrive soon. This allows multiple write requests to be committed to disc with the one operation which can improve performance. If an NFS server received mainly small unrelated requests, this behavior could actually reduce performance, so no_wdelay is available to turn it off.
subtree_check This option enables subtree checking. (Default)
no_subtree_check This option disables subtree checking, which has mild security implications but can improve reliability in some circumstances.
root_squash Map requests from uid/gid 0 to the anonymous uid/gid. Note that this does not apply to any other uids or gids that might be equally sensitive, such as user bin or group staff.
no_root_squash Turn off root squashing. This option is mainly useful for disk-less clients.
all_squash Map all uids and gids to the anonymous user. Useful for NFS exported public FTP directories, news spool directories, etc.
no_all_squash Turn off all squashing. (Default)
anonuid=UID These options explicitly set the uid and gid of the anonymous account. This option is primarily useful for PC/NFS clients, where you might want all requests to appear to be from one user. As an example, consider the export entry for /home/joe in the example section below, which maps all requests to uid 150.
anongid=GID Read above (anonuid=UID)

Finally, restart the NFS server to apply the changes:

sudo systemctl restart nfs-kernel-server

Step 5. Configuration Firewall.

You need to allow access through the firewall otherwise, accessing and mounting the shared directory will be impossible. To achieve this run the command below:

sudo ufw allow from 192.168.77.0/24 to any port nfs
sudo ufw enable
sudo ufw status

Step 6. Set Up the NFS Clients.

On the client machines, we need to install only the tools required to mount a remote NFS file system.

  • Install NFS client on Debian and Ubuntu

Run the following command to install it:

sudo apt install nfs-common
  • Create Mount Points on the Client.

Now we create two directories for mounts on the client:

sudo mkdir -p /nfs/general
sudo mkdir -p /nfs/home

Next, mount the shares using the Host’s IP address:

sudo mount host_ip:/var/nfs/general /nfs/general
sudo mount host_ip:/home /nfs/home

Verify that the remote file systems are mounted successfully using either the mount or df command:

df -h

To make the mounts permanent on reboot, open the /etc/fstab file and add the following lines:

sudo nano /etc/fstab

Add the following line:

192.168.77.10:/var/nfs/general /nfs/general  nfs defaults,timeo=900,retrans=5,_netdev 0 0
192.168.77.10:/home /nfs/home                nfs defaults,timeo=900,retrans=5,_netdev 0 0

Step 7. Test NFS Sharing.

Now we create a test file to the /var/nfs/general share:

sudo touch /nfs/general/general.test

Check its ownership:

$ ls -l /nfs/home/home.test
-rw-r--r-- 1 root root 0 Aug 1 13:32 /nfs/home/home.test

Step 8. Unmounting NFS File System.

If the remote NFS share is no longer needed, you can unmount it as any other mounted file system using the umount command:

sudo umount /general

Congratulations! You have successfully installed NFS Server. Thanks for using this tutorial for installing the NFS Server on Ubuntu 20.04 LTS Focal Fossa system. For additional help or useful information, we recommend you check the official NFS website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button