DEV Community

Cover image for Home Storage with Raspberry Pi and Samba
Petar G. Petrov
Petar G. Petrov

Posted on

Home Storage with Raspberry Pi and Samba

Contents

Goal

The idea here is pretty simple. Setup a central storage on a Raspberry Pi [1] device, so we could exchange files at home without using the cloud or Skype, or any other messaging service. The whole thing wasn't so hard to setup and I do recommend it to everyone that can spare some time and effort into doing this.

Things I wanted to have:

  1. Central storage to place and exchange files.
  2. Accessible from desktop and mobile devices.
  3. Available only on the internal network.
  4. User/Password protection.
  5. A reason to finally do something with that RPi I got.

Samba share scheme

Initially I wanted to install a dedicated cloud or storage solution on the RPi device, however, I found out it was much more simple and intuitive to configure a Samba server instead.

Raspberry Pi

Get a Raspbian Lite [2] installation and use dd to copy the image to your SD Card. Check the official installation guidelines [3].

On Linux the following should suffice:

dd bs=4M if=2018-10-09-raspbian-stretch.img of=/dev/sd? conv=fsync
Enter fullscreen mode Exit fullscreen mode

Careful with the sd? part! Make sure that's your SD card device.

Remove X11 (Optional)

EDIT: If you download Raspbian Lite, you don't need to do anything here.

This might be a good idea, if you'd like to use Raspbian only as a server without a desktop environment. You'd also probably free up to 500MB SD card space.

sudo apt-get remove --purge x11-common
sudo apt-get autoremove
Enter fullscreen mode Exit fullscreen mode

Storage User

Add a new user with the name storeuser. All shared files and folders will be stored in the home directory of this user.

sudo adduser storeuser
Enter fullscreen mode Exit fullscreen mode

It might also be a good idea to disable shell login for this guy. In the end we only want to have this user as a basis for our Samba share configuration.

sudo usermod -s /usr/sbin/nologin storeuser
Enter fullscreen mode Exit fullscreen mode

A new home folder /home/storeuser will get created. Next, create a share folder under which the shared files and folders will be saved.

sudo cd /home/storeuser && mkdir share
Enter fullscreen mode Exit fullscreen mode

Adjust the read/write permissions to the share folder:

sudo chown -R storeuser /home/storeuser/share
sudo chgrp -R storeuser /home/storeuser/share
sudo chmod -R 1770 /home/storeuser/share
Enter fullscreen mode Exit fullscreen mode

About the 1770 permissions. We set the sticky bit [4] to 1 to allow only the owner of this folder storeuser to edit or delete files.

Create a sample file, e.g., a README, just to have at least one file in the share folder for tests.

sudo touch /home/storeuser/share/README
Enter fullscreen mode Exit fullscreen mode

Samba Server

There could be a number of ways to setup a Samba share. For the purpose of my needs I chose a single user password protected share mechanism, but you can extend this to use groups and more complex authentication schemes.

Installation

sudo apt-get install samba samba-common-bin
Enter fullscreen mode Exit fullscreen mode

Configuration

Edit the smb.conf file:

nano /etc/samba/smb.conf
Enter fullscreen mode Exit fullscreen mode

That's right! I used nano. I still can't exit vim on the other terminal. Deal with it. 😊

To be on the safe side add your network to the allowed hosts:

[global]

hosts allow = 192.168.1. 127.
Enter fullscreen mode Exit fullscreen mode

I'm using a single subnet 192.168.1.0/24 network at home. Adjust this depending on your network configuration.

Add a workgroup name and enable Windows Internet Name Serving:

[global]

hosts allow = 192.168.1. 127.
workgroup = homeworld.net
wins support = yes
Enter fullscreen mode Exit fullscreen mode

Now configure the share. Add the following at the end of the smb.conf file:

[mystorage]
   comment= my home storage
   path=/home/storeuser/share
   browseable=Yes
   writeable=Yes
   only guest=no
   create mask=1770
   directory mask=1770
   public=no
Enter fullscreen mode Exit fullscreen mode

path specifies a directory to which the user of the service is to be given access. browseable controls whether this share is seen in the list of available shares in a net view and in the browse list.

Add a new storeuser password to the local Samba smbpasswd file. This user must already exist on the system. We'll use the storeuser that we created earlier.

smbpasswd -a storeuser
Enter fullscreen mode Exit fullscreen mode

This is exactly the user and password that clients will use to access the Samba share.

Clients

Linux Client

I'm using Arch Linux quite often at home, so the description below mostly concerns that case. However, this should not differ too much on other Linux distributions.

View Shares

We could first have a look at the available shares in the network by using smbtree. Just like that, for fun and glory.

smbtree -U storeuser
Enter fullscreen mode Exit fullscreen mode

When prompted, give the password of storeuser that you set earlier using smbpasswd. The result should look similar to this:

MYDOMAIN.TLD
    \\RASPBIAN              my raspbian server
        \\RASPBIAN\storeuser        Home Directories
        \\RASPBIAN\IPC$             IPC Service
        \\RASPBIAN\mystorage        my home storage
Enter fullscreen mode Exit fullscreen mode

This prints a tree with all the known domains, the servers in those domains and the shares available on those servers.

fstab Mount

Create a target mount folder.

sudo mkdir /mnt/storage
Enter fullscreen mode Exit fullscreen mode

Setup an auto mount by adding the share to /etc/fstab.

//RASPBIAN/mystorage /mnt/storage cifs username=storeuser,password=<password>,comment=noauto,x-systemd.automount,_netdev,uid=<your linux user>,gid=<your linux user group> 0 0
Enter fullscreen mode Exit fullscreen mode

If you're not using systemd (for which I don't blame you 😉), remove the noauto,x-systemd.automount part.

Ok, what the heck is this _netdev trickery?

Basically, this means that we'd like to defer mounting the share until the network interface is up [5]. No point in trying to mount if connection's not been setup yet.

Let's mount the share explicitly by running:

sudo mount -a
Enter fullscreen mode Exit fullscreen mode

You should see the sample README file we created earlier in /mnt/storage.

Android Client

There're many Android tools to choose from. Pretty much everything that supports SMB shares should work. In the past I was using ES File Explorer [6], but I found the ads too obstructive, so I switched to File Manager [7]

Here's a quick setup overview of ES File Explorer.

  • Select Network / LAN from the app menu and then add a new connection with New.
  • Type in the server name or IP address of your RPi device.
  • Type in the storeuser username and password.

ES File Explorer

You should now be able to access your Samba share with Read/Write permissions.

Setting up File Manager is pretty easy as well. Just use Network place from the menu to select your share and type in the credentials.

File Manager

iOS Client

I'm using File Explorer Free [7] to access Samba shares from iOS devices. The free version is limited to just one share, which is good enough for me.

Configuration is quite easy. Add a new Linux/Unix share and configure the sever name or IP address of your RPi device.

Type in the storeuser username and password.

File Explorer Free

You should now be able to access your Samba share with Read/Write permissions.

Do you know any other good tools? Free or paid. Drop a comment below.

References

  1. raspberrypi.org
  2. raspberrypi.org/downloads
  3. raspberrypi.org/documentation/installation/installing-images/README.md
  4. en.wikipedia.org/wiki/Sticky_bit
  5. codingberg.com/linux/systemd_when_to_use_netdev_mount_option
  6. ES File Explorer for Android
  7. File Manager for Android
  8. File Explorer Free for iOS
  9. Samba suite config file ref - samba.org/samba/docs/man/manpages-3/smb.conf.5.html
  10. Arch Linux bud? Samba Wiki - wiki.archlinux.org/index.php/samba

Oldest comments (7)

Collapse
 
tux0r profile image
tux0r

Wouldn't it be easier to run a system without a desktop instead of Raspbian on the Raspberry Pi as you don't want to use that anyway?

Collapse
 
petarov profile image
Petar G. Petrov

Totally! I should edit that, forgot that Raspbian Lite comes without a desktop installation.

Collapse
 
tux0r profile image
tux0r

That's another option indeed.

Collapse
 
kungtotte profile image
Thomas Landin

I am really happy with the NextCloudPi setup I have here at home. There are sync clients for all platforms and it runs super smoothly.

It's the perfect setup for me and my wife since we have three computers and two phones between us that we like to share stuff between.

Collapse
 
petarov profile image
Petar G. Petrov

NextCloudPi - looks pretty neat!

I got a question - do you use the SD card or have you plugged an external drive to your RPi? If you use the SD card, are you noticing any considerable delays when uploading / downloading stuff?

Collapse
 
kungtotte profile image
Thomas Landin

The OS is on an SD card but all storage is on a separate USB drive.

For our usage pattern (two users, few devices concurrently) I can detect no performance issues at all.

Apparently it becomes a problem with high concurrency because the ethernet port shares a bus with the USB connectors, so disk writes and network traffic ends up competing for the bus causing slowdowns.

Thread Thread
 
petarov profile image
Petar G. Petrov

Thanks! The shared bus info was something I did not anticipate.