In this guide, we will explain how to install NFS server on Ubuntu 24.04 step-by-step.
What is NFS?
NFS (Network File System) is a protocol that enables file sharing between systems on the same network. It allows users to access remote directories as if they were local, making collaboration and centralized data management much easier. Setting up an NFS server on Ubuntu 24.04 can be a valuable addition to your network infrastructure, enabling efficient file sharing among your devices.
Prerequisites
- A machine running Ubuntu 24.04
- A machine running Rocky Linux for the NFS client.
- Administrative (sudo) privileges on both the machines.
- A basic understanding of the Linux terminal.
Lab setup
- NFS Server IP: 192.168.1.11 Ubuntu 24.04
- Client System IP: 192.168.1.170 Rocky Linux 9
1) Install NFS Server on Ubuntu 24.04
To get started we are going to install the NFS kernel server package on Ubuntu 24.04 which will, in effect, turn it into an NFS server. But first, let’s update the package list as shown.
$ sudo apt update
Package repositories of Ubuntu 24.04 has the latest and stable NFS server package and its dependencies. Therefore, run the following command to install the NFS server,
$ sudo apt install nfs-kernel-server -y
This command will also install additional packages such as keyutils, nfs-common, rpcbind, and other dependencies required for the NFS server to function as expected.
Run following command to verify the service status of nfs-server
$ sudo systemctl status nfs-server
Above output confirms that NFS server’s service is up and running.
2) Create and Configure Shared Directory for NFS Server
Next, you will create the directory you want to share via NFS. This is the directory in which we will place files to be shared across the local area network For this example, let’s create a directory named “my_share” in the /mnt directory:
$ sudo mkdir /mnt/my_shares
Since we want all the files accessible to all clients, we will assign the following directory ownership and permissions using chown command,
$ sudo chown nobody:nogroup /mnt/my_shares $ sudo chmod -R 777 /mnt/my_shares
These permissions are recursive and will apply to all the files and sub-directories that you will create.
3) Configure NFS Exports
After creating the NFS directory share, now, you will need to configure NFS exports to specify which directories you want to share and who can access them. The /etc/exports
file defines which directories are shared and with whom. so, open the exports file ( /etc/exports) in a text editor:
$ sudo vi /etc/exports
To allow access to a single client, add the line below and replace the client-IP parameter with the client’s actual IP.
/mnt/my_shares client-IP(rw,sync,no_subtree_check)
In order to add more clients to the list, simply specify more lines as shown below:
/mnt/my_shares client-IP-1(rw,sync,no_subtree_check) /mnt/my_shares client-IP-2(rw,sync,no_subtree_check) /mnt/my_shares client-IP-3(rw,sync,no_subtree_check)
Additionally, you can specify an entire subnet as shown.
/mnt/my_shares 192.168.1.0/24(rw,sync,no_subtree_check)
This allows all clients in the 192.168.1.0 subnet access to the server. In our case, we will grant all clients access to the NFS server as shown
/mnt/my_shares 192.168.1.0/24(rw,sync,no_subtree_check)
Save and exit the text editor.
Let’s briefly brush through the permissions and what they stand for.
rw
: Allows the client to read and write to the shared directory.sync
: Data is written to the disk immediately (synchronous).no_subtree_check
: Disables subtree checking for better performance.
4) Export the Shared Directory
Once you have defined the exports, you need to export them by running:
$ sudo exportfs -a
Above command makes the shared directory accessible over the network (192.168.1.0/24).
Check the exported shares:
$ sudo exportfs -v
5) Adjust Firewall Rules
If you’re using UFW (Uncomplicated Firewall), you need to allow NFS traffic. Use the following commands:
$ sudo ufw allow from [client-IP or client-Subnet-IP] to any port nfs
In our case, the command will appear as follows:
$ sudo ufw allow from 192.168.1.0/24 to any port nfs $ sudo ufw reload $ sudo ufw status
We are all good now with configuring the NFS Server. The next step is to configure the client and test if your configuration works. So, let’s proceed and configure the client.
6) Mount NFS Share on Rock Linux 9
Login to Rocky Linux 9 system and install nfs client using following command,
$ sudo dnf install nfs-utils -y
After NFS client installation, create a directory (client_shared_folder) under /mnt folder on which we will mount NFS share from the server.
$ sudo mkdir -p /mnt/client_shared_folder
Finally, mount the remote NFS share directory to the client directory as follows.
$ sudo mount 192.168.1.11:/mnt/my_shares /mnt/client_shared_folder $ df -Th /mnt/client_shared_folder
Perfect, output above confirms that remote nfs share is mounted on “/mnt/client_shared_folder”
In order to mount this share permanently, we need to add the following entry in the /etc/fstab file.
192.168.1.11:/mnt/my_shares /mnt/client_shared_folder nfs auto,nofail,noatime,nolock,tcp,actimeo=1800,_netdev 0 0
7) Test NFS Share
You can verify that the NFS share is mounted correctly by listing the contents of the mounted directory:
$ cd /mnt/client_shared_folder $ ls
To test if our configuration is working, we are going to create a test file in the NFS shared directory, head back to the NFS server and run,
$ cd /mnt/my_shares $ touch nfs_share.txt
Now, let’s get back to our client and see if we can see the file in our mounted directory
$ ls /mnt/client_shared_folder/
And voila! There goes our file as shown in the snippet below. This is confirmation that our setup was successful.
That’s all from this guide, we hope this guide was beneficial to you and that you can comfortably share files using NFS on your network. Kindly do paste your queries and feedback in below comments section.
Conclusion
Congratulations! You’ve successfully installed NFS server on Ubuntu 24.04 and configured an NFS client on Rocky Linux 9. You can now share files and directories between these two systems effortlessly. NFS is a robust solution for network file sharing and can be customized further to suit your specific needs. Explore the possibilities and leverage NFS to streamline your file-sharing requirements.
Read Also : How to Install KVM on Ubuntu 22.04 Step-by-Step