Home Storage with Raspberry Pi and Samba
Tags: raspberrypi, linux, samba, shell
Published: 2018-11-02 20:53:25

# Contents

* [Goal](#goal)
* [Raspberry Pi](#rpi)
  * [Remove X11](#remove-x11)
  * [Storage User](#storage-user)
* [Samba Server](#samba-server)
  * [Installation](#samba-server-install)
  * [Configuration](#samba-server-conf)
* [Clinets](#clients)
  * [Linux Client](#linux-client)
      * [View Shares](#linux-client-shares)
      * [fstab Mount](#linux-client-fstab)
  * [Android Client](#android-client)
  * [iOS Client](#ios-client)
* [References](#refs)  
 
 
# <a name="goal"></a>Goal

The idea here is pretty simple. Setup a central storage on a Raspberry Pi [[1]](#refs) 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](https://thepracticaldev.s3.amazonaws.com/i/kxofvivme0llfdl6x154.png)

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.

# <a name="rpi"></a>Raspberry Pi

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

On Linux the following should suffice:

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

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

## <a name="remove-x11"></a>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

## <a name="storage-user"></a>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 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

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

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

About the `1770` permissions. We set the sticky bit [[4]](#refs) 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


# <a name="samba-server"></a>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.

## <a name="samba-server-install"></a>Installation

    sudo apt-get install samba samba-common-bin

## <a name="samba-server-conf"></a>Configuration

Edit the `smb.conf` file:

    nano /etc/samba/smb.conf

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:

```bash
[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:

```bash
[global]

hosts allow = 192.168.1. 127.
workgroup = homeworld.net
wins support = yes
```

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

```bash
[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'll use the `storeuser` that we created earlier.

    smbpasswd -a storeuser

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

# <a name="clients"></a>Clients

## <a name="linux-client"></a>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.

### <a name="linux-client-shares"></a>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

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

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

### <a name="linux-client-fstab"></a>fstab Mount

Create a target mount folder.

    sudo mkdir /mnt/storage

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

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]](#refs). No point in trying to mount if connection's not been setup yet.

Let's mount the share explicitly by running:

    sudo mount -a

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

## <a name="android-client"></a>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]](#refs), but I found the ads too obstructive, so I switched to File Manager [[7]](#refs)

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](https://thepracticaldev.s3.amazonaws.com/i/f1abbbbux0zncxhbyi2d.png)

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](https://thepracticaldev.s3.amazonaws.com/i/ouip5s8b60qpsh74u8gk.png)


## <a name="ios-client"></a>iOS Client

I'm using File Explorer Free [[7]](#refs) 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](https://thepracticaldev.s3.amazonaws.com/i/1new2cle3m70y05t36ur.jpg)

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.


# <a name="refs"></a>References

  1. [raspberrypi.org](https://www.raspberrypi.org)
  2. [raspberrypi.org/downloads](https://www.raspberrypi.org/downloads/raspbian)
  3. [raspberrypi.org/documentation/installation/installing-images/README.md](http://www.raspberrypi.org/documentation/installation/installing-images/README.md)
  4. [en.wikipedia.org/wiki/Sticky_bit](http://en.wikipedia.org/wiki/Sticky_bit)
  5. [codingberg.com/linux/systemd_when_to_use_netdev_mount_option](http://codingberg.com/linux/systemd_when_to_use_netdev_mount_option)
  6. [ES File Explorer](https://play.google.com/store/apps/details?id=com.estrongs.android.pop) for Android
  7. [File Manager](https://play.google.com/store/apps/details?id=com.asus.filemanager) for Android
  8. [File Explorer Free](https://itunes.apple.com/us/app/fileexplorer-free/id510282524?mt=8) for iOS
  9. Samba suite config file ref - [samba.org/samba/docs/man/manpages-3/smb.conf.5.html](https://www.samba.org/samba/docs/man/manpages-3/smb.conf.5.html)
  10. Arch Linux bud? Samba Wiki - [wiki.archlinux.org/index.php/samba](https://wiki.archlinux.org/index.php/samba)