Simplifying File Sharing: A Guide to Setting Up NFS Server and Client on Linux

Simplifying File Sharing: A Guide to Setting Up NFS Server and Client on Linux

Streamline File Transfers: Adopt NFS for Load Distribution and Consistent Uptime

Network File System (NFS) is a powerful tool that allows users to share files and directories across a network seamlessly. This blog post outlines the step-by-step process for setting up an NFS server and client on Linux, enabling efficient file sharing between systems.

Overview of NFS Setup

Setting up NFS involves two main components: the NFS server, which hosts the shared directories, and the NFS client, which accesses these directories. The following sections detail the commands necessary for configuration on both sides.

Server Side Configuration

  1. Install NFS Server:

    To begin, install the NFS kernel server package using:

     sudo apt install nfs-kernel-server
    
  2. Create Shared Directory:

    Create a directory that will be shared:

     sudo mkdir -p /root/public
    
  3. Set Permissions:
    Change ownership and permissions to allow access:

     sudo chown nobody:nogroup /root/public
     sudo chmod 777 /root/public
    
  4. Configure Exports:
    Edit the /etc/exports file to define access permissions for clients. Replace IP_of_Server_B with the actual IP address of the client:

     /root/public IP_of_Server_B(rw,no_root_squash,no_subtree_check)
    
  5. Export the Shares:
    Update the export table:

     sudo exportfs -a
    
  6. Adjust Firewall Settings:
    Allow NFS traffic through the firewall:

     ufw allow from Client_IP to any port nfs
    
  7. Restart NFS Service:
    Finally, restart the NFS server to apply changes:

     sudo systemctl restart nfs-kernel-server
    

Client Side Configuration

  1. Install NFS Common Package:
    On the client machine, install the necessary package:

     sudo apt install nfs-common
    
  2. Create Mount Point:
    Create a directory where the shared files will be mounted:

     sudo mkdir -p /mnt/public_share
    
  3. Mount the Shared Directory:
    Mount the NFS share from the server, replacing IP_of_Server_A with the server's actual IP address:

     sudo mount -t nfs IP_of_Server_A:/root/public /mnt/public_share
    
  4. Verify Mounting:
    Check if the mount was successful:

     df -h | grep public_share
    
  5. Persisting Mounts:
    To ensure that the mount persists after a reboot, add it to /etc/fstab:

     echo "IP_of_Server_A:/root/public /mnt/public_share nfs defaults 0 0" | sudo tee -a /etc/fstab
    

Conclusion

By following these steps, you can easily set up an NFS server and client on Linux, facilitating efficient file sharing across your network. This setup is ideal for environments where collaborative file access is necessary, enhancing productivity and resource management. With NFS, you can leverage your network's capabilities to streamline workflows and improve data accessibility across multiple systems.