How to set up an NFS server and client in an Ubuntu environment to share files/directories

Osanda Hemachandra
5 min readNov 2, 2019

--

Network File System (NFS) is a shared file system protocol originally developed by the Sun Microsystems. Through NFS, you can allow a system to share directories/files with others over a network. In NFS file sharing, users and also applications can access data on remote systems almost as if they were staying on a local machine.

NFS is performed in a client-server environment where the server is responsible for handling the authorization, authentication, and management of clients, as well as all the data shared within a specific file system. Setting up an NFS server on your Ubuntu system is very simple. All you need to do is make some necessary installations and configurations, both on the server and client machines and, you are good to go.

Let’s go step by step.

Setting Up the Host Machine

Step 1: Install NFS Kernel Server

Before installing the NFS Kernel server, you need to update your system’s repository index (install the latest available version of software through the Ubuntu repositories) from the following command:

$  sudo apt-get update

Now, run the following command to install the NFS Kernel Server on your system:

$  sudo apt install nfs-kernel-server

In a few seconds, the software will be installed successfully on your system.

Step 2: Create the Export Directory

The directory that we need to share with the client system is called an export directory. You can name it according to your choice; here, I am creating an export directory by the name of sharedfolder in my system’s mnt directory.

So create a folder to share, by specifying a mount folder name according to your need using the following command:

$  sudo mkdir -p /mnt/sharedfolder

As you want all clients to access the directory, remove restrictive permissions of the export folder by the following commands:

$  sudo chown nobody:nogroup /mnt/sharedfolder$  sudo chmod 777 /mnt/sharedfolder

Now all users from all groups on the client system will be able to access your sharedfolder.

You can create as many sub-folders in the export folder as you want, for the client to access.

Step 3: Assign server access to client(s) through NFS export file

After creating the export folder, you want to provide the clients the permission to access the host server machine. This permission is defined through the exports file located in your system’s /etc folder. Use the following command to open this file through the nano editor:

$ sudo nano /etc/exports

Updating this file needs root access; hence, you will need to use sudo with your command.

Once you have opened the file, you can allow access to:

1. A single client by adding the following line to the file:

/mnt/sharedfolder < client_IP >(rw,sync,no_subtree_check)

2. Multiple clients by adding the following lines to the file:

/mnt/sharedfolder < client1_IP >(rw,sync,no_subtree_check)/mnt/sharedfolder < client2_IP >(rw,sync,no_subtree_check)

3. Multiple clients, by specifying an entire subnet that the clients belong to:

/mnt/sharedfolder < subnetIP >/24(rw,sync,no_subtree_check)

Add the required line(s) to your exports file as you want and then save it by hitting Ctrl+X, entering Y, and then hit Enter.

Defined permissions in the file to the client:

  • rw: read and write operations
  • sync: write any change to the disc before applying it
  • no_subtree_check: prevent subtree checking

Step 4: Export the shared directory

After making all the above configurations in the host server machine, now you need to export the shared directory from the following command as sudo:

$  sudo exportfs -a

Finally, restart the NFS kernel server as follows:

$  sudo systemctl restart nfs-kernel-server

Step 5: Open firewall for the client(s)

Before starting the step, check your firewall status by running the following command:

$  sudo ufw status

If the status is inactive, skip this step since you don’t have an active firewall.

If not, follow the below steps.

This step sets the server’s firewall open to the clients so that they can access the shared content. The following command will configure the firewall to give access to clients through NFS:

$  sudo ufw allow from < client_IP / client_Subnet_IP > to any port nfs

Now when you recheck the firewall status, you will be able to see a new rule added to the firewall status (action status set as ALLOW from your client_IP to the host server machine).

Your host server machine is now ready to export the shared folder to the specified client(s) through the NFS Kernel Server.

Setting Up the Client Machine

Time to make configurations to the client machine, so that the shared folder from the host server can be mounted to the client and then accessed smoothly.

Step 1: Install NFS Common

Before installing the NFS Common application, update your system repository index from the command, as mentioned earlier.

Now, run the following command to install the NFS Common client on your system:

$  sudo apt-get install nfs-common

In a few seconds, the application will be installed on your system.

Step 2: Create a mount point for the NFS host’s shared folder

Your client’s system needs a directory where all the content shared by the host server machine in the export folder can be accessed. You can create this folder anywhere on your system.

$  sudo mkdir -p /mnt/sharedfolder

Step 3: Mount the shared directory on the client

The folder that you created in the above step is like any other folder on your system except you mount the shared directory from your host to this newly created folder.

Use the following command to mount the shared folder from the host to a mount folder on the client:

$  sudo mount < nfs_server_IP >:/mnt/sharedfolder /mnt/sharedfolder

Executing this command will export /mnt/sharedfolder from the server host machine to the mount location /mnt/sharedfolder on the client machine.

Step 4: Test the connection

Create or save a file in the export folder of the NFS host server machine. After that, open the mount folder on the client machine; you should be able to observe the same file shared and accessible in this folder.

Setting up an NFS client-server environment on Ubuntu systems is an easy task. From this article, I hope you learned how to establish the required NFS packages on both the server and the clients. Also, all the configurations that need to set up the environment. Now you can easily share content from one Ubuntu system to the other using this protocol.

See you in another article.

References

[1] https://help.ubuntu.com/lts/serverguide/network-file-system.html

[2] https://help.ubuntu.com/community/NFSv4Howto

[3] http://nfs.sourceforge.net/

--

--

Osanda Hemachandra

Senior Software Engineer @ Sysco LABS | Visiting Lecturer