The concept of NFS (Network File System) empowers a user on a client computer to have read and write privileges on shared file directories on a server computer.
This over-the-network file-sharing concept is implementable by anyone due to its open-source attribute. Before this article guide teaches us how to implement the NFS protocol, we first need to understand some of the NFS footprints.
Since it is the server computer that hosts the shareable files, it is also the one that decides the implementable depth of file-sharing permissions and access rights.
This article guide assumes that you have access to an Ubuntu 20.04/22.04 server and a client computer. It also assumes that you have root or Sudo user access on both operating system environments.
Installing NFS Server on Ubuntu Server
The first step is to always ensure that your operating system environment is up-to-date.
$ sudo apt update
We now need to proceed and install the nfs-kernel-server package.
$ sudo apt install nfs-kernel-server
We now need an NFS export directory created. This directory will be responsible for hosting the shareable files that will be accessible from client computers that have access.
$ sudo mkdir -p /mnt/nfs_share
With this directory now existing, it needs to be associated with certain permissions so that an access attempt from a client computer is not prohibited.
$ sudo chown -R nobody:nogroup /mnt/nfs_share
We will also grant the client computers that have access to these shared files additional read, write and execute permission with the following command.
$ sudo chmod 777 /mnt/nfs_share
For users from client computers to have access to this created shareable directory, we have to make some changes inside the /etc/exports file.
$ sudo nano /etc/exports
This file was created during the installation of the NFS server.
It is in this file that we will set the access permission for single or multiple client computers that need a pathway to shareable directories and files.
On my end, the client’s computer is using the IP address 192.168.11.196. Therefore, adding the following line to the above file will grant the client computer associated with the IP address access to shared files.
If I want multiple client computers with a similar subnet to have access, the line to include in the /etc/exports file will look like the following:
Finally, we need to export the /mnt/nfs_share directory for it to be accessible to targeted client computers.
$ sudo exportfs -a
Also, remember to restart the nfs-kernel-server.
$ sudo systemctl restart nfs-kernel-server
If you have a firewall on your server, implement a similar command to the following to allow access.
$ sudo ufw allow from 192.168.11.0/24 to any port nfs
Configuring a Linux Client to Access NFS Share
To access NFS share from the client machine, you need to install the nfs-common package in the client machine, here I am using Ubuntu as the client machine.
$ sudo apt install nfs-common
We now need a mount point to link with the nfs_share directory on the server computer.
$ sudo mkdir -p /mnt/nfs_clientshare
To complete this linkup, you need to mount the NFS share directory on the client mount point using the following command:
$ sudo mount 192.168.11.130:/mnt/nfs_share /mnt/nfs_clientshare
The above IP address belongs to the NFS server computer.
NFS File Sharing/Access Verification
Go back to the nfs_share folder on the server computer and create some sample files:
$ cd /mnt/nfs_share $ touch file1.txt file2.txt
Go back to the nfs_clientshare folder on the client computer to see if the above-created files are visible.
$ ls -l /mnt/nfs_clientshare/
As you can see, the two created files exist with reading and writing access.
An NFS server provides a flexibly easy approach to sharing crucial/important files and directories from a single server to a specific or multiple client computers.