How to Set Up a Raspberry Pi 5 NFS Server

Ben
Ben
@benjislab

Setting up a Network File System (NFS) server on your Raspberry Pi 5 allows you to share files and directories across your network, making it easy to access data from multiple devices. This guide will walk you through the steps to set up an NFS server on your Raspberry Pi 5, enabling efficient file sharing and network management.

Equipment Needed

  • Raspberry Pi 5 with Raspberry Pi OS installed
  • Internet connection
  • Access to the command line
  • External storage (optional)

Update Your System

Before starting, ensure your Raspberry Pi 5 is up-to-date with the latest software and security patches.

sudo apt update
sudo apt upgrade

Install NFS Server Software

To set up an NFS server, you need to install the nfs-kernel-server package.

sudo apt install nfs-kernel-server

Configure the NFS Server

Step 1: Create Shared Directory

Create the directory you want to share over the network. You can use an existing directory or create a new one. For example, to create a directory named nfs_share:

sudo mkdir /mnt/nfs_share

Step 2: Set Directory Permissions

Set the appropriate permissions for the shared directory. For example, to allow read and write access to all users:

sudo chown -R pi:pi /mnt/nfs_share
sudo chmod 755 /mnt/nfs_share

Step 3: Edit the Exports File

The exports file specifies the directories to be shared and their permissions. Open the file for editing:

sudo nano /etc/exports

Add the following line to share the nfs_share directory with read and write permissions for all clients on your local network (adjust the IP range as needed):

/mnt/nfs_share 192.168.1.0/24(rw,sync,no_subtree_check)
  • 192.168.1.0/24 specifies the IP range of your local network.
  • rw allows read and write access.
  • sync ensures changes are written to disk before the client is notified.
  • no_subtree_check improves reliability by disabling subtree checking.

Save and close the file.

Step 4: Export the Shared Directory

Export the shared directory to make it available over the network:

sudo exportfs -a

Step 5: Start and Enable the NFS Server

Start the NFS server and enable it to start on boot:

sudo systemctl start nfs-kernel-server
sudo systemctl enable nfs-kernel-server

Configure NFS Client

To access the NFS share from another device, you need to set up an NFS client. Here’s how to do it on a Linux machine:

Step 1: Install NFS Client Software

Install the NFS client package on the client machine:

sudo apt install nfs-common

Step 2: Create a Mount Point

Create a directory where you want to mount the NFS share:

sudo mkdir /mnt/nfs_clientshare

Step 3: Mount the NFS Share

Mount the NFS share to the mount point:

sudo mount 192.168.1.XX:/mnt/nfs_share /mnt/nfs_clientshare

Replace 192.168.1.XX with the IP address of your Raspberry Pi 5.

Step 4: Verify the Mount

Verify that the NFS share is mounted by listing the contents of the mount point:

ls /mnt/nfs_clientshare

Step 5: Automatic Mount at Boot

To mount the NFS share automatically at boot, add the following line to the /etc/fstab file on the client machine:

192.168.1.XX:/mnt/nfs_share /mnt/nfs_clientshare nfs defaults 0 0

Conclusion

Setting up an NFS server on your Raspberry Pi 5 is a straightforward process that can greatly enhance your network's file sharing capabilities. By following these steps, you can efficiently share files and directories across your local network, making data accessible to multiple devices. Whether you’re managing a home network or a small office setup, an NFS server on your Raspberry Pi 5 provides a reliable and efficient solution for network file sharing.