Making a NAS out of my Raspberry Pi 4

I used to have a Raspberry Pi B (model 1) act as my NAS.
The PI (or probably it’s memory card) died on me, and I’ve always been appalled by it’s slow, slow data throughput, so I ordered a new Raspberry Pi 3B+. Literally two days after it ordered, the Pi 4B+ was announced. So I ordered that one too.

Finally, I have some time to put it together.

What I want is basically the same as the old:

  • Have a fixed IP on my network
  • Have SSH to be able to remotely manage it
  • Serve as a NAS, available over SMB, for a KODI media player somewhere on the same network
  • Serve as a NAS, available over NFS, to make backups of my music and photos
  • Serve as a KODI database library server.
  • Be available over Ethernet (and not WIFI, as it’s right next to the router)
  • In the future: have a physical “clean shutdown” button.

For this I chose the useful tutorial from Desertbot: Headless Raspberry Pi 4 SSH WiFi Setup (Mac + Windows). It is for Mac+Windows, but I guess I can manage with Linux as a host system as well.

Basic Raspberry Pi Headless Wired install

I like to install raspberry pi’s headless, that way you don’t need to lug out a monitor just for the initial install.
I have my Raspberry connected with only power and an ethernet cable, no keyboards, mice, monitors.

  1. Get Raspbian Buster Lite from https://www.raspberrypi.org/downloads/raspbian/ and download the latest image.
  2. Write the image to the SD card using the following commands (replace sdX with the right partition, use lsblk to check which partition is your SD card.):
    sudo dd bs=4M if=2021-03-04-raspios-buster-armhf-lite.img of=/dev/sdX conv=fsync status=progress
    … aaand wait a minute or 5
  3. To enable SSH, create an empty file “ssh” in the /boot folder
    touch ssh
  4. Per How to disable wifi in Raspberry Pi 4, I disabled wifi by adding
    dtoverlay=pi3-disable-wifi
    to /boot/config.txt – the pi3 works for the Pi 4, and the claimed also-working disable-wifi does not work.
  5. Enable a static IP address:
    in /etc/dhcpcd.conf, uncomment the lines under the static IP configuration: (found in How to set up a static IP using Raspbian Buster)
    #Example static IP configuration:
    interface eth0
    static ip_address=192.168.15.2/24
    static routers=192.168.15.1
    static domain_name_servers=192.168.15.1 8.8.8.
    8 fd51:42f8:caae:d92e::1
  6. Also, I changed /etc/hostname to the desired hostname, in this case “asteriskpi” (for historical reasons)
    • Update 2023-02-09 Apparently, you now also need to edit /etc/hosts/ and give the 127.0.1.1-line the same hostname.
  7. I already downloaded log2ram, which is a solution that should greatly reduce SD card wear.
    cd /mnt/rootfs/home/pi (or wherever your SD card is mounted)
    git clone https://github.com/azlux/log2ram.git
    cd log2ram
    chmod +x install.sh
  8. Then I unmount the SD card, put it in the raspberry Pi and hooked it up to ethernet and power. After about a minute, I could log in (with some first-time SSH error messages)
    ssh pi@192.168.15.2 (with default password “raspberry”)
  9. First, do some mandatory maintenance:
    1. Change the password
      sudo passwd pi (and change the password to something to your liking)
    2. Install log2ram
      cd /home/pi/log2ram
      sudo ./install.sh
    3. Then reboot
      sudo reboot
      ssh pi@192.168.15.2
    4. and do an update:
      sudo apt-get update
      sudo apt-get upgrade
      sudo apt-get dist-upgrade
      sudo apt-get autoremove

Install an external harddrive, automount

  1. The external hard drive I got was a WD My Passport 5TB. It came with a preformatted NTFS partition. I changed this to EXT4 with fdisk:
    lsblk to see which device I just plugged in
    sudo fdisk /dev/sdX (change to the drive lsblk gave you)
    Then in fdisk:
    m (print menu)
    d (delete existing partition)
    g (create a new empty GPT partition table)
    n (add a new partition), then accept the default suggestions a few times
    w (write table to disk and exit)
    sudo mkfs.ext5 /dev/sdX1 (again, change to the partition lsblk gave you)
  2. Next step was to connect the external hard drive to the Raspberry and make it auto-mount.
    sudo mkdir /media/exthd
    touch /media/exthd/ "EXTERNAL DRIVE NOT MOUNTED"
    This is an empty file. If you see this file later, you know the problem is not in the network file system but in the drive not being mounted. If a drive gets mounted on /media/exthd, this file “disappears”.
    Then, add to /etc/fstab
    /dev/sda1 /media/exthd auto defaults,nofail,x-systemd.device-timeout=30 0 2
    The “nofail” makes the booting not fail when the external hard drive is not present, after 30 seconds. This will show up trough the file name EXTERNAL DRIVE NOT MOUNTED in the shared file.
    A quick reboot showed that indeed, the drive was automatically mounted.
    Then change the permissions on the drive
    sudo chmod 755 /media/exthd
    sudo chown pi:pi /media/exthd/

Install Network File System

Now to install NFS (serverside) – I mostly followed the Raspberry Pi NFS article.

  1. sudo apt-get install nfs-kernel-server
    sudo mkdir /exports /exports/exthd
    sudo chmod 777 /exports /exports/exthd

    To mount-bind /exports/exthd to /media/exthd:
    One-time (for testing purposes): mount --bind /home/users /export/users
    Permanently: add to /etc/fstab:
    /media/exthd    /exports/exthd  none    bind,nofail    0       0
  2. Add to /etc/exports
    /exports/exthd 192.168.15.0/24(rw,fsid=root,insecure,async,no_subtree_check)
    The fsid=root means that this directory (/exports/exthd) is the “root” of all exported NFS file systems.

    Then do a
    sudo exportfs -ra
    sudo systemctl restart nfs-kernel-server
  3. To install on your Linux computer (in my case, Kubuntu):
    apt-get install nfs-common
    To test: sudo mount -t nfs -o proto=tcp,port=2049 192.168.15.2:/ /mnt/nfshd/
    To do this permanently, add to /etc/fstab (this way, any user can mount)
    192.168.15.2:/ /mnt/nfshd nfs rw,relatime,user,noauto 0 0

Power management

The raspberry pi will be on 24/7, but I want the hard drive to spin down after a while. My usage scenario is a backup every few weeks, a movie maybe once or twice a week, and music for a few hours per day but sometimes not for days.
Luckily, a
sudo hdparm -I /dev/sda1
told me
Capabilities:
       Standby timer values: spec'd by Standard, with device specific minimum

I couldn’t find any specs what this Standard was, but then I left for lunch, found the disk still spinning after 20 minutes and not spinning after just over an hour. So I guess the drive spins down after either 30 or 60 minutes.

Conclusion

I started this article a long time ago, then got really busy at work with covid etcetera, and didn’t find the time+energy to work on this again. I finally did try this and this 3/4 written article really helped with the setup. Especially the “Basic install” section is useful if you want to do this more often and not spend much time in researching how to set everything up.

Regarding NFS: I found a lot of online guides, and a lot of them had unnecessary or even false steps (such as needing to change permissions on all files on the external hard drive – not necessary)

I skipped two elements of my goals here. I didn’t install Samba, as in my use case, I don’t need it. I have only linux/android devices and it seems they all work with NFS. The other is adding a physical power button, which I did add, but that’s a separate article.

Join the Conversation

5 Comments

    1. Hi Atul,
      Thank you for your interest!
      The SD card on this machine only hosts an operating system. A normal, portable Hard Drive is attached to act as storage. See the chapter “Install an external harddrive, automount”.

      1. Yes, I successfully made NAS out of a Raspberry Pi. It is power-efficient and everything under my control. Initially I though SD card will be slow, but it is not like that. Thanks for the guide.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.