Setting Up a Raspberry Pi 4 as an NFS Server
Why Use a Raspberry Pi 4 for NFS?
If you primarily use Linux and macOS, NFS (Network File System) can provide better performance than SMB for file sharing. The Raspberry Pi 4 is a great low-power server for this purpose. This guide will walk you through installing Raspberry Pi OS, enabling SSH, and setting up NFS.
Step 1: Choosing the Right Raspberry Pi OS
For an efficient NFS server, you should use Raspberry Pi OS Lite (64-bit) because it’s lightweight and optimized for server tasks.
Recommended OS Versions:
- Best Option: Raspberry Pi OS Lite (64-bit) (no desktop, minimal resources used)
- Alternative: Raspberry Pi OS with Desktop (64-bit) (if you need a GUI, but not necessary for NFS)
⚠️ 32-bit vs. 64-bit: Choose 64-bit unless you have older software requiring 32-bit.
Step 2: Installing Raspberry Pi OS
1. Download Raspberry Pi Imager
- Install the official Raspberry Pi Imager on your computer.
2. Flash the SD Card
- Insert your microSD card into your computer.
- Open Raspberry Pi Imager.
- Click "Choose OS" → Select Raspberry Pi OS Lite (64-bit).
- Click "Choose Storage" → Select your SD card.
- Click "Settings" (⚙️ icon in the bottom-right corner).
Step 3: Enable SSH and Set Username
Before installing, configure SSH and user settings:
Enable SSH via OS Customization (Recommended)
- Set Hostname (optional, e.g.,
raspberrypi-nfs). - Enable SSH → Choose:
- "Use password authentication" (unless using SSH keys).
- Set Username & Password (prevents setup later).
- Click Save, then Write the OS to the SD card.
Alternative: Manually Enable SSH After Flashing
If you’ve already flashed the OS:
- Insert the SD card into your computer.
- Navigate to the boot partition and create an empty file named
ssh:touch /boot/ssh - Eject the SD card, insert it into the Raspberry Pi, and boot it up.
Connect to Raspberry Pi via SSH
Find your Raspberry Pi's IP using your router or raspberrypi.local (mDNS):
ssh pi@<raspberry_pi_IP>
Step 4: Setting Up the NFS Server
1. Install NFS Server
Once logged into your Pi via SSH, update the system and install NFS:
sudo apt update && sudo apt upgrade -y
sudo apt install nfs-kernel-server -y
2. Create a Shared Directory
Set up a folder to share over the network:
sudo mkdir -p /mnt/nfs_share
sudo chown nobody:nogroup /mnt/nfs_share
sudo chmod 777 /mnt/nfs_share
3. Configure NFS Exports
Edit the NFS configuration file:
sudo nano /etc/exports
Add this line (adjust for your LAN subnet):
/mnt/nfs_share 192.168.1.0/24(rw,sync,no_root_squash,no_subtree_check)
Save and exit (CTRL + X, then Y, then ENTER).
4. Apply Changes and Restart NFS Service
sudo exportfs -a
sudo systemctl restart nfs-kernel-server
sudo systemctl enable nfs-kernel-server
Step 5: Mounting the NFS Share on Clients
Linux Clients
- Install NFS support:
sudo apt install nfs-common -y - Create a local mount point:
sudo mkdir -p /mnt/nfs_client - Mount the NFS share:
sudo mount -t nfs 192.168.1.X:/mnt/nfs_share /mnt/nfs_client(Replace
192.168.1.Xwith the Raspberry Pi’s IP.) - Make the mount permanent: Add this line to
/etc/fstab:192.168.1.X:/mnt/nfs_share /mnt/nfs_client nfs defaults 0 0
macOS Clients
- Create a local mount point:
sudo mkdir -p /mnt/nfs_client - Mount the NFS share:
sudo mount -t nfs -o vers=4 192.168.1.X:/mnt/nfs_share /mnt/nfs_client - Make the mount persistent: Edit
/etc/auto_nfsand add:/mnt/nfs_client -fstype=nfs,resvport,vers=4 192.168.1.X:/mnt/nfs_shareThen update AutoFS:
sudo automount -vc
Conclusion
Your Raspberry Pi 4 is now a fully functional NFS server, providing fast and efficient file sharing for Linux and macOS devices. If you run into any issues, check logs using:
sudo journalctl -xe
Let me know if you need further assistance! 🚀
