paper-thin-bits

Tech articles, mini tech blog

Shared home storage with Raspberry Pi and Samba

Contents

Goal

My goal was pretty simple. Setup a central storage on a Raspberry Pi device, so we could exchange files at home without using the cloud or skype. It wasn’t so hard to setup and I do recommend it to everyone that can spare some time and effort into doing this.

List of goals:

  • Central storage to place and exchange files.
  • Accessible from desktop and mobile devices.
  • Available only on the internal network.
  • User/Password protected.

Samba share scheme

Initially I wanted to install a dedicated cloud or storage solution on my Raspberry Pi device. However, I found out it was much more simple and intuitive to do this with a samba configuration.

Raspberry Pi

Get a RASPBIAN installation and use dd to copy the image to your SD Card. Check the official installation guidelines.

Remove X11 (Optional)

This might be a good idea if you’d like to use Raspbian only as a server without a desktop environment. You would also probably gain between 300-500MB disk space.

sudo apt-get remove --purge x11-common
sudo apt-get autoremove

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

It might also be a good idea to disable shell login for this user. 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

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

sudo cd /home/storeuser && mkdir share

Adjust the RW 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

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

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

sudo touch /home/storeuser/share/README

Samba Server

There are many different ways to setup a samba share. For the purpose of my needs I chose a simple 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

Configuration

Edit the smb.conf file:

nano /etc/samba/smb.conf

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

[global]

hosts allow = 192.168.1. 127.

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 = petrovs.net
wins support = yes

Now configure your server 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

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 have already created the storeuser when setting up the system above.

smbpasswd -a storeuser

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

Linux Client

I’m using Archlinux at home, so the description below mostly concerns my case. However, this should not differ too much for any other Linux installation.

View Shares

We could first have a look at the available shares in our network by using smbtree. It prints a tree with all the known domains, the servers in those domains and the shares on the servers.

smbtree -U storeuser

You will be asked for the storeuser password you set when calling smbpasswd earlier. The result would look similar to this:

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

fstab Mount

I went with the simplest possible way by adding the share directly in my /etc/fstab configuration.

//RASPBIAN/mystorage /mnt/storage cifs username=storeuser,password=<password>,comment=systemd.automount,uid=<your linux user>,gid=<your linux user group> 0 0

If you are not using systemd (for which I won’t blame you ;) ) please remove the comment=systemd.automount part.

You would of course need to create the storage folder first:

sudo mkdir /mnt/storage

We can now reload the fstab configuration by running:

sudo mount -a

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

Android Client

A very convenient way to access the SMB share is to use ES File Explorer.

Select Network / LAN from the app menu. Then add a new connection with New.

Type in the server name or IP address of your raspberry Pi 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.

iOS Client

I’m using File Explorer Free to access the samba share from an iPhone/iPad device. The free version is limited to one share configuration which is good enough for me. Feel free to use anything you like.

Configuration is quite easy. Add a new Linux/Unix share and configure the sever name or IP address of your raspberry Pi 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.

References