How to Setup an NFS (Network File System) Server on Ubuntu

What is NFS?

The Network File System (NFS) is a distributed file system protocol that allows a user on a client computer to access files over a computer network much like local storage is accessed. While Windows relies heavily on SMB/CIFS for file sharing, NFS is the native, incredibly fast standard for sharing directories between Linux and Unix systems. It is perfect for centralizing web application files, creating shared backup repositories, or clustering servers.

Step 1: Install the NFS Kernel Server

Log in to the Ubuntu machine that will act as the storage server. Update your package manager and install the core NFS package:

sudo apt update

sudo apt install nfs-kernel-server -y

The NFS service will start automatically once the installation completes.

Step 2: Create the Export Directory

You must create the directory that you wish to share with your client machines (the “export”). For example, let’s create a shared directory for web cluster data:

sudo mkdir -p /mnt/nfs_share

Because NFS translates client root operations to the nobody:nogroup user for security reasons (root squashing), you must change the ownership of the directory to match:

sudo chown nobody:nogroup /mnt/nfs_share

sudo chmod 777 /mnt/nfs_share

Step 3: Configure the NFS Exports File

The /etc/exports file controls which directories are shared and who can access them. Open the file in a text editor:

sudo nano /etc/exports

Append a line at the bottom defining your share. You can grant access to a single IP address (e.g., 192.168.1.50) or an entire subnet (e.g., 192.168.1.0/24):

/mnt/nfs_share 192.168.1.0/24(rw,sync,no_subtree_check)

  • rw: Grants read and write permissions.
  • sync: Forces NFS to write changes to disk before replying to the client, preventing data corruption during a crash.
  • no_subtree_check: Disables subtree checking, improving reliability when files are renamed.

Step 4: Export the Share and Restart the Service

Save and close the file. To apply the new export configuration without rebooting the server, run the exportfs command:

sudo exportfs -a

Finally, restart the NFS kernel server to ensure everything is running smoothly:

sudo systemctl restart nfs-kernel-server

Step 5: Mount the Share on a Client Machine

Log in to your client Ubuntu machine. First, install the NFS common package so the client understands the protocol:

sudo apt install nfs-common -y

Create a mount point on the client and mount the remote share using the server’s IP address:

sudo mkdir -p /mnt/client_share

sudo mount 192.168.1.100:/mnt/nfs_share /mnt/client_share

You can now seamlessly read and write files across the network!

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.