June 13, 2025

How to Set Up an NFS Server on Linux: Complete Guide for Beginners (2025)

ubuntu

Introduction

Setting up an NFS (Network File System) server is an excellent way to share files across a network, enabling multiple clients to access data seamlessly. Whether you’re managing a small business network or a home lab, an NFS server provides a lightweight, efficient solution for file sharing on Linux systems like Ubuntu. In this comprehensive guide, we’ll walk you through the process of setting up an NFS server on Ubuntu, configuring clients, and securing the setup. With 7 years of DevOps experience, I’ve streamlined this tutorial to be beginner-friendly yet detailed enough for advanced users.

Table of Contents

What is NFS?

NFS, or Network File System, is a distributed file system protocol that allows a client computer to access files over a network as if they were on its local storage. Developed by Sun Microsystems, NFS is widely used in Linux and Unix environments for its simplicity and performance. It’s ideal for scenarios where multiple machines need access to shared resources, such as configuration files, media, or application data.

Diagram illustrating NFS server and client communication

Prerequisites

Before diving into the setup, ensure you have the following:

  • Server: A machine running Ubuntu 20.04 or later (other Linux distributions like CentOS work, but this guide focuses on Ubuntu).
  • Client: At least one client machine (Linux-based) to access the NFS share.
  • Network: Both server and client must be on the same network with known IP addresses.
  • Root Access: Administrative privileges (sudo) on both server and client.
  • Basic Tools: Ensure ssh and ping are available for connectivity checks.

Step 1: Install the NFS Server

First, we’ll install the necessary NFS server packages on the Ubuntu server. The primary package is nfs-kernel-server, which provides the core NFS functionality.

  1. Update the system: Always start with an updated system to avoid compatibility issues.
    sudo apt update && sudo apt upgrade -y
  2. Install NFS server: Run the following command to install the NFS kernel server.
    sudo apt install nfs-kernel-server -y

    Terminal screenshot of installing nfs-kernel-server on Ubuntu

  3. Verify installation: Check that the NFS server is installed.
    dpkg -l | grep nfs-kernel-server

    This should return details about the installed package.

Step 2: Configure NFS Exports

The NFS server shares directories via an “exports” file, which specifies which directories are shared and who can access them.

  1. Create a directory to share: For this example, we’ll create a directory called /mnt/nfs_share.
    sudo mkdir -p /mnt/nfs_share
  2. Set permissions: Ensure the directory is accessible. The NFS user (nobody:nogroup) needs read/write access.
    sudo chown nobody:nogroup /mnt/nfs_share
    sudo chmod 755 /mnt/nfs_share
  3. Edit the exports file: Open the /etc/exports file to define the share.
    sudo nano /etc/exports

    Add the following line to allow access from a specific client IP (e.g., 192.168.1.0/24 for a subnet):

    /mnt/nfs_share 192.168.1.0/24(rw,sync,no_subtree_check)
    • rw: Allows read and write access.
    • sync: Ensures data is written before confirming operations.
    • no_subtree_check: Improves performance by disabling subtree checking.

    Save and exit (Ctrl+O, Enter, Ctrl+X in nano).

  4. Export the share: Apply the configuration.
    sudo exportfs -a

Step 3: Start and Enable NFS Services

Start the NFS server and ensure it runs on boot.

  1. Start the NFS service:
    sudo systemctl start nfs-kernel-server
  2. Enable on boot:
    sudo systemctl enable nfs-kernel-server
  3. Verify status: Check that the service is running.
    sudo systemctl status nfs-kernel-server

    Terminal screenshot showing active NFS server status

Step 4: Configure the Firewall

If a firewall is active (e.g., UFW), allow NFS traffic.

  1. Allow NFS ports: NFS typically uses port 2049, along with other services like RPC.
    sudo ufw allow from 192.168.1.0/24 to any port 2049
    sudo ufw allow from 192.168.1.0/24 to any port 111
    sudo ufw allow from 192.168.1.0/24 to any port 20048
  2. Reload firewall:
    sudo ufw reload
  3. Check firewall status:
    sudo ufw status

Step 5: Set Up the NFS Client

Now, configure a client machine to access the NFS share.

  1. Install NFS client tools: On the client (Ubuntu), install the NFS client package.
    sudo apt update
    sudo apt install nfs-common -y
  2. Create a mount point: Create a directory to mount the NFS share.
    sudo mkdir -p /mnt/nfs_client
  3. Mount the share: Mount the NFS share from the server (replace 192.168.1.100 with your server’s IP).
    sudo mount 192.168.1.100:/mnt/nfs_share /mnt/nfs_client
  4. Automate mounting: Add to /etc/fstab for persistent mounts.
    sudo nano /etc/fstab

    Add:

    192.168.1.100:/mnt/nfs_share /mnt/nfs_client nfs defaults 0 0

    Save and exit.

Step 6: Test the NFS Share

Verify that the NFS share works correctly.

  1. Create a test file: On the server, create a file in the shared directory.
    sudo touch /mnt/nfs_share/testfile.txt
    echo "Hello from NFS!" | sudo tee /mnt/nfs_share/testfile.txt
  2. Check on client: Verify the file appears on the client.
    ls /mnt/nfs_client
    cat /mnt/nfs_client/testfile.txt

    You should see testfile.txt and its contents.

Step 7: Secure Your NFS Server

Security is critical for NFS servers, as they expose data over the network.

  • Restrict access: Limit access in /etc/exports to specific IPs or subnets.
  • Use a firewall: Ensure only trusted clients can connect (as configured in Step 4).
  • Enable Kerberos: For advanced setups, use Kerberos for authentication.
  • Regular updates: Keep the server and client systems updated.
    sudo apt update && sudo apt upgrade -y

Step 8: Troubleshoot Common NFS Issues

Here are solutions to common problems:

  • Mount failure: Ensure the server IP, share path, and firewall settings are correct. Use showmount -e 192.168.1.100 from the client to verify exports.
  • Permission issues: Check directory permissions and user mappings (nobody:nogroup).
  • Line ending errors: If scripts fail (e.g., $\r: command not found), convert files to Unix line endings using dos2unix.
    sudo apt install dos2unix
    dos2unix script.sh

Best Practices for NFS Servers

  • Monitor performance: Use tools like nfsstat to track NFS performance.
  • Backup data: Regularly back up shared directories to prevent data loss.
  • Limit write access: Use ro (read-only) in /etc/exports when possible.
  • Log activity: Enable logging for NFS operations to troubleshoot issues.

Conclusion

Setting up an NFS server on Ubuntu is straightforward with the right steps. By following this guide, you’ve learned how to install, configure, and secure an NFS server, set up a client, and troubleshoot issues. With proper maintenance and security practices, your NFS server will provide reliable file sharing for your network. For more advanced setups, explore tools like Ansible for automation or Docker Swarm for containerized deployments.

 

Amritpal

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 6+ years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

Amritpal

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 6+ years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

View all posts by Amritpal →

Leave a Reply

Your email address will not be published. Required fields are marked *