NFS (Network File System) is a distributed file system protocol allowing a system to share directories and files over a network. With NFS, you can mount remote directories on your local system and access them as if on your local hard drive. This is useful for sharing files between multiple machines on a network or storing data on a central server that multiple clients can access.
NFS is a widely-used protocol and is supported on a variety of operating systems, including Ubuntu. You can easily set up an NFS server in Ubuntu and configure it to share directories over your network. Once you have set up your NFS server, you can mount shared directories on other machines on your network and access them just like you would any other local directory.
In this tutorial, we’ll show you how to install and use NFS on Ubuntu.
Before you start installing and using NFS on Ubuntu, there are a few prerequisites that you should meet:
- Ubuntu Installation: You should have a working installation of Ubuntu on your machine. If you still need to install Ubuntu, download the latest version from the official Ubuntu website and follow the installation instructions.
- This tutorial will use an Ubuntu 22.04 Server for the NFS server installation and an Ubuntu 22.04 desktop for the client.
- Root access: To install and configure NFS, you need root access to your Ubuntu machine. This can be obtained by logging in as the root user or using the sudo command to run commands with root privileges.
- Network connectivity: NFS relies on network connectivity to share files and directories between machines. Ensure your Ubuntu machine is connected to a network with the other machines you want to share files with.
Step 1: Install the NFS Server on Ubuntu
Log in to your Ubuntu server and run the following command in the terminal to update the local package index cache.
sudo apt-get update
Next, install the NFS server and its dependencies by running the following command.
sudo apt-get install -y nfs-kernel-server
Step 2: Create an export directory
Next, you need to create a directory that will be shared over the network. This directory is called an export directory. To create an export directory, run the following command:
Note. Replace the /ext_drive/nfs with the path you want to share. This example uses an external drive mounted to /ext_drive.
This will create a directory called nfs under the /ext_drive directory.
sudo mkdir /ext_drive/nfs
Next, change the ownership of the shared folder to nobody and nogroup. This command ensures that the root operations on the client will be translated to the said user and group. For example, when you create a file with sudo, the owner will not show as root. Instead, the owner will show as nobody and nogroup.
sudo chown -R nobody:nogroup /ext_drive/nfs ls /ext_drive/
Step 3: Configure the NFS server
Now that you have created an export directory, you must configure the NFS server to share it over the network. To do this, you need to edit the NFS configuration file /etc/exports. In this example, we’ll edit the file using nano.
sudo nano /etc/exports
In this file, add the following line:
This line tells the NFS server to export the /ext_drive/nfs directory to all clients, with read and write permissions (rw), to sync changes immediately (sync), and to not check for subdirectories (no_subtree_check).
Note. Check our guide on how to install a LAMP stack on Ubuntu.
The file will look similar to the screenshot below. Save and close the NFS configuration file.
Step 4: Export the directory
Now that you have configured the NFS server, you need to export the directory by running the following command:
sudo exportfs -a
This command exports all directories listed in the /etc/exports file.
Step 5: Install the NFS client on another machine
To access the shared directory from another machine, you need to install the NFS client on that machine. To do this, open a terminal window on the client machine and run the following command to update the local package cache.
sudo apt-get update
Now, install the NFS client package:
sudo apt-get install -y nfs-common
Step 6: Mount the shared directory on the client
Finally, you need to mount the shared directory on the client machine. To do this, create a directory where you want to mount the shared directory. In this example, let’s create the /nfs folder.
sudo mkdir /nfs
Next, mount the NFS shared drive to the /nfs directory on the local computer. Replace <server-ip> with the IP address of the Ubuntu machine where the NFS server is running.
In this example, the server IP address is 192.168.203.142, and the shared directory is /ext_drive/nfs.
sudo mount -t nfs <server-ip>:/ext_drive/nfs /nfs df -Th
As you can see below, we’ve mounted the NFS share as an nfs4 type filesystem.
Note. Learn how to perform system monitoring on Ubuntu using OSQuery.
Step 5: Test Access to the NFS Share
Now let’s test our access to the NFS share by creating a new file. On the client machine, run the following commands to create a new file called /nfs/test.file.
sudo touch /nfs/test.file ls -l /nfs/test.file
Since we created the file as root, the NFS automatically translated the owner to nobody and nogroup.
Step 6: Mount NFS Share at Boot
When you reboot the computer, the mounted NFS shares will disappear, and you’ll have to repeat the mounting process. We can add the mount to the /etc/fstab file to make it permanent.
Open the /etc/fstab file in the text editor.
sudo nano /etc/fstab
Once the file is open, add the following line.
192.168.203.142:/ext_drive/nfs /nfs nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
Save and close the file after editing.
From this point, the NFS share will be mounted persistently during computer reboots.
And that’s it! You have successfully installed and used NFS on Ubuntu. You can now share files and directories between multiple machines on your network using NFS.
Use case scenarios
Here are a few scenarios where NFS can be useful:
- Sharing files between multiple machines: If you have multiple machines on your network, you can use NFS to share files and directories between them. This can be useful if you have a central server containing important files and want to make them available to other machines on your network.
- Storing data on a central server: If you have a lot of data that needs to be accessed by multiple machines, you can store it on a central NFS server. This can be more efficient than storing the data on individual machines since it allows you to centralize your storage and avoid duplicating files.
- Running applications on remote machines: You may want to run an application on a remote machine but store the data locally. NFS can mount the application directory on your local machine while keeping the data on the remote machine.
In this tutorial, we learned the Ubuntu install NFS client and server. We’ve created the NFS share directory, modified the NFS configuration, and setup NFS mount on the client computer.
We’ve also shown how to modify the fstab file to create a persistent NFS mount on the client.