Creating a Raspberry Pi NAS
This is a project I've had on my "want to try" list for a long time, and I finally made it happen.
Let's start at the beginning, as I'm sure many of you have questions. The very title of this article is a bit dense, and requires explanation, which I will give you forthwith.
First of all, what is a NAS? #
NAS stands for "Network Attached Storage". This is a fancy word that means "an external hard drive that you're able to access over your network, rather than plugging it in via USB".
If you're not familiar with external hard drives, they look like this:
[pic of external hard drive]
I have always been a fan of these simple devices. I have five or six of them that I use for backups (and backups of backups), but it's difficult juggling all of these hard drives. It's great to have so many copies of my important files, just in case one copy goes bad. But it is a hassle, especially when you have to physically plug all of these drives into your computer to access them.
A NAS fixes this problem. A NAS allows you to plug your hard drives into it, and then you can access those drives at any time over your network. This allows you to seamlessly manage and backup your files at any time.
This also allows you to automate backups. Since your external storage is available to your computer at all times, you can use software to automatically connect to your NAS and backup your important files as often as you need. On Macs this is almost trivial using Time Machine, which we will see in a bit.
What is a Rasperry Pi? #
A Rasperry Pi is a tiny computer, not much larger than a credit card. I've been following the Raspberry Pi project since it began, back in 2011 (my goodness where does the time go?), but I've never had a really useful project for one before. I'm the sort of person who needs to have a project in order to justify an expense, so I never purchased my own Pi.
That changed a few months ago, when I decided I needed to find a new-and-improved way to handle my backups. I've never irrecoverably lost data before, fortunately, but I've also never had a storage device fail, and I'm due for it. I have hard drives dating back to 2011 that I'm still using, and that's a recipe for disaster.
So I decided I needed a NAS, some sort of networked device that could handle backups automatically for me. I started looking into commercial NAS solutions, and I was surprised at what I found.
(Warning: tin foil hat moment coming up!)
Every commercial NAS I looked at ran proprietary closed-source software. Most of them were big and clunky and loud, and most don't even come with storage devices. You end up having to pay for $300USD for a black box that runs secret magical software, and then you'll need hundreds of dollars worth of hard drives to go with it.
This was not acceptable to me. A NAS is by nature a somewhat dangerous thing to have lying around: it has all of your data on it. Anyone with access to your home can have access to everything by unplugging your NAS. In addition, if it's running closed source software, you have no idea who else might be snooping around in your data. If your NAS receives software updates (which it should), then this could change at any time. Even if it's safe right now, you never know when that company could have a management change and decide that it's all of a sudden okay with harvesting user's data at their own expense.
This is why I decided to go with a Raspberry Pi. Not only is it relatively cheap (I spent about $100 on the Pi and $100 on 4TB of storage), but it also runs only the software I decide to install and use. Still not 100% safe, because I'm still relying on third party software, but certainly safer than a black box.
So there's the theory, how do we actually do this in practice?
The hardware #
For this project you need a few things. I ended up "splurging" and buying a kit to simplify this process, but you don't have to do that if you don't want to. You could make this project even cheaper if you are willing to piece together your own Raspberry Pi kit. For prosperity though, here are the two things I ended up ordering (affiliate links):
- Seagate Expansion Portable 4TB External HDD ($95 at time of writing)
- CanaKit Raspberry Pi 4GB Starter Kit ($99 at time of writing)
The CanaKit starter kit is extremely good if you're a beginner to the Raspberry Pi, as I am. It comes with clear instructions on setup, and everything you need to get up and running with your Pi. We'll go through the setup here in case you don't get the kit, but I recommend it for beginners. It made me much more confident in what I was doing.
In addition to the above, you'll need an external display (a TV or a computer monitor), and a usb-compatible keyboard and mouse.
Setting up your Pi #
The CanaKit starter pack comes with NOOBS—New Out Of Box Software—pre-installed on the micro SD card. This allows you to install an operating system and get started very quickly. If you decided to DIY, you can format your SD card and install NOOBS here. This makes it much easier to get started.
It's also a good idea to assemble the hardware of your Pi at this point. This means installing it inside its case if you have one, adding any heatsinks you may have, installing a fan if you have one, all of that jazz. The kit above comes with heatsinks and a fan, which are very helpful to keep your Pi working at peak capacity, but not necessarily required for this project.
Once the hardware is ready, it's time to install the operating system. Here are the steps I followed:
- Insert MicroSD card into the Pi.
- Connect keyboard and mouse via USB ports.
- Connect to computer monitor or TV via HDMI cord (use the port closest to the UCB-C power port)
- Plug your Pi in! If you have a power switch, turn it on.
Once this is done, your Pi should boot up and show you a menu that will allow you to install an operating system. I went with the default, Raspbian, which is a version of Linux built specifically for the Pi. If you plug your Pi into your internet, via Ethernet, NOOBS will give you more operating systems to choose from.
That should be all you have to do! NOOBS will install your chosen operating system and reboot. After it reboots, it will fire up your new OS.
Next step: Making your Pi network accessible #
Setting up your user #
Once our operating system is up and running, we need to create a new and secure user for our OS. By default, the administrator is called "pi" and the password is "raspberry", which is a very insecure setup. We want to create our own super user, and then effectively disable the default.
Open up a terminal (top left of your screen on Raspbian), and type the following command (replacing "tim" with your desired username):
sudo /usr/sbin/useradd --groups sudo -m tim
That will create a superuser account for you. Next, you need to set up a password. You can do that like this:
sudo passwd tim
You will be prompted to enter a password, preferably something strong and difficult to guess. Enter and then re-enter to confirm (it won't show anything as you type, so type confidently).
Once you have your new user set up, then you can disable the "pi" user by running this command:
sudo passwd -l pi
Enabling SSH #
Now that users are taken care of, we need to make the Pi accessible via SSH. This will allow you to log into your NAS from other computers on the same network, which is useful for all sorts of things.
A good way to do this is to use the
raspi-config utility. This utility allows you to edit all kinds of system settings, here's how you can use it to enable SSH:
sudo raspi-configin your terminal wendow
Okay, and then
Just like that, SSH is now enabled on your machine. It's worth poking around
raspi-config too, just so you gain familiarity with it. It's a useful tool.
Giving your Pi a fixed IP #
There's one more thing we need to do in order to consistently SSH into our Pi. We need to give it a fixed IP. Otherwise the IP of our machine is likely to change any time we boot it up, which makes it difficult to access remotely.
First we need to gather some information. Start by running this command in your terminal:
ip r | grep default
This should return something like this:
default via 192.168.0.1 dev eth0 src 192.168.0.4 metric 202
default via 192.168.0.1 dev wlan0 proto dhcp src 192.168.0.5 metric 303
You may only have one entry. That's fine, we're mainly interested in the first IP address, in this case
192.168.0.1. That's the IP address for our router.
Also take note of "eth0" (Ethernet) and "wlan0" (WiFi). I have both wifi and ethernet working, so I can use either, but you may only have one choice. If you have Ethernet available, I would recommend using it, because it will likely be faster.
Next, we also need to get the current DNS server. We can do this by opening up the "resolve.conf" file by running this command:
sudo nano /etc/resolv.conf
This should open a file that shows something like this:
# Generated by resolvconf
Again, make a note of that first IP address. Then press CTRL + X to exit the file. That should give us all the info we need.
Next we need to edit this file:
sudo nano /etc/dhcpcd.conf
Use the arrow keys to scroll down the file, and eventually you'll see something that says "Example static IP configuration", which is exactly what we want. Below it, you'll see something like this:
The first thing you need to edit is
<NETWORK>. This will need to be either "eth0" or "wlan0", depending on which option you chose earlier.
<STATICIP> should be whatever you want your static IP to be. I chose
192.168.0.195, mainly because it's unlikely to conflict with anything.
<ROUTERIP> should be the first IP you recorded above.
<DNSIP> you can use second IP you got above, or you can use another DNS provider such as Cloudflare's 126.96.36.199 or Google's 188.8.131.52. All in all, your file should look something like this:
# Example static IP configuration:
Then press Ctrl + X and then Y followed by enter to save.
After all of that, reboot your pi and it should have your chosen IP address! You can check by running
SSH into your Pi #
Now that we have all of that set up, we can disconnect our Pi from our keyboard and monitor and login from an external computer if we want. Mac and Linux already have this built in, you should be able to run
ssh 192.168.0.195 (or whatever your chosen IP was), enter your password, and you'll be off to the races! The remainder of these steps can be performed either through SSH or directly on the Pi.
Setting up storage #
This is a marathon, not a sprint! We have most of the basic setup done. Now we just need to add our storage.
We have a couple of options when it comes to file transfer. I am on a Mac and would like to use Apple's Time Machine, so I'm going to use AFP (Apple File Protocol). But you could just as easily use Samba shares. Either one works on Macs, but Samba is a better choice if you'll be connecting with Windows.
Before we install AFP, we should update our Pi by running these two commands:
sudo apt update && sudo apt upgrade
Now that everything is up to date, we can install Netatalk. Netatalk is a free and open source implementation of AFP. To install:
sudo apt install netatalk
Once that completes, we need to configure it to share directories. To do this, we modify this file:
sudo nano /etc/netatalk/afp.conf
This article is not quite finished yet! This is a big process. Check back soon for more...
- Add new article about my NAS