How to Build a Raspberry Pi NAS Server in 2021
Backing up your documents, photos, and videos can often be a pain. My solution? A cheap Raspberry Pi combined with a couple USB hard drives. With a few hours of time you can have a sweet storage system that can be utilized by any device on your network, and backing up your data will be as easy as copying and pasting from one folder to another.
What you’re going to need:
- A Raspberry Pi (I used a Raspberry Pi 4 with 4GB of RAM).
- A minimum of 1 external hard drive, though two is better.
- A powered USB hub to power the hard drives.
- A power supply for the Raspberry Pi, along with a quality microSD card.
If you only plan on using one hard drive, you might be able to get away with plugging it directly into the Raspberry Pi itself, but a powered USB hub would be preferable to ensure that your hard drive is getting enough power. If you’re planning on using two drives (which you should; the rule when it comes to backups is “1 is none and 2 is 1”) then you definitely need a powered USB hub.
Note that this guide assumes you are using a hard drive with a base directory called “shares”. Feel free to rename the directory, or set your drives up differently, but pay extra attention to step 5 and step 9 to make sure you enter the correct directory.
Step 1: Load a Fresh OS Image
Go over to the Raspberry Pi website and download the latest Raspberry Pi OS. At the time of this writing the latest version is the December 2nd 2020 release.
One tip- if you don’t already have it installed, download Etcher, which makes flashing images onto an SD card a breeze. Open it up, select the OS image you just downloaded before, and then select your SD card. Click “Flash” and give it a minute or so. When it’s finished you can go ahead and close the program.
Step 2: Boot Your Raspberry Pi and Configure the OS
Go ahead and plug the Raspberry Pi in, and login as the “pi” user (the default password is “raspberry”).
Once you are in, we’re going to need to do some basic configuration. Enter
sudo raspi-config on the command line to open the configuration utility.
- Change the hostname (if you would like) in System Options -> Hostname.
- Change the password for the “pi” user in System Options -> Password.
- Expand the filesystem, to allow the Raspberry Pi access to the full amount of space available on your SD card, in Advanced Options -> Expand Filesystem.
Once you have finished with configuring the Raspberry Pi, select “Finish” and then reboot the Pi.
Step 3: Update and Upgrade Packages
When the Raspberry Pi has rebooted, log back in and run the following commands to update your package information and install any updates that have been released.
sudo apt-get update
sudo apt-get upgrade
Step 4: Install Samba
Now we are going to install samba on the Raspberry Pi. This will allow us to share files on the Pi with other devices on the network. Run the following command to install samba and it’s necessary dependencies:
sudo apt-get install samba samba-common-bin
Step 5: Configure Samba
Next, we need to configure our samba installation. This is a two-step process. First, we need to set our security to user-level, which means that people who want to access our drive will need to supply a username and password. Second, we need to define the specifics of our samba share.
Open up the samba config file located at /etc/samba/smb.conf with the editor of your choice. We are going to add the following line to the authentication section:
security = user
Note that whitespace and capitalization do not matter in the smb.conf file. I’d recommend matching the existing style choices in the config file.
Now head to the very bottom of the file so we can define our samba share settings. Copy and paste the following into your smb.conf file:
[backupserver]
path = /media/Drive1/shares
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no
Whatever you include in the square brackets is the drive name that will be displayed on other computers, so if you wanted it to be called “Jimmy”, then you would put [Jimmy] instead of [backupserver].
The valid group = @users line means that any user that is a part of the “users” group will be allowed to access this share. The force group = users line ensures that the group attributes of files created in the share match the “users” group. The create mask and directory mask are setting default permissions for new files and directories.
When you have finished adding that to your smb.conf file, save it and then restart samba by running this command:
sudo /etc/init.d/smbd restart
Step 6: Add Samba Users
At this point we need to add the users who we would like to access our share. The “pi” user will be a good start. Run the following command to set the password you want to use with the “pi” user when connecting to the samba share from another computer:
sudo smbpasswd -a pi
If you would like to add other users, run these commands:
sudo useradd <username> -m -G users
sudo passwd <username>
sudo smbpasswd -a <username>
The first line creates a new Unix user, creates a home directory for it, and adds it to the group “users”. The second line sets the Unix password for this user. This is the password you would use if you were trying to ssh into the Raspberry Pi as this user. The third line sets the samba password for this user, which is what you would use when trying to access this share from another computer.
Step 7: Create Mounting Points for Your Drives
We are going to want to make a directory for each of our drives. I would advise you to do something simple like “Drive1” and “Drive2”, though it is up to you (just make sure to use the correct directory in step 8 if you choose something different). Use the mkdir command to create the directories in /media, like so:
sudo mkdir /media/Drive1
sudo mkdir /media/Drive2
Step 8: Connect the Hard Drives
I would recommend only plugging in one drive at first, and then when you’re finished with that one plug in the second (if you’re using one) and repeat the same process.
Once you have connected the drive, run this fdisk command to determine the name of the partition we are looking for:
sudo fdisk -l
Look for your drive in the output, it should be listed as something like /dev/sda. Then look at the partitions contained within that drive. If your drive was mounted on /dev/sda, then the partition we want is likely going to be /dev/sda1 or something along those lines.
When you have identified the partition’s name, open /etc/fstab and add this line so that the partition will be mounted automatically when the Pi reboots:
/dev/sda1 /media/Drive1 auto noatime 0 0
This tells the Raspberry Pi to mount the /dev/sda1 partition in the /media/Drive1 directory. The “auto” part tells the Pi to automatically figure out the format of the drive. The “noatime” part prevents the OS from updating the atime information on a file that was strictly being read and not written, saving system resources. Finally the two 0s at the end of the line tell the OS not to “dump” the drive if the system crashes and not to perform a file system check on the drive, since we are only using this drive to store files.
Once you’ve added the line to /etc/fstab, you can run mount -a -v to verify that everything worked correctly. If you messed up the formatting and the drive does not mount, you will see this in the output.
Step 9: Mirror Your Drives
If you are using two hard drives for this project, you’re going to want a way to automatically make sure that the second drive is a mirror of the first. To do this, we are going to set up a nightly cron to copy over any new/changed files from the first drive to the second.
To create a new cron, we are going to need to make an entry in the crontab. Enter crontab -e on the command line, and add the following line:
0 3 * * * rsync -cav /media/Drive1/shares/ /media/Drive2/shares/
What this line does is use rsync to recursively copy over any files that do not have matching checksums from the shares directory on Drive1 to the shares directory on Drive2 at 3am every day.
Step 10: Connect to Your NAS
Okay, we’ve reached the last step. If you’ve made it this far, congratulations! Now is the moment of truth, let’s try to connect to the NAS from another computer.
If you are on a Windows computer, go to your file explorer, click on “This PC”, and then choose “Map Network Drive” from the top menu.
In the “Folder” field in the popup, enter the following:
\\<Raspberry-Pi-IP>\<Share-Name>
where the Raspberry-Pi-IP is the IP of your Pi and Share-Name is the name you put inside the square brackets for your samba share definition.
Make sure the “Reconnect at sign-in” and “Connect using different credentials” checkboxes are both checked, then click Finish. When you get a popup asking for a username and password, use one of the samba users you set up before (“pi”, or another user if you set any others up).
You should now see your drive in your file browser on the left hand side with the drive letter you chose for it before.
That’s a Wrap!
Now you’re done, and you have a fully functioning NAS server that you can access from any computer on your local network.
To save something to the drive, just copy it and then navigate to your drive and paste it. It’s that simple! You can reverse those directions to copy something off of the shared drive and onto your computer as well.
I hope you found this guide helpful; feel free to leave a comment with any questions you may have and I will try my best to answer them!