How To Set Up A Raspberry Pi NAS

Get posts like this one in your inbox by signing up for our newsletter.

Because why buy something meant for the job when you can just throw a bunch of other stuff and hopefully get a working thing? A Raspberry Pi NAS is affordable and easy to set up, all you need is a Raspberry Pi and some digital storage.

Installing Ubuntu Server

The first thing you’ll need to do is install an OS onto your Raspberry Pi. Although the default Raspbian OS will get the job done, I decided to go with Ubuntu Server because it’s more suited for being, well, a server. However, if you want to stick with Raspbian, you should still be able to follow this tutorial as both Ubuntu Server and Raspbian are based off of Debian, so both will use the same package manager ( apt) among other similarities. Anyways, installing an OS on a Raspberry Pi is pretty simple: you just need to download the image, flash it onto the micro SD card, and then put it in your Raspberry Pi and boot.

To get the Ubuntu Server image, you can visit this page, and click on “Ubuntu Server image for Raspberry Pi [model]”. You’ll then need to extract the image, which can be done on Ubuntu by running:

unxz [image name].xz

If you’re on Windows, I believe you’ll need to download a third-party tool to extract xz files. Once you’ve extracted the image, you can use dd on Linux or Mac:

sudo dd if=[image name].img of=/dev/sdc status=progress

In this case, my micro SD card was mounted on /dev/sdc, but it will likely be different for you. You can find what your micro SD card is called by running:

sudo fdisk -l

For Windows, you’ll need to use a third-party tool to get the img file onto the micro SD card.

Once the image is on the Raspberry Pi’s micro SD card, jut pop it into your Raspberry Pi, then connect it to power and ethernet.

Accessing Your Raspberry Pi

Since I’m too lazy to connect my keyboard and display to the Raspberry Pi, I’ll be doing all of the setup via SSH. To do this, simply find the local IP of your Raspberry Pi (I use a program called Angry IP Scanner), then SSH into it:

ssh ubuntu@[IP address]

The default password is ubuntu (you won’t see anything happening when you type in the password), and upon logging in you’ll be prompted to update it. Once you’ve done that, you’ll get kicked out of the server for some reason. After you ssh back in, it’s good practice to update everything, which can be done with:

sudo apt update
sudo apt upgrade

Type y and hit enter if prompted.

Configuring RAID 5

Since I have three disks, I’ll be using RAID 5 because it offers redundancy and more storage available than RAID 1. If you have two disks, then just use RAID 1 which will mirror everything across the drives. To configure this, we’ll be using a tool called mdadm, which you can install with:

sudo apt install -y mdadm

At this point, you can plug in your disks (it doesn’t matter if you’ve plugged them in already) and create the array (use sudo fdisk -l to get your disk labels):

sudo mdadm --create /dev/md0 --level 5 --raid-devices 3 /dev/sda /dev/sdb /dev/sdc

Type yes and enter if prompted. You can then use the following command to see the creation progress:

watch -n 1 sudo cat /proc/mdstat

Encryption

After your RAID array is done setting itself up, it’s time to configure encryption. For this, we’ll be using LUKS to encrypt the entire volume, although you can also opt to use ecryptfs on a per-folder basis. The latter approach will also let you use the rest of the volume for something else that may not require encryption. However, because encrypting everything sounds a bit safer to me, that’s the route I’m going to go.

First, we’ll install the encryption utility we need:

sudo apt install -y cryptsetup

Now, here’s the fun part:

sudo cryptsetup -y -v luksFormat /dev/md0

You’ll be asked if you want to continue:

WARNING!
========
This will overwrite data on /dev/md0 irrevocably.
Are you sure? (Type uppercase yes):

Just type YES and hit enter. You’ll now be asked for a password and to verify your password. As usual on Linux, you won’t see your password as you’re typing it, so just hit enter once you’re done typing.

Pro tip: If you’re on Ubuntu, or probably most Linux distros, you can paste your password into the terminal by using ctrl + shift + v.

Mounting

To mount your disk, which you’ll have to do after every reboot, simply issue the following commands:

sudo mkdir -p /mnt/storage
sudo cryptsetup luksOpen /dev/md0 storage
sudo mount /dev/mapper/storage /mnt/storage

You can change /mnt/storage to whatever directory you want, as well as storage on line 2 (you’ll have to update /dev/mapper/storage as well).

Note: Upon rebooting, my Raspberry Pi seems to move /dev/md0 to /dev/md127

First Mount

If you run the above commands, you’ll notice that the last line gives you a mount error. This is because it’s not formatted yet. To do so, we’ll use the mkfs command to format the volume as ext4:

sudo mkfs.ext4 /dev/mapper/storage

You can now mount the volume, and finally set up samba!

Samba

Once you’ve mounted your encrypted volume, you can now set up samba, which will allow you to access the files on the volume from any computer on your network. For this, we’ll install a samba server, update it’s configuration file, then add a user.

Install the Samba Server

sudo apt install -y samba

Configuration

The samba configuration file can be found at /etc/samba/smbd.conf. My favorite in-terminal editor to use is nano, which comes pre-installed:

sudo nano /etc/samba/smb.conf

Scroll to the bottom of the file (using the arrow keys), and add the following:

[share name]
comment = My Raspberry Pi NAS
path = /mnt/storage/nerdoflinux
guest ok = no
browseable = yes
create mask = 0600
directory mask = 0700
valid users = nerdoflinux
writeable = yes

To save and exit, press ctrl + x followed by y.

If you want a trash folder, add the following:

vfs objects = recycle
recycle:repository = .recycle
recycle:keeptree = yes
recycle:versions = yes

Now, everything you delete should make its way to the directory you specify instead of actually being deleted.

Add A User To Your Raspberry Pi NAS

Now, add a user (replace nerdoflinux with your desired username):

sudo adduser nerdoflinux --disabled-password --no-create-home

Just press enter over and over until the user is created. Now, add the user to samba:

sudo smbpasswd -a nerdoflinux

And type in your desired samba password, then press enter.

Lastly, create the samba folder (replace nerdoflinux with your samba user):

sudo mkdir -p /mnt/storage/nerdoflinux
sudo chown nerdoflinux:nerdoflinux /mnt/storage/nerdoflinux

and restart samba:

sudo smbd restart

Connecting

You should now be able to connect to your Raspberry Pi NAS just like you would any other NAS. Open up a file manager with samba support, enter your Raspberry Pi’s IP address as the host, then log in to start storing files.

Sources: