NFS (Network File System) is basically developed for sharing of files and folders between Linux/Unix systems by Sun Microsystems in 1980.
It allows you to mount your local file systems over a network and remote hosts to interact with them as they are mounted locally on the same system.
It is operated in the client-server fashion where the system having the shared directory is known as a server while the system accessing the shared directory is known as a client.
The client can mount the share directory by NFS server to its own system which appears as a local disk partition.
A client can then read/write files on the remote NFS server based on the permissions assigned by the NFS server.
In this context, we shall look into how to install the NFS server and client, export shares, and mount/unmout the NFS share on client.
Features of NFS includes:
i. User can access remote files locally.
ii. It is not necessary for both machines to have the same operating system.
iii. It offers a centralized storage solution.
iv. User can store their data in a central location hence it allows saving local storage space.
Now you will learn how to install the NFS server and client on Linux.
Here, we will be using the two machines with the following details:
NFS host (Debian 10)
NFS client (Debian 10)
How to install and Configure NFS server ?
Here, we will first install the NFS server on our host machine.
Then we will setup two share directories that are /var/nfs-public and /var/nfs-docs for sharing to client machines.
Now, follow the steps provided below.
1. Install the NFS server on the host machine
For a system to setup as an NFS server, you will need to install nfs-kernel-server package on it. On the NFS host machine, first, update the local repository index using the below command in Terminal:
$ sudo apt update
Then install nfs-kernel-server using the below command:
$ sudo apt install nfs-kernel-server
Now you may be provided with the y/n option to continue the installation. Hit y to continue, after which the system will begin the installation of the nfs-kernel-server.
Once the nfs-kernel-server is installed, you can verify the installation using the below command in Terminal:
$ dpkg -l | grep nfs-kernel-server
2. Create a shared directory on the host
Now, create a directory on the host that you want to set for sharing with the clients.
We have created two directories /var/nfs–docs and /var/nfs-public with different configuration settings:
$ sudo mkdir -p /var/nfs-docs
$ sudo mkdir -p /var/nfs-public
For the /var/nfs-public directory, we will modify its ownership to nobody:nogroup using the below command:
$ sudo chown nobody:nogroup /var/nfs-public
To verify if the ownership has changed, use the below command:
$ ls -la /var/nfs-public
For the /var/nfs-docs, we will not change the ownership.
3. Export the shares
Now we will configure the NFS server export file for sharing of the resources.
The NFS export file is /etc/exports which can be edited as follows:
$ sudo nano /etc/exports
Now in this file, we will specify the directories that we want to share and the client that can access it.
You can add entries in the NFS export file using the below syntax:
directory host1(options) host2(options)...hostN(options)...
a. directory is the directory you want to share with the clients.
b. host is the IP address/subnet address of the client/subnet you want to share with. For finding the IP address of your client machine, you can visit our guide on How to Find Your IP address .
c. options are some of the configuration options that specify how the resources should be shared.
For both our directories /var/nfs-docs and /var/nfs-public, we have added the following entries in the /etc/exports file:
/var/nfs-docs 192.168.72.159(rw,sync, no_root_squash,no_subtree_check)
In the above configuration file, both entries tell the NFS server to share the /var/nfs-public and /var/nfs-docs directories with the NFS client 192.168.72.159 with rw (read and write) permission.
In the second entry, we have added the no_root_squash that allows the client to have root permissions on the NFS server as well. Once you are done with the export configurations, save and close the /etc/exports file.
Now in order to export the shares, you will have to restart the NFS server.
Here is the command to do so:
$ sudo systemctl restart nfs-kernel-server
Whenever you make any change in the /etc/exports file, make sure to restart the service to apply the configuration changes.
Alternatively, you can use this command to export the shares:
$ sudo exportfs -a
To verify if the NFS server is running without any issues check its status using the below command in Terminal:
$ sudo systemctl status nfs-kernel-server
4. Set up Firewall on the NFS server
If a firewall is enabled on the NFS server, you will need to configure it to allow clients to connect to NFS port 2049.
To verify if the firewall is enabled on the NFS server, issue the below command in Terminal:
$ sudo ufw status
An active status shows that the firewall is enabled.
Now to allow clients to NFS port 2049, you will have to add a rule in the firewall.
Run the below command in Terminal to do so:
$ sudo ufw allow from client_ip/subnet_ID to any port nfs
For instance, to allow whole subnet 192.168.72.0/24 access to NF port 2049, the command would be:
$ sudo ufw allow from 192.168.72.0/24 to any port nfs
Then to verify if the rule has been added successfully, check the status of firewall:
$ sudo ufw status
Now we have completed the configurations on the NFS Server. Let's move towards NFS client installation and configuration.
How to install and configure NFS Client ?
In this section, we will first install the NFS client on our client machine.
Then we will setup two mount points that are /media/public and /media/docs for mounting the remote NFS shares /var/nfs-public and /var/nfs-docs.
Follow the steps below.
1. Install NFS Client Package on the Client Machine
For a system to setup as an NFS client, you will need to install the nfs-common package on it.
On the NFS client machine, first, update the local repository index using the below command in Terminal:
$ sudo apt update
Then install nfs-common using the below command:
$ sudo apt install nfs-common
Now you may be provided with the y/n option to continue the installation.
Hit y to continue, after which the system will begin the installation of the nfs-common package.
2. Create Mount Points
On the client system, you will need to create a mount point to mount the shares located on the NFS server.
On our client, we have created the two directories at /media for our mount points:
$ sudo mkdir -p /media/docs
$ sudo mkdir -p /media/public
3. Mount NFS shares on the client manually
Now we will mount the NFS shares on the mount points we created in the previous step. Here is the syntax to do so:
$ sudo mount NFS_server_IP:NFS_share client_mountpoint
a. NFS_server_IP is the NFS server's IP address
b. NFS_share is the share located on the NFS server.
c. client_mountpoint is the mount point where you want to mount the share.
From our NFS server, we have exported two shared directories that were :/var/nfs-docs and /var/nfs-public.
On our client machine, we will mount one share /var/nfs-docs to one mount point /media/docs while the other share /var/nfs-public on the second mount point /media/public.
To enable us mount the NFS shares to the NFS client, we executed the following commands:
$ sudo mount 192.168.72.158:/var/nfs-docs /media/docs
$ sudo mount 192.168.72.158:/var/nfs-public /media/public
Now to verify if the NFS shared directories have been mounted successfully, issue the “df –h” command in Terminal:
$ df -h
Here is the output of "df -h" command will show the two NFS shares at the bottom.
As we have mounted the shares on the /media directory, you can also access the mounted directories from File Manager.
4. Mounting the NFS shares on client automatically at Boot
Each time you restart the client system, you will need to mount the NFS share manually using the mount command. This is okay for one time or occasional use.
However, if you want the NFS shares access all the time, you will need to make the mount permanent using the /etc/fstab file.
Edit the /etc/fstab file:
$ sudo nano /etc/fstab
Now add entries for your NFS share that you want to mount automatically:
192.168.72.158:/var/nfs-docs /media/docs nfs rw,sync,hard,intr 0 0
192.168.72.158:/nfs-public /media/public nfs rw,sync,hard,intr 0 0
Then save and close the /etc/fstab file.
Next time, when you reboot the system, the client will automatically mount the NFS shares.
How to test NFS Access ?
Now we will test the access to NFS shared directories by creating a new file in each directory.
In the client machine, first, create a test1 file as sudo in the local mount point /media/public:
$ sudo touch /media/public/test1
Now check the ownership of the new file:
$ ls -l /media/public/test1
You will see the ownership of the file is nobody user and nogroup group. Although, we created the file as sudo on the client machine, but the NFS server has translated it to the ownership of the host nobody:nogroup.
This means a root user on the client cannot perform the administrative task on the server's shared directory.
Now create a test1 file as sudo in the second local mount point /media/docs:
$ sudo touch /media/docs/test1
If we check the ownership of this file, you will see it is owned by the root user and root group.
This is because we have set the no_root_squash option which allows the root user on the client machine to act as a root on the server’s directory and can perform administrative tasks.
Now on the NFS server machine, check if you can view both files.
How to unmount the NFS Share from the Client machine ?
If you no longer need the NFS share, you can unmount it from the client system.
To unmount the NFS shares, type umount followed by the name of the directory where the NFS share is mounted:
$ umount /media/docs
$ umount /media/public