NFS (Network File System) works in the form of a client-server model where the machine sharing the directories is known as the NFS server whereas the machine accessing those directories is known as the NFS client.
In this context, we shall look into how to configure the NFS server and client on Linux Mint 20.
Previously, we dealt with how to configure the NFS client and server on the Debian system .
How to set up NFS server and client on Linux Mint 20 ?
Here, we will be using the two machines with the following details:
NFS Server (Linux Mint 20)
NFS Client (Linux Mint 20)
Steps to set up NFS Server:
1. Install NFS Server
On the machine to be setup as an NFS server, you will need to install nfs-kernel-server. i. First, update the local repository index:
$ sudo apt update
ii. Enter the password for sudo.
iii. Then install nfs-kernel-server package as follows:
$ sudo apt install nfs-kernel-server
iv. After running the above command, the system will prompt with the y/n option. Hit y to continue the installation.
v. After the installation is completed, you can verify it by running the following command in Terminal:
$ dpkg -l | grep nfs-kernel-server
Now, NFS server will be installed successfully.
2. Create an Export Directory
On the server machine, create an export directory you want to share with the clients. Let's say we want to export two directories /var/nfs–share and /var/nfs-share1.
i. First, we will have to create these directories. Run the following commands with sudo to do so:
$ sudo mkdir -p /var/nfs-share
$ sudo mkdir -p /var/nfs-share1
ii. As these directories were created with sudo, therefore the root user currently owns them.
You can verify it by running the ls commands as follows:
$ ls -la /var/nfs-share
$ ls -la /var/nfs-share1
iii. From our two export directories (/var/ nfs-share and /var/ nfs-share1), we will change the ownership of one of the directory /var/ nfs-share to nobody:nogroup.
We will not change the ownership of the second directory; it will remain owned by the root user.
iv. Run the following command to change the ownership of /var/ nfs-share directory from root to nobody:nogroup:
$ sudo chown nobody:nogroup /var/nfs-share
v. You can confirm the new owner through the following command:
$ ls -la /var/ nfs-share
Now, you will see that the /var/ nfs-share is now owned by nobody:nogroup.
3. Configure and Share the NFS Exports
Next, we will configure NFS for the sharing of the export directories.
The NFS configuration file is /etc/exports which determine which clients will have access to the exported directories.
i. In the /etc/exports file, add the configuration lines using the following syntax:
export_directory1 client1(options) client2(options)...clientN(options)...
export_directory2 client1(options) client2(options)...clientN(options)...
export_directoryN client1(options) client2(options)...clientN(options)...
a. export_directory is the directory you want to share.
c. client is the IP address or the subnet of the client that wants to access the shared directories. To find the IP address of a machine, you can visit our post on How to Find the IP address.
c. options specify how the resources should be shared.
These configuration lines basically specify the directories we are going to export and who can access them.
ii. In order to share our NFS export directories (/var/ nfs-share and /var/ nfs-share1) to client IP addres 192.168.72.191, we will add following lines in the configuration file /etc/exports.
/var/nfs-share1 192.168.72.191(rw,sync, no_root_squash,no_subtree_check)
Our configuration lines specify the directories (nfs-share and nfs-share1) we want to export to the client 192.168.72.191 with read-write, sync, and no_subtree_check options.
In the second entry, you can find the no_root_squash option which gives the client root permission on the server.
You can visit more information about the options on the exports man page.
iii. Save and close the file.
iv. Now you will have to share the exports configured in the /etc/exports. You can do so using the following command in Terminal:
$ sudo exportfs -av
Here, the NFS server is exporting the shared directories to the clients.
Alternatively, you can also export the shares by restarting the NFS service:
$ sudo systemctl restart nfs-kernel-server
You can also check the NFS service status to ensure if it is actively running. Here is the command to do so:
$ sudo systemctl status nfs-kernel-server
You will see that the NFS server is running fine without any issues.
4. Add a Firewall Rule for NFS Traffic
i. First, check if a firewall is enabled on your server by running the following command in Terminal:
$ sudo ufw status
If you see the active status in output, it means the firewall is enabled.
ii. Therefore, now you will have to add a rule in the firewall for NFS traffic.
Using the following syntax, add a rule in the firewall to allow clients to NFS port 2049.
$ sudo ufw allow from client_ip/subnet_ID to any port nfs
For example, you can run the following command to allow client subnet 192.168.72.0/24 access to NFS server port 2049:
$ sudo ufw allow from 192.168.72.0/24 to any port nfs
iii. Now to check if the firewall rule has been added successfully, run the following command:
$ sudo ufw status
Steps to set up NFS Client ?
1. Install NFS Client
On the machine to be setup as an NFS client, you will need to install nfs-common package on it.
You can install it as follows:
i. First, update the local repository index:
$ sudo apt update
ii. Enter the password for sudo.
iii. Then install nfs-common package:
$ sudo apt install nfs-common
iv. After running the above command, the shell will prompt with the y/n choice. Press y to continue the procedure.
v. After the installation of client package is completed, you can verify it as follows:
$ dpkg -l | grep nfs-common
2. Create Mount Points
To mount the NFS shares on your client machine, you will first have to create the mount points.
We will be creating two directories for our mount points:
$ sudo mkdir -p /media/nfs-share
$ sudo mkdir -p /media/nfs-share1
3. Mount NFS Shares on the Client Manually
Now that we have the mount points, we can mount the NFS shares exported from the NFS server on the client.
i. Use the following syntax to mount the NFS shares:
$ sudo mount NFS_server:NFS_export_directory client_mountpoint
In the above command syntax:
a. NFS_server: Enter IP address of NFS server
b. NFS_export: Enter NFS export directory shared by the NFS server
c. client_mountpoint: Enter the mount point of the client where you want to mount the NFS export directory.
We have run the following commands to mount the NFS shares:
$ sudo mount 192.168.72.170:/var/nfs-share /media/nfs-share
$ sudo mount 192.168.72.170:/var/nfs-share1 /media/nfs-share1
The above commands mount the shares from:
/var/nfs-share directory on the NFS server (192.168.72.170) to the client's local mount point /media/nfs-share
/var/nfs-share1 directory on the NFS server (192.168.72.170) to the client's local mount point /media/nfs-share1.
Now run this command to verify if the mounting has been successful:
$ df -h
The output of the df command will shows the NFS mounts at the bottom.
Using the below command, you can also determine how much space is actually used by each mount point:
$ du -sh <mount_point_name>
As we have created the mount points under the /media directory, the system will create one link to that mount point in the left panel of File Manager. Therefore, you can access the shared directories from your File Manager.
You will also find one link to mount point on the Desktop.
4.Mount the NFS shares at startup (automatically)
In the previous step, we have seen how to mount the NFS shares on the client manually. But when you restart the system, they get unmounted.
Therefore, after restarting the system, you will have to again mount it.
To avoid this manual procedure, again and again, you can make the mount permanent on your system by configuring it in the /etc/fstab file.
This configuration will make the NFS shares to automatically mount at startup.
Open the /etc/fstab file using the Nano editor as follows:
$ sudo nano /etc/fstab
In the file, add entries for your shares that you want to mount at startup:
192.168.72.170:/var/nfs-share /media/nfs-share nfs rw,sync,hard,intr 0 0
192.168.72.170:/var/nfs-share1 /media/nfs-share1 nfs rw,sync,hard,intr 0 0
Then save and close the file.
Now after every restart, the NFS shares will be automatically mounted at their specified mount points.
5. Test NFS Access
To test the NFS access, create a new file in each shared directory in your client machine.
First, create a sample.txt file as sudo in the /media/nfs-share:
$ sudo touch /media/nfs-share/sample.txt
Then create a second file sample1.txt as sudo in the /media/nfs-share1:
$ sudo touch /media/nfs-share/sample1.txt
Now check the ownership of both files:
$ ls -l /media/nfs-share/sample.txt
$ ls -l /media/nfs-share1/sample1.txt
You will see the ownership of the sample.txt file is nobody user and nogroup group.
Even though this file was created as sudo but the NFS server has now translated its ownership to the server's nobody:nogroup. This shows the root user on the NFS client cannot carry the administrative jobs on the NFS share.
The ownership of the other file sample1.txt is owned by the root user and root group. It is due to the no_root_squash option that we configured in the /etc/exports file. It enables the root user on the NFS client to function as root and performs administrative jobs.
Now, also check if you can access both files from the NFS server.
How to Unmount NFS Share ?
In case, you no longer want the NFS shares, you can easily unmount them from your machine. To unmount the NFS shares, use the following syntax:
$ umount <mount_point_name>
In our example, it would be:
$ umount /media/nfs-share
$ umount /media/nfs-share1