edan.uk
Posted on
Linux

Setting Up NFS on Ubuntu 24.04

Author
Setting Up NFS on Ubuntu 24.04

Network File System (NFS) is an excellent solution for facilitating remote file access across a network. It operates on a client-server architecture, enabling seamless file sharing between various Linux distributions. The following sections will guide you through the installation and configuration of an NFS server and clients on Ubuntu 24.04.

Requirements

To proceed with the setup, ensure you have:

  • Multiple Ubuntu 24.04 machines to act as servers and clients.
  • Terminal access with sudo privileges on all machines.

While the steps discussed are specific to Ubuntu and Debian-based systems, variations exist for other distributions such as Fedora, CentOS, and Rocky Linux due to different package managers.

Installing NFS Server

Step 1: Install the NFS Kernel Server

Begin by setting up the NFS kernel server on your designated host machine.

  1. Update the package repository to ensure access to the latest available versions:

    sudo apt update
    
  2. Install the NFS kernel server:

    sudo apt install nfs-kernel-server -y
    

Wait for the installation process to finish before proceeding.

Step 2: Create a Shared Directory

After installing the necessary packages, set up a directory to share with the client machines.

  1. Create a directory for sharing:

    sudo mkdir -p /mnt/nfs_shared
    
  2. Change ownership to enhance accessibility:

    sudo chown nobody:nogroup /mnt/nfs_shared
    
  3. Set permissions to allow full access:

    sudo chmod 777 /mnt/nfs_shared
    

Step 3: Configure Permissions in the Exports File

Control access to the shared directory by editing the /etc/exports file.

  1. Open the exports file using your preferred text editor:

    sudo nano /etc/exports
    
  2. Add entries for each client with access, formatted as follows:

    /mnt/nfs_shared client_IP(rw,sync,no_subtree_check)
    

    Replace client_IP with the actual IP address of the client machine.

  3. For multiple clients, you can specify a subnet:

    /mnt/nfs_shared subnet_IP/24(rw,sync,no_subtree_check)
    

    Here, the options mean:

    • rw: Read/write permissions for clients.
    • sync: Ensures data integrity by writing changes before processing requests.
    • no_subtree_check: Disables subtree verification to alleviate potential issues.
  4. Save and close the file.

Step 4: Export the Shared Directory

Register your shared directory via the following commands:

sudo exportfs -a

Afterward, restart the NFS kernel server:

sudo systemctl restart nfs-kernel-server

Step 5: Configure Firewall Settings

For systems using UFW (Uncomplicated Firewall), allow NFS traffic:

sudo ufw allow from [client_IP or clientSubnet] to any port nfs

Confirm the rule is set by checking the firewall status:

sudo ufw status

Setting Up NFS Clients

Now, perform the following instructions on all client machines that need access to the shared files.

Step 1: Install NFS Common Package

  1. Update the package information:

    sudo apt update
    
  2. Install the NFS common package:

    sudo apt install nfs-common -y
    

Step 2: Create a Mount Point

You need a designated directory on the client to mount the shared NFS directory:

sudo mkdir -p /mnt/nfs_client

Step 3: Mount the Shared Directory

To mount the shared directory to your mount point, use the syntax below:

sudo mount host_IP:/mnt/nfs_shared /mnt/nfs_client

Replace host_IP with the actual IP address of the NFS server. Verify the mount with:

df -h

To unmount the directory when no longer needed, execute:

sudo umount /mnt/nfs_client

Step 4: Automate Mounting on Boot

To ensure the shared directory mounts automatically at startup, add an entry in the /etc/fstab file:

  1. Open the fstab file:

    sudo nano /etc/fstab
    
  2. Add the following line (ensure to replace host_IP accordingly):

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

You can repeat this for any additional shared directories you wish to mount.

Step 5: Confirm Connectivity

After setting everything up, create or copy files in the NFS server's shared directory. Verify that the client can access these files from the mounted directory.

Conclusion

Implementing NFS allows for efficient file access across multiple systems within a network. This setup enhances productivity while simplifying file management on Ubuntu machines. With the guidance provided, establishing an NFS network should be straightforward. Explore additional resources for optimizing your server security and remote access methods.