NFS: Tutorial & Best Practices

Network File System

What is NFS?

NFS, or Network File System, is a distributed file system protocol that allows you to share directories and files with others over a network. It was originally developed by Sun Microsystems in 1984 and has since become a key component in many Unix-like systems, including Linux. NFS allows you to mount remote directories on your server as if they were local, making it easier to share data across multiple machines.

Why Use NFS?

NFS is instrumental in environments where multiple servers need to access the same data. This is particularly useful for:

  • Centralized Storage: Keep all your files in one place.
  • Data Sharing: Easily share files between different servers.
  • Backup Solutions: Centralize your backup operations.

Installing NFS on Linux

Usually, NFS is not installed by default. Here's how you can install it on a Linux system:

On Ubuntu/Debian

sudo apt update
sudo apt install nfs-kernel-server

On CentOS/RHEL

sudo yum install nfs-utils

After installation, you might want to check the status of the NFS service to ensure it's running:

sudo systemctl status nfs-server

Setting Up NFS

Configuring the NFS Server

  1. Edit the Exports File: The /etc/exports file contains a list of directories that you want to share.

    sudo nano /etc/exports
    

    Add a line for each directory you want to share, along with the permissions and client specifications. Here’s an example:

    /srv/nfs    192.168.1.0/24(rw,sync,no_subtree_check)
    

    This line shares the /srv/nfs directory with all clients on the 192.168.1.0/24 network, allowing read/write access.

  2. Export the Directories: Run the following command to make your changes take effect:

    sudo exportfs -a
    
  3. Restart the NFS Service:

    sudo systemctl restart nfs-server
    

Configuring the NFS Client

  1. Install NFS Client: If not already installed.

    On Ubuntu/Debian:

    sudo apt install nfs-common
    

    On CentOS/RHEL:

    sudo yum install nfs-utils
    
  2. Mount the NFS Share: Create a mount point and mount the NFS share.

    sudo mkdir -p /mnt/nfs
    sudo mount 192.168.1.100:/srv/nfs /mnt/nfs
    

    Replace 192.168.1.100 with the IP of your NFS server.

  3. Persistent Mount: To make the mount persistent across reboots, add it to your /etc/fstab:

    192.168.1.100:/srv/nfs /mnt/nfs nfs defaults 0 0
    

Troubleshooting Common Issues

Permission Denied

If you encounter a "permission denied" error, ensure that both the server and client have the appropriate permissions. Check the /etc/exports file and directory permissions.

Stale NFS File Handle

This can occur if the server restarts or if there are changes to the underlying filesystem. A quick unmount and remount usually resolve this:

sudo umount /mnt/nfs
sudo mount /mnt/nfs

High Load

NFS can sometimes cause high load on the server if there are many simultaneous accesses. Consider tuning the NFS server parameters or upgrading your server hardware.

Best Practices

Use Sync Option

Always use the sync option in your /etc/exports file to ensure data integrity. This forces NFS to write changes to disk before responding to the client.

Limit Permissions

Be cautious with permissions. Use the ro (read-only) option where possible to limit the risk of data corruption.

Monitor Performance

Regularly monitor your NFS server performance with tools like top and iostat to catch any issues early.

Secure Your NFS Server

Use firewalls and consider employing NFS over a VPN to secure your data from unauthorized access.

By following these guidelines, you can set up a robust and efficient NFS server to meet your data-sharing needs.

The text above is licensed under CC BY-SA 4.0 CC BY SA