Setting Up NFS Server on Raspberry Pi

Introduction:
NFS (Network File System) allows you to share directories and files between systems over a network. In this guide, we’ll walk you through setting up an NFS server on your Raspberry Pi and sharing a directory with other systems.

Step 1: Install NFS Server
To install the NFS server package, run the following command:

sudo apt-get install nfs-kernel-server -y

Step 2: Create the NFS Share Directory
Next, create a directory that you want to share with other systems. In this example, we’ll use /mnt/nfsshare:

sudo mkdir /mnt/nfsshare

Step 3: Set Permissions for the NFS Share
For the NFS share to be accessible, we need to set appropriate permissions on the shared directory. In this example, we’ll set the permissions to allow everyone full access. Note that this configuration might not be suitable for production systems.

sudo chmod 777 /mnt/nfsshare

Step 4: Configure the NFS Export
Open the /etc/exports file in a text editor:

sudo vi /etc/exports

Insert the following line at the end of the file to define the NFS share:

/mnt/nfsshare *(rw,no_root_squash,insecure,async,no_subtree_check,anonuid=1000,anongid=1000)

Explanation of the options used:

  • *: Allow any client to access the share.
  • rw: Allow read and write access to the share.
  • no_root_squash: Preserve the root user’s identity from the client-side (use with caution).
  • insecure: Allow the use of non-privileged ports for NFS.
  • async: Improve performance by allowing asynchronous writes to the shared directory.
  • no_subtree_check: Disable subtree checking to improve performance.
  • anonuid=1000 and anongid=1000: Map the anonymous user to the UID and GID 1000, which typically represents the first regular user on most systems.

Step 5: Activate the NFS Export
After modifying the /etc/exports file, apply the changes using the following command:

sudo exportfs -ra

Step 6: Start NFS Services
Start the NFS services to make the shared directory accessible:

sudo systemctl start nfs-kernel-server

Step 7: Enable NFS Services on Boot
To ensure the NFS services start automatically on boot, enable them with the following command:

sudo systemctl enable nfs-kernel-server

Conclusion:
Congratulations! You have successfully set up an NFS server on your Raspberry Pi. The /mnt/nfsshare directory is now shared with other systems on the network. You can access this NFS share from other machines using the appropriate mount command.

Please note that for production environments or when sharing with specific clients, you should consider setting more secure and restricted permissions in the /etc/exports file.

Happy sharing!