Coding

How to Share a Folder on Raspberry Pi


File Folders by One Way Stock

File Folders by One Way Stock

In part one of our tutorial, we discussed how to mount an external hard drive or USB drive on to our Raspberry Pi (or any other Linux based) computer. In this tutorial we will discuss how to share folders from that computer to your local network. I am doing this so I can share the media files I have on my external hard drive, and access them from many places on my network, such as my laptop, my android phone, and my Raspberry Pi running XBMC. You can use this tutorial to share files in a similar way.

Introducing Samba

Samba is a system service available for Linux which allows for folder and printer sharing on the network.  If you are familiar with using shared drives on Microsoft Windows, then you’ll have a good idea of what Samba can provide in the Linux world.

Step 1 – Install Samba

First we’ll need to install samba, if we don’t already have it.  To do this run as root (or use sudo):

apt-get install samba samba-common-bin

This will install samba, samba tools and any dependencies they need.

Step 2 – Configure a Guest User

I want to share my folder with anyone, and not need to supply a user name and password to access the folder.  In order to do this, we will be mapping the folder to the Linux guest user, nobody.  Nobody is a special guest user on Linux systems that has very little permissions, even less than a regular user.

In order to tell samba we want to log in without a password, we need to remove the password from the ‘nobody’ user with the following command (as root):

smbpasswd -an nobody

This is obviously not secure.  By doing this you are allowing anybody on your network to read and write from your shared folder.  I would not recommend doing this on a corporate network, or on any computer which is going to be connecting to untrusted networks.  This is intended for simple home use only, where you are in the relative safety behind your broadband firewall.  A more secure solution would be to configure samba to require passwords to access the shared folder.

Step 3 – Configure Samba

Samba’s main configuration file can be found at /etc/samba/smb.conf.  This file contains configuration values for the smbd system service which will be managing your shared printers and folders, and deciding who gets access to them.  This file is laid out like a windows INI file, with sections denoted by headers in square brackets, and configurations given as key-value pairs.

Open the /etc/samba/smb.conf file in your favourite editor (as root).  In the [global] section, under Authentication ensure you have the following settings:

[global]
security = user
guest account = nobody
map to guest = bad password

Setting “security” to “user” means that Samba will try to authenticate a connection to a shared folder as a local user. Local users can either be users managed by Linux, or users managed by Samba itself. By default Samba uses the Linux users as defined in the Linux password file.

The “guest account” setting tells Samba which local Linux user to map the guest access to. This can be any valid local user, but nobody is a good choice as they have very few permissions and it makes the configuration a little more secure (but not very).

The “map to guest” setting tells Samba when to treat a login attempt as a guest login. In this case the “bad password” setting causes Samba to treat any login with a bad password as a guest login.

You can leave the other options at their default settings.

Step 4 – Configure your Shared Folder

Now we configure our shared folders properties in a new INI section.  The section header will become the share name for your folder.  In my example I have:

[iomega]
comment=Iomega Share
browseable=yes
path=/media/iomega
public=yes
writable=yes
guest ok=yes

As you can see I’m sharing this folder with the name iomega, given by the section header.  Setting browsable to yes means that the folder becomes a member of the share list when viewing the shares using net view.  The path sets the local directory path to the folder I want to share.  We discussed previously, how to mount an external hard disk to the local file-system.  Public as yes and guest ok as yes are synonyms, you can use either.  These allow the guest user to access this share.  Finally writeable as yes allows clients to write or create files on the share once they have connected.

Save your edits to the /etc/samba/smb.conf file and exit your editor.

Step 5 – Start Samba

Finally we can start the Samba service and allow it to share our folder.  Do this by issuing (as root):

service samba start

Once the service has started you should be able to test your set-up. Try connecting to your share from another computer. Within Windows Explorer try \\YourHostName\YourShareName, or from the Linux file manager try smb://YourHostName/YourShareName.

Further Reading

Yet again, we’ve barely scratched the surface of the configuration options open to you when you use Samba.  I would recommend visiting the Samba site, and reading more about the configuration options available in smb.conf to gain a better understanding of this configuration.

Standard

20 thoughts on “How to Share a Folder on Raspberry Pi

  1. Interesting article! I am very interested in the Raspberry Pie; I think it’s a great piece of hardware for a great price.

    Does it come with a Linux distro pre-installed? And if so, which one?

    • ccollins says:

      Hi Sebastian,
      The reccomended Linux distro for Raspberry Pi is called Raspian and is based on Debian. The Pi doesn’t come with a distro as such, but you can download one from the Raspberry Pi website.

      • ccollins says:

        To an extent yes.

        The hardware is limited in it’s capabilities in terms of raw computing power, so a lightweight OS is needed. The Raspian distribution produced has selected a lightweight desktop and tools for the default configuration.

        Also, the CPU the RasPi uses is based on the ARM architecture. While this is common among embedded systems, or even smart-phones, it’s not that common an architecture among desktops or servers, so most Linux distros don’t produce ARM compatible binaries. (Though the RasPi might change this)

  2. Pingback: Home Theater Raspberry Pi « Chris Collins

  3. fishbone says:

    Jesus Christ your guide was SO much easier than others that I have read. They all seem to be outdated (for example Raspbian now automounts drives at startup). THANK you! Following your guide I got it setup in minutes.

    Now, one more question if you would please help.
    How would I go about getting samba to autostart at boot?

    • ccollins says:

      Thanks Fishbone,
      I’ve read many of the same posts, but for some reason automounting never really worked as I expected. Also, it’s no harm in understanding what’s going on under the hood so you know where to start fixing it if issues arise.

      As for Samba, if you installed using apt-get, then it should be starting at boot automatically. You can check this if you use (as root) the “service samba status” command. This should show both smbd and nmbd (samba’s services) as running.

      If they are not running, it’s no harm to try to run them manually using “service samba start” and check again. If they’re failing to run, then have a look at samba’s log files for clues to what might be upsetting it. Unless you’ve modified your configuration to put them elsewhere, you should find samba’s logs under /var/log/samba/.

      It may be something as simple as an unknown or unexpected entry in the configuration file that’s preventing samba from running. There’s no harm in having a look here to make sure the configuration values are well formed.

      -Chris

      • fishbone says:

        You were correct! Samba and the shared drive came up happily on their own upon reboot. Thanks for the easy peasy guide

  4. Bob says:

    Just wondering. Should the line ‘browsable’ be ‘browseable’? This is my issue with Linux, there is no continuity which is really hard to learn. Other codes in different areas need the latter.

    • ccollins says:

      Ah here! Let’s hear no more of my creative use of spelling!

      If anybody needs me, I’ll be reading my dictionary.

  5. Pingback: Setting up a Raspberry Pi Mini Computer » Tim Kerins Science and Sums

  6. Ben says:

    So finally I have my public share working on my Pi. Following your instructions I did come to the conclusiont that having both lines valide users = %S AND public = yes could not be together for the same share. So I removed the valid users = %S from the samba configuration and now finally all is working and nobody keeps asking me for a password which is not there,
    Big Thanks !

  7. Pingback: Mi Raspberry Pi: servidor multimedia + servidor web + torrent box

  8. Pingback: Raspberry Pi (3): Weerrobot en MySql – Besturen, meten, regelen en schakelen met computer of microcontroller

Comments are closed.