# Home Server: NAS

## Intro

There are many types of servers, so what are you waiting for?
In this tutorial we are going to take a closer look at the NAS, Storing your data on a server is handy for backup reasons, or for sharing purposes. Lets take a look.

## Requirements

• Raspberry pi (2 or above, preferably 3B or above)
• Network connection (preferably wired)
• Power supply
• SSH connection (see Remote connections)
• USB drive (or stick) for storage

## Samba

No we are not going to dance.
Samba is a program that runs on a Linux computer and takes care of the sharing of drives. Samba is a handy Linux package that is used to share drives and folders to other (windows) computers. these shared folders (a drive in Linux is the same as a folder) can be included as a network drive in windows.

We’ll assume you are using an external drive to be used as storage.
Storing files on the SD card is NOT recommended when using it as a server.

Once samba is set up, you can mount the file server on all the computers in your network. This makes it easy to use files, share files, or use it as a backup. A wired Ethernet connection is recommended for fast transfer speeds and stability. Nevertheless it will work fine with Wi-Fi, but speed will be decreased.

Before installation we will update all installed packages on the pi with the 2 following commands:

sudo apt update


Depending on the image ‘age’ you used to create the sd card this can take a while. Installing samba is easy, just use following terminal command and samba is installed and ready for use:

sudo apt-get install samba samba-common-bin ntfs-3g -y

As you can see we installed 3 packages at once, the first 2 are needed for the file sharing.
the NTFS-3G packages is used to mount the NTFS drives we might need.

Did i say ready for use? Well not really, we might have samba installed but we didn’t configure it yet. Lets start  by creating our shared folder(s) for example:

sudo mkdir -m 1777 /share

Some explanation on this line, ‘mkdir’ is the command used to create a new directory. The ‘-m’ is one of the option available with the command, this sets the mode of the folder. The 1777 is the mode we use, in this case, the command sets the sticky bit (1) to help prevent the directory from being accidentally deleted and gives everyone read/write/execute (777) permissions on it. the name of the folder or directory is ‘/share’

Now i said that i don’t recommend to use the SD card as storage for your home server.
so lets make an appropriate folder for the external drive.

sudo mkdir -m 1777 /media/share
sudo chmod 777 -R /media/share

The last line isn’t strictly required but if you have writing problems when accessing the drive from windows (see later on) you’ll want to execute this. Now all we have to do is mount our drive to this folder.

### Mounting a drive

sudo mount -t ntfs-3g /dev/sda1 /media/share

/dev/sda1 is the partion of your connected drive. If you only have 1 drive or usb connected with only 1 partition on it it will always be /dev/sda1. But in case you want to be sure lets us the fdisk command:

sudo fdisk -l

Now that our drive is mounted we’ll add some random file on our drive.

cd /media/share
sudo touch test.txt

Lets reboot and go back to your share folder.

sudo reboot now
cd /media/share

Our drive still seems to be there, as we can move into the folder.
but can we find our files inside it? lets try:

ls -a

This command should give us all the files in the current folder. But wait, the only ‘files’ it sees are . and ..

Before we continue in our installation lets explain something about the Linux file system.
Linux sees all folders and drives as a file, isn’t that easy? So where are our files gone to? The good news is that they are still on the external drive. The bad news is that each time we reboot our pi we will need to remount it to the correct folder.

Now isn’t that a pain in the ass? Well yes, but wait there is a solution to this! There is a file called ‘fstab’ in your pi, this file (as we’ll see) is used to auto mount the drives to their correct paths. So lets take a look at this file shall we?

sudo cat /etc/fstab

The output will look like this:

proc                        /proc           proc    defaults          0       0
PARTUUID=6c586e13-01      /boot           vfat    defaults          0       2
PARTUUID=6c586e13-02      /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

As you can see this mounts our sd card to / and /boot. OK, so how do we add our own drives to this? To do that there are 2 ways: using de drive ‘names’ given by the Linux system (eg /dev/sda1) or as in the example use there (PART)UUID number.
The problem with the first option is that when you hook up a second drive (when powered down) then we have no idea if the drives will have the same name.
This is where the second method is more interesting, thought a little more difficult to get to.

To get the Linux device names we simply list all drives connected using the fdisk command.

sudo fdisk -l

An the output

Disk /dev/ram0: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram1: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

...

Disk /dev/ram15: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/mmcblk0: 14.6 GiB, 15640559616 bytes, 30547968 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6c586e13

Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1        8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      532480 30547967 30015488 14.3G 83 Linux

Disk /dev/sda: 984 MiB, 1031798784 bytes, 2015232 sectors
Disk model: DataTraveler 2.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start     End Sectors  Size Id Type
/dev/sda1          63 2015231 2015169  984M 83 Linux

Now hold my hat there sir. that is a lot of information that we don’t need, isn’t there a way around? Ofcourse there is, all we do is specify what kind of devices we would like to visualize:

sudo fdisk -l /dev/sd* | grep /dev/
Disk /dev/sda: 984 MiB, 1031798784 bytes, 2015232 sectors
/dev/sda1          63 2015231 2015169  984M 83 Linux
Disk /dev/sda1: 984 MiB, 1031766528 bytes, 2015169 sectors
Disk /dev/sdb: 14.6 GiB, 15633219584 bytes, 30533632 sectors
/dev/sdb1        2048 30533631 30531584 14.6G 83 Linux
Disk /dev/sdb1: 14.6 GiB, 15632171008 bytes, 30531584 sectors

That is much better.
As you can see now all we have is the drive data we are looking for, in this example I connected 2 usb drives, namely /dev/sda of 1GB and /dev/sdb of 16GB. In my example both drives have a linux partition. further on I’ll assume your drive is formatted as NTFS filesystem (standard windows filesystem for hard drives). So if you wish to use the first method all you have to do is add following to the fstab.

/dev/sda1 /media/share ntfs-3g defaults,rw 0 0

To edit a file we use nano or vi or vim. as nano is installed as per default we’ll use this.

sudo nano /etc/fstab
proc            /proc           proc    defaults          0       0
PARTUUID=6c586e13-01  /boot           vfat    defaults          0       2
PARTUUID=6c586e13-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

/dev/sda1       /media/share    ntfs-3g defaults,rw 0 0

Note that i used the PARTUUID, this is a more stable way of mounting a drive,
you can find the UUID of a partition or drive using the blkid command:

sudo blkid

now lets mount it, we can force our raspberry pi to remount all in the fstab file using the mount command, i recommend to do this every time you change the fstab file, if the fstab file is incorrect you’ll get a kernal panic and will need the root password.

sudo mount -a
ls -l /media/share

Checking the output of the ls command again we’ll see our created file.

total 20
drwx------ 2 root      root      16384 Nov  7 21:13 lost+found
-rw-r--r-- 1 frederick frederick    12 Jan 23 21:28 text.txt

### Configuring Samba

So to configure samba we need to edit the smb.conf. We don’t need to edit any of the existing configurations, we only need to add our own.

sudo nano /etc/samba/smb.conf

We want to add our drive to the list of samba shares. we’ll scroll to the bottom of this file to add our own. First we’ll need to tell it what our network drive is called. Lets call our network drive ‘share’, put it in between brackets. We can add a comment if we’d like. What we absolutely need to add is the path to the folder (obviously) all the others actually speak for themself so I’ll just drop it below.

There is one neat thing we can add, we can add valid users. This is handy if you have multiple shared folders and only want some people to have access to a certain folder. In this case ‘username’ is the only one who can access the share folder. But did you notice the ‘;’ this comments out the line so actually everyone who has access to the samba drive van access this share.

[Share]
comment = Raspberry pi share
path = /media/share
browseable = yes
guest ok = no
;   valid users = ‘username’

Now the only thing left is to add a user to the samba group and give it a password. This user needs to be a Linux user. See our tutorial on how to add a user to the Linux OS.

sudo smbpasswd -a ‘username’

This will ask you to type in a password twice. Now this was the last step to installing samba.

Now open an explorer window on your PC and type in \\192.168.1.11 (the IP address of YOUR pi, change if needed). Windows will now ask you to input your credentials we just added to samba.

Well congratulations, you just installed SAMBA and configured it for usage.