Install NFS Server on Raspberry Pi

date
Sep 28, 2023
thumbnail
slug
install-nfs-server-on-raspberry-pi
author
Can Durmus
status
Published
tags
Raspberry Pi
Linux
Installation
NFS
summary
Share files across devices on your local network!
type
Post
updatedAt
Oct 24, 2023 03:53 PM
Network File System (NFS) is a distributed file system protocol that allows a user on a client computer to access files over a network like how local storage is accessed. I am using it to share files across devices on my local home network. Please do not use it as your only backup system. I strongly recommend you have a look at the 3-2-1 backup strategy here: https://www.seagate.com/blog/what-is-a-3-2-1-backup-strategy/
I am starting this installation with a freshly installed Raspberry Pi OS. To install and configure an NFS server on a Raspberry Pi, you will first need to update your package lists for upgrades, updates, and new package installations, and then install the necessary NFS server packages.
Here are the steps to do this:

1. Update the Raspberry Pi

sudo apt update sudo apt upgrade

2. Install the NFS Server Package

sudo apt install nfs-kernel-server

3. Create the Export Directory

Choose a directory that you want to share with your NFS clients. You can either use an existing directory or create a new one. For example, to create a new directory:
sudo mkdir -p /srv/nfs

4. Set Permissions

You need to decide who can access the shared directory. You can either change ownership to a specific user or make it accessible to everyone. For instance, to make it accessible to everyone:
sudo chmod -R 777 /srv/nfs

5. Configure the NFS Exports

You will need to edit the exports file to define which directories you want to share.
sudo nano /etc/exports
Add a line for the directory you want to share, for example:
/srv/nfs *(rw,sync,no_subtree_check)
Save the file and exit (Ctrl + X, Y, Enter).
This line exports the /srv/nfs directory to all IP addresses (*) with read/write permissions (rw), ensures that changes made to files are written to disk (sync), and disables subtree checking (no_subtree_check).

6. Restart the NFS Server

Once you have configured the exports file, restart the NFS server for the changes to take effect.
sudo systemctl restart nfs-kernel-server

7. Adjust Firewall Settings

If you are running a firewall on your Raspberry Pi, you will need to allow access to NFS. Here’s how to do it using ufw:
sudo ufw allow from [Client IP or Subnet] to any port nfs
Replace [Client IP or Subnet] with the IP address or subnet of your NFS client.

8. Test the NFS Server

Finally, to test if the NFS server is working properly, you can run the following command on a client machine:
showmount -e [Server IP Address]
Replace [Server IP Address] with the IP address of your Raspberry Pi. If the setup is correct, this should list the directories exported by the NFS server.

9. Mount the NFS Share on the Client (Linux)

On the client machine, you can mount the NFS share with the following command:
sudo mount -t nfs [Server IP Address]:/srv/nfs /path/to/mount/point
Replace [Server IP Address] with the IP address of your Raspberry Pi and /path/to/mount/point with the directory where you want to mount the NFS share on your client machine.

Note

If you want the NFS share to be mounted automatically on the client every time it boots up, add an entry in the /etc/fstab file on the client machine like this:
[Server IP Address]:/srv/nfs /path/to/mount/point nfs defaults 0 0
That’s it! You should now have a working NFS server on your Raspberry Pi.