How to setup NFS Server on Ubuntu 21.04

To setup NFS Server on Ubuntu 21.04

Introduction:

A network file system, developed by SUN Microsystems in 1990, allows you to share files and folders between Linux / UNIX systems. With NFS, the remote share appears locally.

Installation Procedure

Step 1: Check the OS version by using the following command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Step 2: Install NFS server by using the following command

root@linuxhelp:~# apt install nfs-kernel-server
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  keyutils libnfsidmap2 nfs-common rpcbind
Suggested packages:
  open-iscsi watchdog
The following NEW packages will be installed:
  keyutils libnfsidmap2 nfs-common nfs-kernel-server rpcbind
0 upgraded, 5 newly installed, 0 to remove and 162 not upgraded.
Need to get 423 kB of archives.
After this operation, 1,689 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu hirsute/main amd64 rpcbind amd64 1.2.5-9 [45.0 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu hirsute/main amd64 keyutils amd64 1.6.1-2ubuntu1 [45.7 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu hirsute/main amd64 libnfsidmap2 amd64 0.25-6 [28.0 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 nfs-common amd64 1:1.3.4-4ubuntu2.1 [205 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 nfs-kernel-server amd64 1:1.3.4-4ubuntu2.1 [99.3 kB]
Fetched 423 kB in 1s (777 kB/s)             

Creating config file /etc/default/nfs-kernel-server with new version
Processing triggers for man-db (2.9.4-2) ...
Processing triggers for libc-bin (2.33-0ubuntu5) ...

Step 3: Check the status of the NFS server by using the following command

root@linuxhelp:~# systemctl status nfs-kernel-server
● nfs-server.service - NFS server and services
     Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
     Active: active (exited) since Fri 2021-11-12 02:29:08 IST; 46s ago
    Process: 3063 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
    Process: 3064 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
   Main PID: 3064 (code=exited, status=0/SUCCESS)

Nov 12 02:29:07 linuxhelp systemd[1]: Starting NFS server and services...
Nov 12 02:29:08 linuxhelp systemd[1]: Finished NFS server and services.

Step 4: Enable the NFS server to start on Boot by using the following command

root@linuxhelp:~# systemctl enable nfs-kernel-server
Synchronizing state of nfs-kernel-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nfs-kernel-server

Step 5: Make a directory to NFS share

root@linuxhelp:~# mkdir /nfsshare

Step 6: Give a permissions to the share Directory to access by the client

root@linuxhelp:~# chmod 777 -R /nfsshare

Step 7: Configure the client for sharing the directory

root@linuxhelp:~# vi /etc/exports

# /etc/exports: the access control list for filesystems which may be exported
#               to NFS clients.  See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
/nfsshare 192.168.6.116(rw,sync,no_root_squash,no_subtree_check)
# Example for NFSv4:
# /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)

Step 8: Restart the NFS server by using the following command

root@linuxhelp:~# systemctl restart nfs-kernel-server

Step 9: Installing NFS client package in client machine

root@linuxhelp2:~# apt install nfs-common
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  keyutils libnfsidmap2 libtirpc-common libtirpc3 rpcbind
Suggested packages:
  open-iscsi watchdog
The following NEW packages will be installed:
  keyutils libnfsidmap2 libtirpc-common libtirpc3 nfs-common rpcbind
0 upgraded, 6 newly installed, 0 to remove and 498 not upgraded.
Need to get 404 kB of archives.
After this operation, 1,517 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us.archive.ubuntu.com/ubuntu focal/main amd64 libtirpc-common all 1.2.5-1 [7,632 B]
Get:2 http://us.archive.ubuntu.com/ubuntu focal/main amd64 libtirpc3 amd64 1.2.5-1 [77.2 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu focal/main amd64 rpcbind amd64 1.2.5-8 [42.8 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu focal/main amd64 keyutils amd64 1.6-6ubuntu1 [45.0 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu focal/main amd64 libnfsidmap2 amd64 0.25-5.1ubuntu1 [27.9 kB]
Get:6 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 nfs-common amd64 1:1.3.4-2.5ubuntu3.4 [204 kB]
Fetched 404 kB in 4s (113 kB/s) 
Selecting previously unselected package libtirpc-common.
(Reading database ... 143265 files and directories currently installed.)
Preparing to unpack .../0-libtirpc-common_1.2.5-1_all.deb ...
Unpacking libtirpc-common (1.2.5-1) ...
Selecting previously unselected package libtirpc3:amd64.
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.targ
et.
Created symlink /etc/systemd/system/remote-fs.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.targe
t.
nfs-utils.service is a disabled or a static unit, not starting it.
Processing triggers for systemd (245.4-4ubuntu3.2) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9) ...

Step 10: Make directory in client system to mount the remote directory

root@linuxhelp2:~# mkdir /nfs

Step 11: Mount the remote directory to the local directory

root@linuxhelp2:~# mount 192.168.6.115:/nfsshare /nfs

Step 12: Create the file on the mounted directory

root@linuxhelp2:~# touch /nfs/file

Step 13: Check the file created by client on server machine

root@linuxhelp:~# ls /nfsshare
file

By this the NFS Server setup on Ubuntu 21.04 has been completed

FAQ
Q
How to check the mounted NFS share?
A
You can check the mounted NFS share by using the command "df -hT".
Q
How to check the NFS shares available on the NFS server?
A
Available NFS shares on the NFS server can be checked by using this command "showmount -e "
Q
The purpose of giving "no_root_squash" to the NFS client in NFS share?
A
If no_root_squash is selected, then root on the client machine will have the same level of access to the files on the system as the root on the server.
Q
The purpose of giving "sync" to the NFS client in NFS share?
A
All changes to the according filesystem are immediately flushed to disk
Q
Where to Create an NFS share?
A
NFS shares are created in the /etc/exports.