How to transform Raspberry to a NAS Pi
A good execise for to play and to learn with our Raspberry can be to use it as a network disk or NAS. Albeit nowadays it may seem an useless thing, since the most modern modem routers are equipped with this functionality, we can learn a lot about its use not only as a consumer but also as a slightly more experienced user. Let’s find out how to turn the Raspberry into a NAS.
How to turn the Raspberry to NAS
Before starting to deal with the topic of the article I suggest you do a quick review on how to start and basic commands for Raspberry. For those who don’t know what is a NAS or what it is used for, the meaning of the acronym and its translation provide us with a good clue: Network Attached Storage, basically a hard disk connected to the network. The usefulness of this tool in this configuration is given by the low energy consumption, by the low noise level and above all by the fact that it gives the possibility to have a disk shared among all your devices in the house.
HD preparation
First of all we should choose the size of our hard disk: it is advisable to use a very large hard disk for this use of the Raspberry and it must be connected via USB (for disks from 3,5’’ must be used an external power supply).
Formatting
Once we have purchased the right HD for our needs, let’s proceed to format it. In the best format for our purpose by choosing between two possible configurations: NTFS or EXT; The first configuration, that is NTFS, it allows us to use the hard disk even on our Windows PCs simply by connecting it but it can significantly slow down the performance of our Pi and also does not allow us to use permissions on folders. Not only for the reasons listed above we are going to format, directly from our Raspberry Pi the hard disk in ext4 mode, using the terminal commands as a practical exercise of the Raspbian shell.
sudo fdisk -l
sudo mkfs.ext4 /dev/sda1 -L etichetta
sudo mkdir /media/nas
sudo chown -R pi:pi /media/nas
sudo chmod 777 /media/nas
sudo mount /dev/sda1 /media/nas
cd /media/nas
ls
After connecting the USB disk to the Raspberry, making sure that the disk is powered, type the command in the terminal to see the list of available hard drives (row 1). The first two “dev” on the list, marked with “mmcblk0p” are the two partitions of our SD card containing the operating system, while the “dev” at the bottom of the list is our USB Hard Drive which in our case will be indicated as sda1, sda2.. o sdb1… o sdc1… ecc… after identifying the name of our USB Disk (sda1 es.), we format it with the command line 2 you can replace the word “label” with a name of your choice.
At this point we proceed with the “mounting” of the disk by creating a folder named “NAS” (or with another name of your choice) inside the already existing “media” folder on which to mount the Hard Disk.In this way, thanks to the name “NAS” we will remember that the contents of our USB disk will be attached to that folder. To create the folder we type the command line 3.
Nextly then let’s assign write permissions to the user “pi” for the “nas” folder with lines 4-5; we will now be able to “mount” the usb disk through the command line 6. To verify that all are successful, go to the “/ media / nas” folder with the commands on line 6-7. The result should show you a single folder called “lost + found” which is a “service” folder used by the operating system.
Assembly of HD to start
The last step will be to make the USB drive mounting automatically when the system starts up; for this operation we are going to edit the fstab file, with the default editor of Raspbian Nano, then we give the command:
sudo nano /etc/fstab
Once the editor is open we will add the following line at the bottom of the file
/dev/sda1 /media/nas ext4 defaults 0 0
To move within the file use the directional arrows and instead of the space between one string and the other use the Tab key. To save the file you will need to use the combo ctrl + x and type Y and push the key “enter”. You can also avoid editing the fstab file and manually mount the disk at each boot. It is a good rule to connect and turn on the hard disk first and then the Raspberry.
File sharing system and Samba printer
To access our Hard Disk remotely we will use the Samba cross-platform program that allows us to access files and printers remotely. To proceed with the installation of Samba, let’s first update the repository list and then give the installation command:
sudo apt-get update
sudo apt-get install samba samba-common-bin -y
Once the installation is finished we will modify the Samba configuration file to allow the program to read our hard disk. To do this we will first move to the Samba directory line 1; then we will make a safety backup of the configuration file line 2; at this point we can easily edit the same with the command line 3
cd /etc/samba
sudo cp smb.conf smb.conf.orig
sudo nano smb.conf
Inside the configuration file we can activate deactivate various available options of the program, uncommenting them, and for our purpose it will be enough only for the Security directive; we will add at the bottom of the file a small piece of code that allows Samba to create a writable resource called USB that refers to the path of our hard disk; after completing the changes ctrx + x to save.
# Direttiva commentata, quindi disattivata!
# security = user
# Eliminare il cancelletto per attivare la direttiva!
security = user
[usb]
comment = Il mio nuovo NAS
path = /media/nas
writeable = yes
The last step is to create a password for the Pi user
sudo smbpasswd -a pi
sudo service samba restart
Obviously we should type the password twice for security purposes, at the end we restart the samba service. Now that we know how to turn the Raspberry into a NAS, let’s enjoy it.