NFS was developed by Sun Microsystems for sharing files and directories between their systems. NFS allows the systems on a network to share files through the TCP/IP network. 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. Some of the features of NFS are as follows:
- User can access remote files locally
- It is not necessary for both machines to have the same operating system
- It offers a centralized storage solution
- User can store their data in a central location hence it allows saving local storage space.
In this post, we will be showing you how to install the NFS server and client, export shares, and mount/unmout the NFS share on client.
For the demonstration, we will be using the two machines with the following details:
NFS host (Debian 10)
- Hostname: nfs
- IP: 192.168.72.158
NFS client (Debian 10)
- Hostname: client
- IP: 192.168.72.159
Note: The steps mentioned here have been tested on Debian 10 (Buster system).
Installing and Configuring NFS server
In the following section, 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. Let’s get started.
Step 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
Step 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.
Step 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)...
- directory is the directory you want to share with the clients.
- 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 .
- 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-public 192.168.72.159(rw,sync,no_subtree_check) /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
Step 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
In the following output, you can see the active status which shows the firewall is enabled.
Now to allow clients to NFS port 2049, you will have to add a rule in the firewall. Issue 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
The highlighted entry in the following output shows the connection from 192.168.72.0/24 is allowed to port 2049.
Now we have completed the configurations on the NFS Server. Let’s move towards NFS client installation and configuration.
Installing and Configuring NFS Client
In the following 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. Let’s get started.
Step 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.
Step 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
Step 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
NFS_server_IP is the NFS server’s IP address
NFS_share is the share located on the NFS server
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.
We have used the following commands to mount the NFS shares to the NFS client.
$ 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 which shows 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.
Step 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.
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.
Unmounting 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
This is how you can setup NFS server and client in Debian 10 system. In this article, we have covered how to install the NFS server and client, configure NFS shared directories, and mount/unmount the NFS shares on the client system. You may want to visit our post on How to Configure NFS Server and Client on Linux Mint 20.