NFS (Network File System) is a file system protocol that allows users to view and access files and folders on a remote system as if they were stored locally. It is a client-server setup where the system that shares storage is called the server, while the system that accesses storage stored on a server is called the client. NFS allows users or system administrators to mount all or part of a server’s file system on the client’s system. Clients can then access the mounted files based on specific permissions (read, write) assigned to those files.
Creating the NFS client-server setup is a simple task that can be performed in a few steps – installation, export, mounting, and access. In this article, we will explain the process of setting up an NFS server and client on a Debian system so that you can share files between remote systems.
We have run the commands and procedures mentioned in this article on a Debian 10 system.
NFS Server
To setup a host system for sharing directories, we will have to install the NFS Kernel server in it. Follow the below steps to do so:
Step 1: Install NFS Kernel Server
Before proceeding towards the installation of NFS server, update your system repository index by running the following command in Terminal:
$ sudo apt-get update
Once updated, install NFS Kernel server by running the following command in Terminal:
$ sudo apt install nfs-kernel-system
The system might provide you with a Y/n option to confirm the installation. Hit y to confirm and the installation will be started in your system.
Step 2: Create the Export Directory
Now we have to create an export directory that will be used to share with the client’s systems. You can label it as per your preference. Here we are creating an export directory with the name “sharedfolder” in the /mnt directory.
Run the following command by mentioning the export directory path as follows:
$ sudo mkdir –p /mnt/sharedfolder
To allow all clients access permission to the export directory, you will need to remove restrictive permissions. Run the following command to do so:
$ sudo chown nobody:nogroup /mnt/sharedfolder
Then apply new permission that allows everyone read, write and execute access.
$ sudo chmod 755 /mnt/sharedfolder
It will allow all clients to access the shared folder.
Step 3: Configure the export directory
The configuration file for the NFS server is located at the /etc/ directory. Here you can specify the directories that you want to share with your clients along with the hostname of clients. In order to edit the /etc/exports file using the nano editor, run the following command as sudo in Terminal:
$ sudo nano /etc/exports
Use the following format to assign access to clients:
directory hostname(options)
In order to allow access to a single client, add the following line in it:
/mnt/sharedfolder clientIP(rw,sync,no_subtree_check)
In order to allow access to multiple clients, add the following line in it:
/mnt/sharedfolder client1IP(rw,sync,no_subtree_check) /mnt/sharedfolder client2IP(rw,sync,no_subtree_check)
In order to allow access to multiple clients by specifying a whole subnet, add the following line in it:
/mnt/sharedfolder subnetIP/24(rw,sync,no_subtree_check)
Here, we are specifying the whole subnet for clients to allow them access to our shared directory.
Once done with editing the /etc/exports file, press ctrl+O to save and ctrl+X to exit the file.
The parameters (rw,sync,no_subtree_check) in above file means the client has the following permissions:
- rw: read and write operations
- sync: write any change to the disc before applying it
- no_subtree_check: no subtree checking
Step 4: Export the shared directory
Next in this step, you will have to export the shared directory listed in /etc/exports. To do so, run the following command in Terminal:
$ sudo exportfs –a
Then restart the NFS Kernel server in order to apply the configuration changes.
Step 5: Configure firewall
Now it is important to verify that the server is open for the clients to access the shared content. You have to add the rule that allows traffic from the specified clients to the NFS port. To do so, use the following syntax:
$ sudo ufw allow from [client-IP or client-Subnet] to any port nfs
In our example, we are going to allow whole 192.168.72.0 subnet to the NF port:
$ sudo ufw allow from 192.168.72.0/24 to any port nfs
Now to verify if the rule is successfully added, run the following command in Terminal:
$ sudo ufw status
Now our host NFS server is configured and ready to be accessed by the specified clients.
Configuring the Client Machine
Now, we will configure the client machine to make them access the server’s export directory. Follow the below steps to do so:
Step 1: Install NFS client
First, update your client machine repository index by running the following command in Terminal:
$ sudo apt-get update
Then install the NFS client application known as NFS common by running the following command in Terminal:
$ sudo apt-get install nfs-common
The system might provide you with a Y/n option to confirm the installation. Hit y to confirm and the installation will be started in your system.
Step 2: Create a mount point for the NFS sever’s shared folder
Now create a mount point that will be used to access the shared content of the server. Here we are creating the mount point with the name “sharedfolder_clientr” in the /mnt directory. We have used the following command to do so:
$ sudo mkdir -p /mnt/sharedfolder_client
Step 3: Mount the server’s shared directory on the client
In the previous step, we have created the mount point. Now we will mount the NFS server’s shared directory to the above-created mount point. Following syntax can be used for this purpose:
$ sudo mount serverIP:/exportFolder_server /mnt/mountfolder_client
For instance, in our case, we will mount the shared directory “/mnt/sharedfolder” from the NFS server to the mount point “/mnt/mountfolder_client” in our client machine.
$ sudo mount 192.168.72.164:/mnt/sharedfolder /mnt/sharedfolder_client
Where 192.168.72.164 is our NFS server’s IP.
Now the shared NFS directory has mounted to the client’s machine.
Step 4: Test the connection
Now its time to test our NFS client-server setup. To do so, create a test file or folder in your NFS server shared directory. Like in the below example, you can see that we have created two folders name “documents” and “music” in our NFS server shared directory.
Now open the mount point on your client machine. You will see here the same files and folders that were created in the server’s shared directory.
That is all there is to it! I hope you have learned to install and setup the NFS server and client on a Debian 10 system. This article also covers how to access the NFS host in order to access the shared information.