In this post, you will learn how to build your own Raspberry Pi NAS (Network Attached Storage).
What do you need?
- Raspberry Pi
- MicroSD card for your Raspberry Pi
- External USB drive (SSD / HDD)
- UTP cable (or you connect your Raspberry Pi to your wireless network)
1. Prepare your Raspberry Pi
Download Raspberry Pi OS Lite and burn it to your microSD card. Before booting your Raspberry Pi, make sure you enable SSH and add a WiFi configuration to your device. Check out this post if you need any help with this:
Once your device has booted and you are connected to it, you can change the hostname. For example nas
, so the network address is nas.local
. Finally, make sure everything is up-to-date with
sudo apt update
sudo apt -y upgrade
After running this commands, reboot your Raspberry Pi.
2. Add your USB drive
Connect your external USB drive. After connecting the drive enter the following command:
lsblk
This command tells you about all the connected devices to your Raspberry Pi. Normally your disk is called 'sda'.
3. Prepare the USB drive
The next step is to partition the drive, so Raspbian can understand how to store data on it. To do this we use fdisk
.
sudo fdisk /dev/sda
After running the command above, enter n
for creating a new partition. If you get an error that a partition already exists, use d
to delete it (this will lose any data on the disk). Then enter p
(primary partition). After that you will be asked a series of questions about sectors. Just press ENTER (accept default) until Created a new partition appears. Now type w
to write the changes to the disk.
4. Mount the drive
In this step you will mount the drive to the system.
sudo mkdir -p /nas
sudo mkfs.ext4 /dev/md0
sudo mount /dev/md0 /nas
To make sure the drive is mounted whenever the system boot. We will add a line to the file /etc/fstab
sudo nano /etc/fstab
/dev/md0 /nas ext4 defaults,noatime 0 1
5. Setting up the Samba
Now we will setup Samba, a protocol for sharing files over the network. Samba is not installed by defaults, so we need to install it manually. This can be done with running the following commands:
sudo apt install samba samba-common-bin
If you are asked any questions during the install, just select the default answer. Now make a shared directory and allow all users to access it.
sudo mkdir /nas/shared
sudo chmod -R 777 /nas/shared
Tell Samba to share the directory on the network by editing the Samba config file (/etc/samba/smb.conf
).
sudo nano /etc/samba/smb.conf
[shared]
path=/nas/shared
writeable=yes
create mask=0777
directory mask=0777
public=no
Save the file (ctrl+x
, followed by y
), then restart Samba:
sudo systemctl restart smbd
6. Granting user access
To give a user access to the shared files, we need to run a Samba command. To grant access to the current user 'pi' we run:
sudo smbpasswd -a pi
You will be asked to choose a password. Once done the 'pi' user can access the Samba share from Windows, macOS or any other device with the ability to read and write files.
If you want to create additional users, run the following commands:
# username is your choice of username ;)
sudo adduser username
sudo smbpasswd -a username
7. Other Samba setups
With Samba you can do a lot more than just sharing a folder. If you want to learn more, check out this website.
Happy backing up! 👨💻