Installing and Configuring NFS Server on Ubuntu 22.04

Installing and Configuring NFS Server on Ubuntu 22.04

Network File System (NFS) allows a system to share directories and files with others over a network. With NFS, users and programs can access files on remote systems as if they were on the local hard disk. This is useful for sharing resources such as documents, media files, and configurations across multiple machines.

Step 1: Update the System

Ensure all packages are up-to-date:

sudo apt update

Step 2: Install NFS Server

Install the nfs-kernel-server package to provide NFS functionality:

sudo apt install nfs-kernel-server -y

Step 3: Create a Directory for Sharing

Create a directory that you want to share over the network.

sudo mkdir -p /mnt/nfs_share

Set the appropriate permissions for the shared directory. For example, to allow read/write access to everyone:

sudo chown nobody:nogroup /mnt/nfs_share
sudo chmod 777 /mnt/nfs_share

Step 4: Configure NFS Exports

Edit the /etc/exports file to define the directories that you want to share and specify which clients can access them:

sudo nano /etc/exports

Contents of /etc/exports should look like this:

/mnt/nfs_share 192.168.13.0/24(rw,sync,no_subtree_check)

Here’s what the options mean:

  • rw: Allow read and write access.

  • sync: Ensure changes are written to disk before the client is informed.

  • no_subtree_check: Improves performance by disabling subtree checking.

Step 5: Apply the NFS Export Configuration

Apply the new export configuration by running:

sudo exportfs -ar
sudo exportfs -v

Step 6: Start and Enable NFS Server

Start the NFS server and enable it to run at boot:

sudo systemctl start nfs-kernel-server
sudo systemctl enable nfs-kernel-server

sudo systemctl status nfs-kernel-server

Check if the NFS export configuration is correct by running:

sudo exportfs -v

Step 7: Adjust Firewall

If you are using ufw (Uncomplicated Firewall), allow NFS traffic:

sudo ufw allow from 192.168.13.0/24 to any port nfs

Step 8: Mount NFS Share on Client

On a client machine, install the NFS client tools:

sudo apt install nfs-common -y

Create a directory to mount the NFS share:

sudo mkdir -p /mnt/nfs_client

Mount the NFS share from the server (replace nfs-server-ip with the IP address of your NFS server):

sudo mount nfs-server-ip:/mnt/nfs_share /mnt/nfs_client

To check the mount:

df -h

To make the mount persistent across reboots, add the following line to the client’s /etc/fstab:

sudo nano /etc/fstab

Add this line:

nfs-server-ip:/mnt/nfs_share /mnt/nfs_client nfs defaults 0 0

Step 9: Verify the NFS Share

On the NFS server, create some folders:

mkdir folder1
mkdir folder2
mkdir folder3

Now check the mounted directory on the client machine to verify that the NFS share is working:

ls -l /mnt/nfs_client

You should see output like this:

All folders should be visible, indicating that the NFS share is functioning correctly.