How to Share a Folder on Raspberry Pi
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.
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:
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:
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.
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.