IWH Software
My projects
Retro stuff
Random stuff
About me
Contact me
Admin login

Shared network folders

Now, you've set up your new Raspberry Pi, added an external USB drive (through a powered hub) now what..?

How about a cheap low power network attached storage (NAS)?
With one of these you can keep all your files available on any device, at any time, and it's possible to access them from anywhere in the world. And also on that stubborn smart DVD player that won't play your music from your computer unless it's switched on and running the DLNA server software.
And by using the Pi as a NAS means you can control almost every aspect of the programme, and set it up to your exact requirements.
Most off the shelf NAS you can buy are a one size fits all type affair, the LaCie NetworkSpace I have does not let you set up additional shared folders, or password protect the folders within the existing OpenShare partition, which meant those meddling kids either accessing your home video collection, or editing and deleting your other very important files.

So, does this sound like something you want? Then read on...

First of all you will need to install Samba

pi@Home:/$ sudo apt-get install samba samba-common-bin

Easy enough to install, but it's the configuration that's tricky. The folders I wanted to share are on the external USB drive mounted at /media/external/ and I have several folders to share within it.
You can create folders with the mkdir command followed by the new folder name, so navigate to the directory that you want to create the new folder in

pi@Home:/$ cd /media/external

and create the folder...

pi@Home:/media/external/$ mkdir ian

And repeat for all other folders you want.

You can get a listing of what's in the current directory with the command ls (LS) you can also use ls -a (LS - A) to list hidden files, and ls -l (LS - L) to get a more detailed listing, or combine options like this ls -al (LS - AL).

pi@Home:/media/external$ ls
Bluetooth  Music   PyLoad  Transmission   Ian
Pictures   secret  Temp    Video
pi@Home:/media/external$ ls -l
total 80
drwxr-xr-x  2 pi pi  4096 Apr 26 22:43 Bluetooth
drwxr-xr-x  9 pi pi  4096 Apr 17 17:43 Ian
drwxr-xr-x  4 pi pi  4096 Dec 29  2014 Music
drwxr-xr-x  2 pi pi  4096 Jan  2  2015 Pictures
drwxr-xr-x  2 pi pi  4096 Apr 16 10:08 PyLoad
drwxr-xr-x  2 pi pi  4096 Aug  5  2015 secret
drwxr-xr-x  2 pi pi  4096 May  1 10:35 Temp
drwxr-xr-x  2 pi pi  4096 May  1 08:38 Transmission
drwxr-xr-x  8 pi pi  4096 Dec 28  2014 Video

Next I changed the permissions so the shared folders are world writable with this command...

pi@Home:/media/external/$ chmod 777 *

The * means all folders. If you run ls -l now the permissions will have changed to drwxrwxrwx on all folders

Note: Don't worry, the whole world won't have access to your folders, it just means all the users on the Pi itself can write to the folders, and all the applications you install will be running under a user of some description, so setting the folders as World Writable saves you from fiddling with groups and permissions per app.
It's much easier to control who can do and see what within Samba

The Samba configuration file we need to edit is located at /etc/samba/smb.conf, you'd better make a copy of this file just in case, so use

pi@Home:/$ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.old

Now enter the command

pi@Home:/$ sudo nano /etc/samba/smb.conf

BTW nano is a simple text editor that runs in a terminal window.

Use the cursor keys and scroll down to Share Definitions and edit and enter the following for each READ ONLY folder you want to share...

read only = yes
writable = no
browsable = yes
guest ok = yes
create mask = 0777
directory mask = 0777

And edit and enter the following for each WORLD WRITABLE (within your own network only) folder you need...

read only = no
writable = yes
browsable = yes
guest ok = yes
create mask = 0777
directory mask = 0777

Finally edit and enter the following for each PASSWORD RESTRICTED folder you need...

read only = no
writable = yes
browsable = yes
guest ok = no
valid users = ian
create mask = 0777
directory mask = 0777

A breakdown of each line follows...

[Ian] - This is what the shared folder will be called when browsing the network
path=/media/external/ian - This is where the folder is located on the Pi
read only = no - Fairly obvious what this does
writable = yes - I don't know why you need the opposite of read only, but I did need it there
browsable = yes - I also needed this before it worked
guest ok = yes - You need this to be set to no if you don't want to allow access to anyone on the network, otherwise set it to yes
valid users = ian - Use this to restrict access to the folder to valid Unix users, you will need to setup a samba password for each user. You can set up multiple users like this, valid users = ian, pi, somebody, else
create mask = 0777 and directory mask = 0777 - These entries set the permissions on all files to be read, written to and executed by all users, you could also use 0755 to deny execution of files and scripts by users.

Once you're happy with what you've set up, save the file with CTRL + O then press enter, then exit with CTRL + X
Once you're done that, it's time to add some users if you want everybody to have their own private shares.

Technically the latest version of Samba doesn't need each user to have a Unix account, but other applications you may install do, so here's how to add Unix users on the Pi.
This is done with the useradd command

pi@Home:/$ sudo useradd name

Replace name with what you need, it's best practice to keep it all lowercase.
Next you will need to add a password for the account which you will need to type twice.
Then there will be some questions about the user, you can just press enter for all of them, then answer y at the end.
Next the user account will need a samba password, for security reasons this should be different to the Unix password.

pi@Home:/$ sudo smbpasswd -a name

Type the new password twice, once everything is set up, restart the samba services, or restart your Pi.

pi@Home:/$ sudo service samba restart

If that fails with an error about being masked then use both these commands

pi@Home:/$ sudo service smbd restart
pi@Home:/$ sudo service nkbd restart

Or just restart...

pi@Home:/$ sudo reboot

You can check whether the samba services are running with

pi@Home:/$ sudo service samba status

If it's not running then check the config file you've edited for errors. Once it's all up and running you should be good to go...

To access the shares on another computer open the network folder and double click on your devices name, now you will see all the shared folders within it, or go to the address window and type your Pi's IP address like this in Linux: smb:// or in Windows: \\, or the hostname instead of the IP on some systems, home or home.local
On a mobile device you'll probably need an app, on Android either ES File Explorer or Total Commander (but don't forget the LAN plugin) will do, I've never found a suitable app for Windows Phone, and I've no idea about iOS, I doubt the man in California would allow you to anyway.

And lastly, if you're wondering, the fastest file transfer speed I can get in or out of the Pi's 100Mb Ethernet port is 11.6MB/s, this is as close to the maximum 12.5MB/s as is possible due to the overhead that samba file sharing has.

© 2019 IWH Software (Ian Hill)