My brother gifted me a Raspberry Pi 3 a couple of months back. After tinkering around with it for a few days, I kept it aside because I couldn't think of any use case for it that would benefit me everyday. This was until I learned about NAS and its technologies in one of my courses at college.
I found it really cool that you can have a Network Attached Storage to access the same files through any deivce on the network. This would allow me to finally have a centralized storage to host a media center and also store some static files and documents. Also, there is an added benefit that this data is always on my own network and can ensure a larger blanket of privacy around it. Ever since then, I have been playing with the idea of building a NAS using my pi. I managed to set it up in less than a day. And it's been a huge help at home.
I could gone with using OpenMediaVault ( a NAS solution based on Debian Linux) which would set up the NAS for me without me having to do all the heavy lifting. But, where's the fun in taht? So, I took out my black hoodie and did what every professional programmer does. I googled for answers to all my questions. And in a couple of hours, I was done with setting up my very own NAS using the Raspberry Pi.
Hardware That I Used:
- Raspberry Pi 3 model B.
- Ethernet Cord.
- One 500 GB Seagate External Hard disk.
- 16GB class 4 microSD card, loaded with Raspbian Buster Lite.
- Power Source.
If I had another spare external hard disk lying around, I would've tried to implement RAID in my NAS, making sure that I always have a backup of all my files. I implemented my NAS with just one hard disk.
I setup the raspberry pi and the NAS headlessly with my laptop and so used the lite version of raspbian.
Setting up the NAS
Step 1: Initial setup of Raspberry Pi.
I started with downloading and burning the raspbian buster lite OS to the microSD card. Since SSH is not enabled by default, I just created a file named "ssh" with no extension and copied it to the boot folder in the microSD. For those of you who do not know what ssh is- Secure SHell is a software package that enables a secure connection between the client and the server. This means that I am able to control the Pi using my windows machine over my network.
I connected the hard disk, ethernet cable and the power supply to the Pi to turn it on. To connect to the Pi via ssh, I found the IP address that my router had assigned to the Pi and connected to it using the command in the command prompt:
ssh pi@ip_address
After signing into the Pi using the default password of raspberry, I ran the update commands:
sudo apt-get update
sudo apt-get upgrate
These grab and install all the latest versions of the packages in raspbian, ensuring highest level of security available at that time.
Step 2: Install packages and setup NAS
NTFS Package:
While the Linux Kernel has some NTFS support, it is strictly read-only access. This means that we have to install a separate userspace driver to be ale to write to NTFS devices. I had formatted my hard disk to the NTFS format.
To enable the ntfs support by raspbian, I ran:
sudo apt-get install ntfs-3g
exFAT package:
I ran the command to install the exfat support packages as well. You know, just in case.
For this, I ran the command:
sudo apt-get install exfat-utils exfat-fuse
Samba Server:
Samba Server is a very powerful server that will help to share files between Linux on the Raspberry Pi and with a Windows OS system or any other system that is trying to access the files stored in our NAS. It is an open-source implementation of the Common Internet Files System/ Server Message Block (CIFS/ SMB) protocols.
To install samba:
sudo apt-get install samba samba-common-bin
Creating a directory:
The NAS contents have to be stored somewhere in the file system of raspbian. For this, I created a new directory named /PiServer.
To create the new directory in the root directory, run:
sudo mkdir /PiServer
When I ran the ls / command, I could see that it had been successfully created.
This folder is currently unaccessable to the public. Since I would be the only one accessing the NAS, I wanted to set it's access permissions to read, write and execute ( that's the significance of 777 ).
To change the access permissions of the directory, I used:
sudo chmod 777 /PiServer
Now, when I keyed in ls / again, the color of PiServer had changed to green, indicating that I have sucessfully changed its access permissions.
Mount Drive:
Now, I had to mount my drive to that directory. Only with this, would the file system know where and how my data is stored on the hard disk. To check the name and status of my hard disk, I used the lsblk command. Now that I had the name of my hard disk, I went ahead and mounted it to the Pi by using:
sudo mount /dev/sda1 /PiServer
When I keyed in the lsblk command in again, the hard disk's status showed me that it had sucessfully got mounted.
Configuring samba:
The next step for me was to configure the samba server.
Run command:
sudo nano /etc/samba/smb.conf
This opened a text document and I scrolled all the way down to the bottom of the file and appended it with:
[Home Disk]
comment = "Home Disk"
path = /PiServer
browsable = yes
read only = no
writeable = yes
create mask = 0777
directory mask=0777
public = no
force user=root
After this, Ctrl + O, Enter to save and Ctrl + X to exit.
Add New User and Restart samba
I added a new user to the raspberry pi with the commands:
sudo adduser user_name
sudo smbpasswd -a password_for_server
sudo /etc/init.d/smbd restart
sudo /etc/init.d/nmbd restart
Set auto mount instructions
If I ever do reboot the pi, then the hard disk will stay unmounted until I manually mount it again. Instead, I can ask the pi to mount the hard disk automatically, everytime that the pi reboots.
To do this, open a configuration file and add the second line to the end of the file.
sudo nano /etc/dhcpcd.conf
/dev/sda1 /PiServer auto defaults, user 0 2
Set a static IP
Everytime that the pi reboots, it's possible that my router would assign a different IP to it since it is currently being assigned an IP dynamically. Since the only way to access my NAS is through its IP address, I would have to check for the new IP address every now and then. To avoid this, I set the Raspberry Pi to have a static IP.
I did this by opening a configuration file and appending osme details to it. The static ip of the Pi can be set anything between 192.168.0.2 - 192.168.0.24
sudo nano /etc/dhcpcd.conf
static ip_address = Enter_ip
static routers = router_ip
static domain_name_servers = router_ip
This was the last step to setup the NAS
Accessing the NAS
Now all that was remaining was to access the NAS from any device on my network. A Windows PC would access the NAS by running the following in the run dialog box:
\\pi_static_ip
This would prompt me with the username and password. Once I was through that, I would be presented with the directories and its contents.
That brings this blog to an end...
Thank you for making it to the end of this blog. During the course of this project, I learnt about setting up my own NAS using the raspberry pi and that by including more than one disks, I can implement RAID for data backup. This was my first experience with samba and configuring the Pi to interface with windows.
I hope this would help you build a NAS using a Raspberry Pi of your own.
If you have any questions, feel free to contact me.
Have a great day!