LinuxHowTo
How to Set Up an NFS Server on Ubuntu 24.04 LTS (Step-by-Step Guide)

How to Set Up an NFS Server on Ubuntu 24.04 LTS (Step-by-Step Guide)

Network File System (NFS) is a distributed file-sharing protocol that allows users to access and share files and directories over a network as if they were stored locally. It operates on a client-server model, where the NFS server exports shared directories, and multiple clients can mount them for seamless access. NFS is widely used in Linux environments to enable efficient resource sharing between systems. It’s especially valuable in cloud-based setups, as it helps reduce storage costs and optimize space by centralizing data on a single server.

In this guide, you’ll learn how to set up an NFS server on Ubuntu 24.04 LTS. The steps provided are also compatible with other Ubuntu-based or Linux distributions, making it a versatile solution for file sharing across multiple systems.

Prerequisites

Before getting started, ensure you have:

1. A server running Ubuntu 24.04 LTS with a static IP and root access.

2. A client system running Ubuntu with sudo privileges.

Step 1: Update the System

Begin by updating your system packages to ensure you’re working with the latest security patches and features.

sudo apt update -y && sudo apt upgrade -y

Step 2: Install and configure the NFS Server

On your server system, install the NFS server package:

sudo apt install nfs-kernel-server -y

Start the NFS service, enable it to launch at system boot, and verify its status using the following commands:

sudo systemctl start nfs-server.service

sudo systemctl enable nfs-server.service

sudo systemctl status nfs-server.service

Output:

Step 3: Create and Configure the NFS Shared Directory

Next, create a shared directory on the NFS server that will be accessible from the client machine. Use the following command to create it:

sudo mkdir -p /mnt/nfsshare

Create test files:

sudo touch /mnt/nfsshare/file1.txt

sudo touch /mnt/nfsshare/file2.txt

Set proper ownership and permissions:

sudo chown -R nobody:nogroup /mnt/nfsshare/

sudo chmod 777 /mnt/nfsshare/

Step 4: Define NFS Export Rules

Now, open the NFS server configuration file and specify the directory you want to share. Use the following command to edit the file:

sudo vim /etc/exports

Add the following line (replace with your client’s IP address):

/mnt/nfsshare 192.168.1.51(rw,sync,no_subtree_check)

To allow multiple clients, add separate lines for each IP.

/mnt/nfsshare 192.168.1.51(rw,sync,no_subtree_check)
/mnt/nfsshare 192.168.1.52(rw,sync,no_subtree_check)

Explanation of options:

  • rw: Grants read/write access.
  • Sync: Ensures changes are committed to disk before replying.
  • no_subtree_check: Prevents subtree checks for improved performance.

Export the shared directory:

sudo exportfs -a

To apply the changes, restart the NFS service:

sudo systemctl restart nfs-kernel-server

Step 5: Install and Configure NFS Client

On the client machine, install the NFS client utilities:

sudo sudo apt update -y && sudo apt install nfs-common -y

Start and enable the NFS client service:

sudo systemctl start nfs-client.target

sudo systemctl enable nfs-client.target

Verify the NFS server’s exported directory from the client:

showmount -e <NFS_SERVER_IP>

Make sure to replace the IP address with the actual IP address of your NFS server.

showmount -e 192.168.1.21

Output:

Step 6: Mount the NFS Share on the Client

Create a mount point on the client:

sudo mkdir -p /mnt/nfs_share_mount

Then, mount the shared directory from the NFS server on the NFS client using the following command:

sudo mount 192.168.1.21:/mnt/nfsshare /mnt/nfs_share_mount

Here, replace the IP address with your NFS server IP address.

To mount the NFS share automatically on boot, edit /etc/fstab:

sudo vim /etc/fstab

Next, add the following line, replacing it with your NFS server’s IP address and the NFS directory name:

192.168.1.21:/mnt/nfsshare /mnt/nfs_share_mount nfs defaults 0 0

Now, the NFS file system will be automatically mounted every time the system reboots.

Step 7: Verify the NFS share on the client system

Use the df -h Command to verify the mounted directory:

df -h

Output:

The mount point will show your server’s IP address, followed by the shared directory path, as highlighted in the output above.

Now, test file creation from the client:

touch /mnt/nfs_share_mount/file3

Here, we created file3 on the NFS client. Now, log in to the NFS server and check the NFS directory.

ll /mnt/nfsshare/

Output:

You should see file3 created by the client.

Conclusion

You’ve successfully set up an NFS server on Ubuntu 24.04 LTS and connected to it from a client machine. You can now share files seamlessly across Linux systems. If you have any questions or run into issues, feel free to ask!