RoboDodd

Setting Up an NFS Server on a Raspberry Pi

Turn a Raspberry Pi into an NFS server with an external SSD to share files across your network, mount shares on Windows and Linux, and use them in k3s.

Setting Up an NFS Server on a Raspberry Pi
Raspberry Pi 2 min read

Welcome! In this guide, I’ll walk you through the steps to set up a Raspberry Pi as an NFS (Network File System) server. This setup is perfect for sharing files across your network.

To get started, I recommend using an external SSD as the storage location for your NFS server files. For example, I use the SAMSUNG T7 Portable SSD, and it works great.

You’ll also need an SSD and an adapter to load the OS onto your Raspberry Pi.

Step 1: Prepare Your Raspberry Pi

Download and Install Raspberry Pi Imager

  1. Download the Raspberry Pi Imager: https://www.raspberrypi.com/software/.
  2. Open the Imager application.
  3. Select your Pi device and choose an operating system. You can use Ubuntu or Raspberry Pi OS Lite. For this guide, I’ll use Raspberry Pi OS Lite.
  4. Press Ctrl + Shift + X to access advanced options. Here you can:
    • Configure wireless LAN settings.
    • Set the hostname and user/password.
    • Enable SSH (under services). Make note of the username and password as you’ll use them for SSH access.

Boot the Raspberry Pi

  1. Insert the prepared SD card into your Raspberry Pi.
  2. Power it on.

Open the console and type:

ip addr

Look for the IP address of your device. If you’re using Ethernet, check under eth0. For example, you might see 192.168.1.3.

Step 2: Access Your Raspberry Pi via SSH

Check for SSH Client on Windows

  1. Press Win + I to open Settings.
  2. Navigate to System > Optional Features.
  3. Search for “OpenSSH”:
    • If it’s installed, you’re good to go.
    • If not, click Add a feature, search for “OpenSSH Client,” and click Install.

Connect via SSH

Run the following command in PowerShell or Terminal (replace <user> and <ip> with your credentials):

ssh <user>@<ip>

For example:

ssh [email protected]

Step 3: Install and Configure the NFS Server

Install NFS Kernel Server

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

Mount Your External Drive

Configure the drive to auto-mount at startup:

sudo nano /etc/fstab

Add the following line (update /dev/sda1 as needed):

/dev/sda1 /mnt/usbdrive auto defaults 0 0

Save and exit (Ctrl + S, Ctrl + X).

Mount the drive:

sudo mount /dev/sda1 /mnt/usbdrive

Create a mount point:

sudo mkdir -p /mnt/usbdrive

Identify your drive:

sudo fdisk -l

Look for your USB drive (e.g., /dev/sdb1 or /dev/sdc1).

Set Permissions

sudo chown nobody:nogroup /mnt/usbdrive
sudo chmod 777 /mnt/usbdrive

Configure NFS Exports

Edit the NFS exports file:

sudo nano /etc/exports

Add the following line (adjust the network range as needed):

/mnt/usbdrive 192.168.1.0/24(rw,sync,no_subtree_check)

Save and exit.

Apply Changes

sudo exportfs -a
sudo systemctl restart nfs-kernel-server

Step 4: Test the NFS Server

Linux Clients

Mount the NFS share:

mount -o anon \\192.168.1.3\mnt\usbdrive X:

Install the NFS client:

sudo apt update
sudo apt install nfs-common -y

Windows Clients

  1. Open Command Prompt.
  2. You should see a new drive X: where you can add and access files.

Run the following command:

mount -o anon \\192.168.1.3\mnt\usbdrive X:

Step 5: Use NFS with k3s

Using an NFS server in a k3s setup is particularly beneficial because it allows you to share data between nodes and makes backing up data much easier. Here’s an example manifest to use the NFS share in a k3s deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k3-deployment
  labels:
    app: k3-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: k3-deployment
  template:
    metadata:
      labels:
        app: k3-deployment
    spec:
      containers:
        - name: k3-deployment
          image: robododd-test
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - mountPath: /server-stuff
              name: my-mount
      volumes:
        - name: my-mount
          hostPath:
            path: /mnt/usbdrive/test
            type: Directory
      nodeSelector:
        kubernetes.io/arch: amd64

Your NFS server is now set up and ready to use! Whether you’re sharing files across devices or integrating with a Kubernetes cluster, this setup provides a reliable and scalable solution. Happy tinkering!