NFS Server is a protocol that allows file sharing over the network. It enables the users to access files the same way they would access local files. You can easily install NFS Server on Ubuntu and make remote sharing easier.
Usually, NFS is not encrypted and does not provide user authentication. Multiple clients can access the server over a network. Through this technology, you can share resources with numerous systems without giving them access to the underlying storage.
This article demonstrates how you can configure and install NFS Server on Ubuntu 20.04. It also explains how you can access the files on the server from the host machine.
What You’ll Need
This tutorial will use two machines: NFS Server and NFS Client.
Ubuntu 20.04 will be the NFS Server, but you can use any other Linux distribution for NFS Client. Both the client and server should be able to communicate over the network. Also, make sure that the NFS Server has sudo
privileges.
Before you start, make sure to allow traffic on port 2049.
NFS Server and Client IPs in this guide are:
- Server IP:
192.168.33.11
- Clients IPs: From the
192.168.33.0/24
range
Installing and Configuring NFS Server on Ubuntu 20.04
To install NFS Server on Ubuntu 20.04, you will have to install the required package, create a file system to export, and perform necessary configurations to the firewall.
1. Install the NFS Server
To run the NFS server on the kernel, the first step is to install the NFS server package. Before installing this package, you might want to update the package list by running the command given below:
sudo apt update
Now execute the following command to install the package:
sudo apt install nfs-kernel-server
Finally, NFS Server will start running automatically. The default NFS Server configuration is defined in /etc/default/nfs-kernel-server
and /etc/default/nfs-common
files.
2. Create the File System on NFS Server
Secondly, you will have to create a file system. By default, operations that require sudo
privileges are not allowed on NFS Server. However, a client might need to perform some operations that require elevated privileges but not superuser access.
Hence, there are two ways through which NFS Client can access files and directories on the host.
First Method
1. To share the directory with the client, first create a directory by running this command:
sudo mkdir /var/nfs/general -p
You can assign any name to your directory.
2. To give the appropriate ownership to the shared directory, execute the following command:
sudo chown nobody:nogroup /var/nfs/general
In case you execute any root operations on the client machine, the NFS will change the credentials to nobody:nogroup
.
Second Method
Additionally, the NFS Server shares the home directory with the NFS Client, which allows the root user access to the directory. Also, this directory already exists on the server, so you will not have to create it.
3. Export the File System on NFS Server
After creating the directory, and assigning the permissions, the next step is to export the file system. This step will allow the NFS Clients to access the files.
1. To export the file system on NFS Server, open the /etc/exports
file on the NFS Server, and execute the following command:
sudo nano /etc/exports
2. Add the following lines at the bottom of the file:
/var/nfs/general client_ip(rw,sync,no_subtree_check)
/home client_ip(rw,sync,no_root_squash,no_subtree_check)
3. Before exiting from the file, make sure to save it by pressing “Ctrl+X”.
4. Finally, restart the server using the command given below to make the changes applied.
sudo systemctl restart nfs-kernel-server
4. Configure the Firewall on NFS Server
It is important to configure the UFW firewall to allow the traffic coming from the NFS Client.
1. Firstly, you will have to enable access to NFS Server. To achieve this, run the command given below:
sudo ufw allow from client_ip to any port nfs
2. Check the status using the status
command to verify.
sudo ufw status
The output shows that the traffic on port 2049 is allowed.
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
2049 ALLOW 192.168.34.0/24
OpenSSH (v6) ALLOW Anywhere (v6)
Setting Up and Configuring NFS Client
Now that the NFS server is ready, it is time to set and configure the client. The client can be a virtual machine, a remote system, or the same server.
This step includes the installation of the required client package and the preparation of the storage system for the mount.
1. Install the NFS Clients
The first step is to install the NFS package on the client machine. You will install nfs-common
on the client machine. This package allows the client to mount the host directory on NFS Server, but the client cannot host it.
sudo apt update
sudo apt install nfs-common
2. Mount File Systems
After the installation of the NFS Client, it is time to configure it and mount the file system.
1. To mount the remote file system, create two directories at any location on NFS Client by executing the following commands:
sudo mkdir var/nfs/general -p
sudo mkdir var/nfs/home -p
2. It is time to mount the shared directories using the host’s IP address. For this step, execute the following commands:
sudo mount host_ip:/var/nfs/general /nfs/general
sudo mount host_ip:/home /nfs/home
3. To check if they have been mounted successfully, run df -h
command.
4. This will show the mounted directories at the bottom of the list. To check the actual memory utilization of each directory, execute the command given below:
du -sh /nfs/home
You will get this output:
36K /nfs/home
3. Test NFS Access
To test if the sharing works, you will have to create sample files.
1. To create a test file on /var/nfs/general
folder, use the touch command.
sudo touch /nfs/general/sample.txt
2. To check its permissions, type ls -l
with the complete file path as given below:
ls -l /nfs/general/general.sample
You will get the output similar to this:
-rw-r--r-- 1 nobody nogroup 0 Aug 25 13:31 /nfs/general/general.sample
Since you created this file as the client machine’s root user, the ownership appears as a nobody group
.
To compare it with the home directory share, create a file on /nfs/home
using the same command, and check its permission.
Hence, you will see that this method overrides the default behavior. Hence, creating the directory using this method allows the root users on NFS Client to as a root. At the same time, you are not giving root access to those clients on the host.
4. Unmount NFS File System
Lastly, the final step is to unmount the NFS file system. You can easily unmount it by moving out the shared directory’s structure as shown below:
sudo umount /nfs/home
sudo umount /nfs/general
Finally, this will only leave the local storage accessible.
The tutorial has discussed how to configure and install NFS Server on Ubuntu 20.04. You have also learned how to access remote files and directories using NFS Client. In case you use NFS in production, make sure to enable Kerberos authentication.
Since SSH is relatively easier to set up and configure, an alternative to NFS is to use an SSH connection to share directories remotely with client machines.
So, that’s a wrap for today! Let us know if you have any comments down below.
If this guide helped you, please share it.