Create a NAS using Raspbian PIXEL

Alexandru Calcatinge bio photo By Alexandru Calcatinge

Introduction

Using the RPi3 as a home NAS is a good choice nowadays, due to the low price of RPi3 and external drives options. Some time ago I have purchased a Lenovo BEACON Personal Cloud Storage Device to use as a NAS, but it is quite old and has an EOL version of Fedora installed on it that can’t be upgraded.

Therefore, if you have a RPi3 you can use it for creating a local NAS that has updated repositories and the PIXEL Linux Operating System on it. All you need is at least one external Hard Drive (or USB Stick) to use with your setup, or two if you want redundancy. Therefore, if you have those, you can start creating your own NAS.

First of all, you should take into consideration what kind of operating systems are used on computers from your local network. For example, in my case, I use only Linux systems, but my wife is using Windows on her computer, along with Android on our phones and tablets. Therefore, the local hard drive should be easily accessible from all the systems we use. In this respect, I will format the External Drive using the NTFS file system.

Secondly, you should take into consideration the limitation that the RPi3 has with regard to USB 2.0 speed transfers and energy supply for external Hard Drives. If you need a lot of available space, you will surely use an external Hard Drive that has 500 GB or 1.0 TB available, but with this kind of configuration you should take into consideration the amount of energy that the External Hard Drive is using.

If available space is not that important, you can use an external USB memory stick that has 32 or 64 GB (those are also fairly priced too). You could use two or even three or four of this type. I have found that I have four 32 GB memory sticks that were sitting unused somewhere in my desk, so in my case, I will use one or two of these, not an external HDD. 32 GB is enough for my home NAS, as I am not storing any large files like movies or music, only small/regular files I and my wife use for work, as text files, spread sheets, some images, pdf files and some small projects I do in Python. And I don’t need redundancy as I can easily manually backup the files to a larger external HDD once a week/month.

Having this cleared up, we have to connect the External Drive to the RPi3 and then power it up. Usually my RPi3 is set to start in GUI, but when you use it as a NAS, you don’t need the GUI, as it takes a larger amount of your available RAM, so you can use it in CLI mode.

NOTE! Keep in mind that PIXEL doesn’t come with vim preinstalled, so if you like that particular text editor, like I do, you should install it first before getting into editing configuration files. You could also use nano, but I like vim more.

So, before anything else, use the command:

sudo apt install vim

to install the vim text editor on your system.

Install the NTFS utilities

Therefore, as PIXEL starts, open the terminal and you are good to go. First thing we need to do is to install the ntfs-3g package for supporting the NTFS file system on the External Drive.

sudo apt install ntfs-3g

Once the files are installed, you can check your external drives by using the command:

sudo fdisk -l

The output will give you a list of the drives available. Bare in mind that one of them is the microSD card that PIXEL runs from on the RPi3 - so this one should be left untouched. The other is sda1 which is the 32 GB memory sticks we used.

Partition and format the external drives

We will have to partition and format the memory sticks in NTFS format using the fdisk and mkfs.ntfs utilities. This will be done for both external drives.

sudo su 
umount /dev/sda1
fdisk /dev/sda

Options to select:

‘d’ to delete the partition
‘n’ to create a new partition
‘p’ for primary
‘1’ for partition number
‘Enter’ for first cylinder (default 1)
‘Enter’ for last cylinder (default of max size)
‘t’ for type
‘L’ to list codes, and enter code for HPFS/NTFS. In my case, it’s ‘7’
‘w’ to write changes to disk, and exit

Then you do:

umount /dev/sda1

You will have to use the option ‘n’ to create a new partition an follow the steps on the screen. At the end, you should write ‘w’ the changes to disk and exit. The last step is to unmount using the umount command.

Next step is to format the partitions to NTFS file system. This will be done by using the mkfs.ntfs utility. You can do a full format, or a quick format, just as you would do using the GUI.

First you should overwrite your existing data on the external drive with zeros by using the command:

sudo su 
shred -vzn 0 /dev/sda

This will override all the blocks of the hard drive with zeros. Using 0 (zero) as a parameter is the “fast” method. As you increase the number of iterations, for example 3 or 4 instead of 0, the longer the process will take. Bare in mind that a full format option could take up to a few hours to complete.

And then execute a quick format to NTFS file system:

sudo su 
mkfs.ntfs /dev/sda1

Mount the external drives

After partitioning and formatting the external drives, we now have to mount them locally. In order to do this we will first create two new directories that will be used to mount the drives. We will use the following commands:

sudo mkdir -p /home/NAS/backup

Usually the external drives are mounted in /media directory, but you can use whatever directory you can find appropriate. For convenience I used /home/NAS/. Now we will mount the external drive to the newly created directories:

sudo mount -t auto /dev/sda1 /home/NAS/backup

Create some sub-directories

Now that the two external drives are mounted to the two directories, you can use the directories as they are, or you can create new ‘shared’ directories inside. Just for consistency, we could create a new directory structure inside each of the backup directories that could look like this:

sudo mkdir /home/NAS/backup/alex
sudo mkdir /home/NAS/backup/rodi

Installing SAMBA

At this point, it is easy for me, as I’m only using Linux systems, to access the files. In order for my wife to have access from her Windows system, I have to install Samba. This is easily done by using the command:

sudo apt install samba

Adding new users to access Samba

Now we need to create new users in order to access the samba directories. This is useful for the Windows system. As you noticed in the above commands, we made the "home_backup" directory available for users inside the "users" group. Thus, the new user we create will part of the "users" group.

In my case, I will use two names for the user: “alex” and “rodi”, so the commands will be:

sudo useradd alex -m -G users
sudo useradd rodi -m -G users

Now set a password for the new users:

sudo passwd alex
sudo passwd rodi

Enter the password at the prompt and press Enter. You will be asked to retype the password. Do that. The next step is to make the newly added user a genuine Samba one by using the command:

sudo smbpasswd -L -a alex
sudo smbpasswd -L -a rodi

Enter the password for the new user.

sudo smbpasswd -L -e alex
sudo smbpasswd -L -e rodi

This will let you use the new system user as a Samba user, by enabling it.

Configuring the /etc/fstab file

In order for the RPi3 to automatically mount the external drives at start up, we need to edit the /etc/fstab file:

sudo vim /etc/fstab

Inside the file, add the following line:

/dev/sda1  /home/NAS/backup  auto defaults,nofail,noatime 0 0

Save the file and exit.

Setting up Samba

Right after installation, first thing that we have to do is to start editing the Samba configuration file. Before starting editing, it is wise to create a copy of the configuration file by using the command:

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

To start editing the Samba configuration file use the command:

sudo vim /etc/samba/smb.conf

While inside the configuration file, first you have to look for the workgroup string.

Usually this is set to WORKGROUP in order to work with the Windows environments. Secondly, we have to set up user authentication for samba. For this we will have to get to the Authentication section of the file.

Remove the comment from the line where it says "security = user".

If there is no reference to it, just add a new line in the Authentication section. Furthermore we will create a new section in the configuration file where we will “create” the backup directory options. For example, we will create a directory called “home_backup” and for this we will create the following lines:

[home_backup_alex]

comment = Backup Directory

path = /home/NAS/backup/alex

valid users = alex

force group = users

create mask = 0660

directory mask = 0771

read only = no

writeable = yes

Now for the second directory:

[home_backup_rodi]

comment = Backup Directory

path = /home/NAS/backup/rodi

valid users = rodi

force group = users

create mask = 0660

directory mask = 0771

read only = no

writeable = yes

Next step is to restart the Samba daemon by using the command:

sudo /etc/init.d/samba restart

To test the configuration, you should run the command:

testparm

You can now test if everything works as planned by doing the following: for Linux System (openSUSE) In a Linux based system, like in my case openSUSE Leap, go to Nautilus and in the location bar enter the following smb://<IPAddress_of_your_RPi3>/<Host directory>

In my case this translates into:

smb://192.168.0.17/home_backup_alex

#or

smb://192.168.0.17/home_backup_rodi

The next step is to enter your credentials for the samba user and password. Enter the credentials of the samba user to connect to the shared folder. To see if everything works fine, we will create a new empty txt file in the shared directory. Right click in Nautilus in the files/directories area and select "Open in Terminal". Into the terminal, use the command

touch test_file.txt

to create a new empty test file. This will show up in Nautilus and in the output of the ls -l command. Open the location in Terminal and use the touch command to create a new empty txt file. The new file will show in Nautilus and at the output of the ls -l command in the terminal.

Enjoy!