X

How to Install an NFS server on Ubuntu 22.04

NFS (Network File System) is a distributed file system protocol developed by Sun Microsystems to share files and folders between Linux/Unix systems. NFS allows us to mount the file system over the local network. NFS uses standard client/server architecture for sharing files and directories between Linux-based systems.

This tutorial will help you to set up an NFS server on Ubuntu 22.04 server.

Requirements

  • A server running Ubuntu 22.04.
  • A static IP address 192.168.0.100 is set up to your server.
  • A non-root user with sudo privileges.

Getting Started

First, update your system with the latest version by running the following command:

sudo apt update -y
sudo apt upgrade -y

Once your system is updated, restart your system to apply the changes.

Install NFS Server

By default, NFS is available in the Ubuntu default repository. You can install it by just running the following command:

sudo apt install nfs-kernel-server -y

Once the installation has been completed, you can check the NFS status with the following command:

sudo systemctl status nfs-kernel-server

Output:

? nfs-server.service - NFS server and services
   Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
   Active: active (exited) since Sat 2023-07-31 06:26:19 UTC; 31s ago
 Main PID: 13820 (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 1114)
   CGroup: /system.slice/nfs-server.service

Jul 31 06:26:19 ubuntu2204 systemd[1]: Starting NFS server and services...
Jul 31 06:26:19 ubuntu2204 systemd[1]: Started NFS server and services.

Configure NFS Server

Before starting, you will need to create a directory that you want to share using the NFS server. You can create it with the following command:

sudo mkdir /mnt/nfsshare

Next, change the directory ownership to nobody:nogroup for security reasons:

sudo chown -R nobody:nogroup /mnt/nfsshare

Next, you will need to configure NFS to export above created directory. You can do this by editing NFS default configuration file /etc/exports:

sudo nano /etc/exports

Add the following lines:

/mnt/nfsshare        192.168.0.0/24(rw,sync,no_subtree_check)

Save and close the file.

192.168.0.0 : Specify your network IP address range.

rw : This option grants read and write permissions on the directory.

no_subtree_check : Specifies that the host should not check the location of the files being accessed withing the host filesystem.

sync : This option forces NFS to write changes to disk before replying.

Next, restart NFS server to apply the changes:

sudo systemctl restart nfs-kernel-server

Install and Configure NFS Client

First, you will need to install NFS client to your system. You can install it by running the following command:

sudo apt install nfs-common -y

Next, create a mount point to mount the shared directory on client machine:

sudo mkdir ~/nfsshare

Next, mount the shared directory on the client machine with the following command :

sudo mount 192.168.0.100:/mnt/nfsshare ~/nfsshare

You can now verify the mounted directory with the following command:

sudo df -h

Output:

Filesystem                   Size  Used Avail Use% Mounted on
/dev/sda1                    138G   41G   90G  32% /
none                         4.0K     0  4.0K   0% /sys/fs/cgroup
udev                         1.9G  4.0K  1.9G   1% /dev
tmpfs                        384M  1.2M  383M   1% /run
none                         5.0M     0  5.0M   0% /run/lock
none                         1.9G   59M  1.9G   4% /run/shm
none                         100M   44K  100M   1% /run/user
/dev/sda5                    225G   35G  180G  16% /Data
192.168.0.100:/mnt/nfsshare  7.9G  5.1G  2.4G  68% /home/vyom/nfsshare

If you want to mount NFS shares automatically on every reboot. Then, you can do this by editing /etc/fstab file on the client machine:

sudo nano /etc/fstab

Add the following line:

192.168.0.100:/mnt/nfsshare/ /home/vyom/nfsshare nfs rw,sync,hard,intr 0 0

Save and close the file. Then, restart your client machine and check whether NFS share is automatically mounted or not.

Test NFS

NFS server and client are now working file. It's time to test access to the shares.

First, create some files and directories inside /home/vyom/nfsshare on the client machine:

sudo mkdir /home/vyom/nfsshare/testdir
sudo touch /home/vyom/nfsshare/testfile

Next, check the ownership of the newly created file and directory:

ls -l /home/vyom/nfsshare/

Output:

drwxr-xr-x 2 nobody nogroup 4096 Jul 31 2023 testdir
-rw-r--r-- 1 nobody nogroup    0 Jul 31 2023 testfile

Now, check the nfsshare directory on the Server machine:

sudo ls -l /mnt/nfsshare

You should see the file and directory which you have created on the Client machine in the following output:

drwxr-xr-x 2 nobody nogroup 4096 Jul 31 2023 testdir
-rw-r--r-- 1 nobody nogroup    0 Jul 31 2023 testfile
FAQForge:
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings