How to Install and Configure NFS on Ubuntu 24.04

Estimated reading: 6 minutes 288 views

NFS, which stands for Network File System, is a tool that lets you share files over a network—having a folder on one computer that others can open, edit, and save files to, just like it’s on their machine. For Ubuntu users, NFS is beneficial. This means you have one central spot for all your files, which anyone on your network can use without needing to transfer files back and forth.

This setup is perfect for groups working on projects or for anyone who wants easy access to shared resources. It simplifies file management, reduces clutter, and speeds up collaborative work. This article will guide you through how to set up NFS on the Ubuntu system, turning it into a file-sharing hub for your network.

Prerequisites

This guide requires the following prerequisites:

  • An Ubuntu Server 24.04 – For installing the NFS Server(IP:173.249.203.65)
  • A Linux Client machine – This example will be used by the Ubuntu client machine(IP: 173.249.203.68)

Install the NFS Server on the server

1. Install NFS Server

To begin, we will install the NFS kernel server package on Ubuntu, effectively transforming it into an NFS server. However, let’s first update the package list as displayed.

# apt update
# apt upgrade

The NFS kernel server package can then be installed by running the following command.

# apt install nfs-kernel-server

Purpose: This command installs the necessary software to run an NFS server on your Ubuntu machine.

Once the installation has been completed, you can check the NFS status with the following command:

# systemctl status nfs-kernel-server

Output:

Start/Enable NFS Server

# systemctl start nfs-server 
# systemctl enable nfs-server
  • start: begins the service immediately.
  • enable: ensures the service starts on system boot.

2. Create the Directory to Share

Create a directory specifically for NFS sharing:

# mkdir /mnt/nfs -p
  • mkdir makes a new directory: The -p flag ensures no error is thrown if the directory already exists, and it creates parent directories as needed

Adjust the ownership and permissions:

# chown nobody:nogroup /mnt/nfs/
# chmod 777 /mnt/nfs/
  • “chown nobody:nogroup” changes the directory ownership to nobody and nogroup, which is a security best practice for NFS shares to prevent unauthorized access.
  • “chmod 777” gives read, write, and execute permissions to everyone. This is risky in production environments; consider more restrictive permissions.

3. Configure NFS Exports

Edit the NFS export file to define access:

# nano /etc/exports

nano is a simple text editor. Here, you define which directories are shared and with what permissions.

Add to content with syntax folder_to_share client(share_option1,…,share_optionN)

/mnt/nfs 173.249.203.0/24(rw,sync,no_subtree_check)

Explain the parameters

  • 173.249.203.0/24: The IP range of the client that is allowed to access the shared folder.
  • rw: Grants the client read and write permissions to the shared folder. (Always used).
  • ro: Grants read permissions, not edits.
  • sync: Writes changes to the hard drive before committing. (Always used)
  • no_subtree_check: Disables subtree checking. (Always used)
  • no_root_squash: The client will be able to access the shared folder as the root of the server. (Not recommended)

Apply the export settings:

# exportfs -a

“exportfs -a” exports all directories listed in /etc/exports, making them available to clients.

4. Firewall Configuration

If you have installed UFW on the Ubuntu system, configure it to allow NFS traffic. As you can see on top, each shared directory is accessible through a specific client IP address and network. So you will need to specify the client IP address or network in the UFW firewall rule that will be allowed to access the NFS service port.

Run the ufw command below to allow the client IP address “173.249.203.68” and IP range “173.249.203.0/24” to access the NFS service port.

# ufw allow from 173.249.203.68 to any port nfs
# ufw allow from 173.249.203.0/24 to any port nfs 

Now reload the UFW firewall rule and verify the list of firewall rules using the below command. You should see the default NFS service port “2049” is accessible through specific client IP addresses and networks.

# ufw reload
# ufw status

Connect to NFS Share on Client

1. Install NFS client

On your client machine, install the NFS client:

# apt install nfs-common

Purpose: Install client utilities needed to mount NFS shares.

2. Create mount directory

Create a new directory to connect to the NFS Share on the server.

# mkdir /mnt/nfs_client

3. Connect to NFS Share

Connect to the NFS Share on the server and mount to the newly created directory on the workstation using the following command

# mount 173.249.203.65:/mnt/nfs /mnt/nfs_client

Now run the following command to check and verify the list of mounted disks on your Ubuntu machine. You should see the NFS shared directory “/mnt/nfs” is mounted to your local machine on the directory “/mnt/nfs_client“.

# df -h

The NFS connection is working.

4. Testing NFS

We will create a few files in the nfs directory on the Ubuntu NFS server in preparation for testing the NFS on the client system:

# cd /mnt/nfs/
# touch green.cloud.txt

Go to the client system after that to see the list of files that are present in the “nfs” directory:

# ls -l /mnt/nfs_client/

According to the output, the Ubuntu client’s successful use of an NFS server is shown by the ability to access files:

5. Auto connect on startup

To make the client automatically connect to the NFS network drive every time it starts, I will edit the /etc/fstab file.

# nano /etc/fstab

Add the following content and save

173.249.203.65:/mnt/nfs/ /mnt/nfs_client nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0

Restart the client machine, the NFS network drive will be automatically mounted to the /mnt/nfs_client directory.

Conclusion

Congratulation! You have now successfully installed the NFS Server on the Ubuntu 24.04 server. you have also secured the NFS Server access using the UFW firewall. In the end, you have also learned how to set up an Ubuntu client machine to mount the NFS shared directory, which includes how to mount the NFS shared directory automatically at system startup via the “/etc/fstab” file.

Share this Doc

How to Install and Configure NFS on Ubuntu 24.04

Or copy link

CONTENTS