Build a Raspberry Pi NAS with Samba

Ever wanted a personal cloud at home? In this tutorial, you will learn how to build a Raspberry Pi NAS in less than 5 minutes – give or take.

To fully understand this tutorial, you need basic Linux understanding. If you need a short review, check this link.

Network-Attached Storage

A Network-Attached Storage, also known as NAS, is a data storage device that can be accessed remotely by any device in your network. With a NAS, you can share media files, seed a file 24/7, and even use it as a backup for your PC. Imagine it as having your own personal cloud hovering at home.

However, compared to cloud services, it is more scalable, has no monthly dues, and most importantly, doesn’t monitor your usage. No need to worry about personalized ads suddenly poping up out of nowhere in your browser.

For instance, a traditional NAS is a network device with 5 HDD/SSD racks. You can always add another hard drive once your memory is full. Actually, you can do that also with Raspberry Pi NAS, you just need to connect a flash drive or hard drive into one of its USB ports. All of this without the additional monthly fees cloud storage services often charge, and no megacorp monetizing your data.

While Pre-built NAS is more appropriate if you’re looking for a long-term solution, they can get expensive fast. If you want a trial NAS to try things out, you can use a Raspberry Pi. If you realize you don’t need it, after all, you can always repurpose the Pi. It’s actually one of its best charms.

Setting Up the Raspberry Pi NAS

You can build the Raspberry Pi NAS using its internal memory, aka the microSD card with the Raspberry Pi OS in it, or an external storage device. Both follow the same procedure, so don’t worry if you don’t have an external HDD yet.

First, update your Raspberry Pi.

sudo apt update 
sudo apt upgrade

Then, create the folders that will be accessible with NAS. Change the folder’s owner to root and the group to the current user. Then, allow read, write, and execute permissions on the current user and group while read and execute only for others.

sudo mkdir /home/shares 
sudo mkdir /home/shares/public 
sudo chown -R root:users /home/shares/public 
sudo chmod -R ug=rwx,o=rx /home/shares/public

We now have our Raspberry Pi ready for a NAS software. In this tutorial, we will use Samba.

Samba

Samba is free, open-source software that allows file and print services between clients and hosts with different operating systems. It’s a reimplementation of SMB (Server Message Block), a network protocol originally used between a Microsoft client and server. It was created to allow Windows clients to a Linux host’s file system.

To install Samba on your Raspberry Pi, enter the command below.

sudo apt install samba samba-common-bin

Then, edit the configuration file using the nano text editor.

sudo nano /etc/samba/smb.conf

You can limit the connections to your NAS by adding authentication. In the Samba configuration file, press CTRL + W and search for “Authentication”.

##### Authentication #####

Now, add the line below.

security = user

Now scroll down and find [homes]. It’s under the Share Definitions section. Change the read-only line to no. This will allow you to write files to your NAS, so that you can send and save files to it.

read only = no

Finally, add the public parameters below. Save the file and exit.

[public] 
  comment = public storage 
  path = /home/shares/public 
  valid users = @users 
  force group = users 
  create mask = 0660 
  directory mask = 0771 
  read only = no

Restart the Samba service to enable the changes.

sudo /etc/init.d/smbd restart

There should be a GUI that pops up after restarting the Samba service. It will ask for your SMB password for client authentication. Otherwise, you can enter the line below to manually add user “pi” and assign a password.

sudo smbpasswd -a pi

Register a Storage Device

At this point. you can already access all of Raspberry Pi’s memory with any connected device in your network. If you don’t have a flash drive or an external storage drive to extend the Pi’s memory, you can skip to the last section to see how to access the NAS server.

You can add multiple storage devices to a Raspberry Pi NAS but to simplify the procedure, it is best to configure things one by one. First, connect a storage device. Identify the storage device’s name by entering the following command.

dmesg

If you’re connecting the first storage device, the name should be sda1. Your device must be formatted with a Linux file system, such as ext3 or ext4. If it isn’t, you can format it using the command below.

umount /dev/sda1
sudo mkfs.ext4 /dev/sda1

Next, create a directory, change the owner, and change the read, write, and execute permissions just like what we’ve done with the Raspberry Pi’s main memory (SD Card).

sudo mkdir /home/shares/public/disk1
sudo chown -R root:users /home/shares/public/disk1
sudo chmod -R ug=rwx,o=rx /home/shares/public/disk1

Finally, mount the device on the folder.

sudo mount /dev/sda1 /home/shares/public/disk1

To make the storage device mount automatically during startup, we must edit the fstab file.

sudo nano /etc/fstab

Now for each device you registered, add the following line to the bottom of the fstab file. You should change the device name and the designated directory for every registered storage device to the Raspberry Pi NAS.

/dev/sda1 /home/shares/public/disk1 auto noatime,nofail 0 0

Accessing the NAS File Server

Figure 1: This PC

To access the Raspberry Pi NAS, go to This PC, click the Computer tab, and then click Map network drive.

Figure 2: Map Network Drive

After that, a prompt will ask you for the network folder you’d like to map. You can use any drive letter you want. If you have never changed the name of your Pi you can connect to the public directory by entering \\raspberrypi\public or to the private directory with the username (in our example pi) by entering \\raspberrypi\pi. Also for smartphones, you can connect to the NAS using an app like File Expert for Android or File Explorer on IOS.

Figure 3: Raspberry Pi NAS in action

There you have it! You now know how to make a Raspberry Pi NAS using only a Raspberry Pi and an optional storage device.