Knowledge for the World

Make a Raspberry Pi NAS device with Samba

Your network storage just got bigger!
  • by Ash (35)
  • 30 minutes

Setting up your own network storage device is super easy with a Raspberry Pi. All you need is an application called Samba and a few tweaks to the default settings. Before you know it, everyone on your network will have access to a custom Raspberry Pi NAS system. Everyone can share files with each other or even keep a private network folder for themselves.

If you're looking for something a little less technically-involved, skip to the end where we'll discuss OpenMediaVault and NAS4Free.

1

Update Raspbian

Before we download anything, we need to start with a fresh update. In this guide, I'm using a Raspberry 3 B+ with Raspbian installed. Be sure to update your OS to the latest version. Visit our guide here on how to update to the latest version of Raspbian.

2

Once everything is up to date, it might be a good idea to restart your Raspberry Pi. You can use the following command to restart:

sudo reboot

After a fresh update and restart, we're ready to get started. Run the following command to install Samba on your Pi.

sudo apt install samba samba-common-bin
3

Samba will need a place to store our files on the network. Therefor we will create our own directories and point Samba to them. Use the following commands to create both a private and public directory location.

Note: My example places the folders under the /home/ directory. You may find another location is suitable for your NAS structure.

sudo mkdir /home/storage
sudo mkdir /home/storage/public

These directories require special permissions to function properly. Use the following commands to set the permissions.

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

Enable authentication prompt

If you want to enable a login prompt for your storage device, open the following file.

sudo nano /etc/samba/smb.conf

Look for this line:

##### Authentication #####

Beneath it, add the following string:

security = user

Leave this file open, we will be using it for the next couple of steps.

5

Adjust private storage permissions

This step will allow for write permissions to the private storage directory. Using the same file from the previous step, look for a section titled:

[homes]

Change the read only section to reflect:

read only = no
6

Adjust public storage permissions

This step will set access permissions for the public storage directory. Using the same file from the previous steps, scroll to very bottom and enter the following:

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

After adjusting all of the permission settings, we will need to save this file. Press ctrl + x to open the exit prompt. When asked to save the document, press Y and enter.

Restart Samba with the following command:

sudo /etc/init.d/samba restart
8

If you'd like to add a user to Samba, run the following command:

sudo smbpasswd -a pi

In my example, I'm adding the user "Pi". Follow the prompts from Samba to set a password for the NAS.

9

You can provide extra storage to your NAS device by adding a usb thumbdrive or even harddrive. Plug in the device and use the following command to find its name:

dmesg

If this is the first and only usb storage device, it will probably be named "sda1". Whichever device you'd like to add to the NAS, take note of the name.

Format the drive

If you haven't already, you will need to format the drive with a Linux file system. Use the following code to format the drive, replacing "sda1" with the appropriate drive name.

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

Create a directory for the additional drive

Create a directory for the drive and set appropriate permissions with the following code:

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

Enable drive mount on reboot

Rebooting your Pi will cause the drive to dismount. You can set up the pi to always mount the additional device on startup. Run the following command:

sudo nano /etc/fstab

Add the following line to the bottom of the file for each drive you want to mount. Replace "sda1" with the name of the respective drive.

/dev/sda1 /home/storage/public/disk1 auto noatime,nofail 0 0
10

Connect to the NAS

This is the easiest part. Congratulations! Your NAS Pi device should now be accessible on your network. It's time to drag and drop files to your heart's content. If you're using a Windows device, the following path will open the NAS from a run window.

Note: My Pi is named "raspberrypi". You will need to adjust the example to reflect the name of your Pi.

Public folder access:

\\raspberrypi\public

Private folder access (for a user named pi):

\\raspberrypi\pi
11

If you don't want to go the Samba route, there are two applications worth mentioning, and both are relatively simple to install.

OpenMediaVault and NAS4Free are available for free, and they install easily from a disk image.

Download the image for either, then write the image to your SD card. I'd recommend checking out Etcher to do this, since it's available for Windows/Mac/Linux and simple to use.

Pop that SD card into your Raspberry Pi and watch as they install automatically. Finish configuring with your IP address and voila!