Last updated on March 13, 2019
As with many of these tutorials, the examples provided will start with an install, firstly adding the software that the service needs. At this stage, it is also worth to note that it is advisable to update the application cache on the device before we try to install so that we are looking at the latest version available.
sudo apt-get update sudo apt-get install samba samba-common-bin
Creating a directory to share
The directory that we will share out will act as a mount point for external storage. So we will need to create the directory and set appropriate permissions to the directory. Using mkdir we can both create the directory and set the permissions in the one command. The -m option will allow you to set the mode or permissions of the directory.
sudo mkdir -m 1777 /data
Investigating the above command, we can see the easy part of cofiguring a Raspberry Pi Samba Server, creating the directory /data. As the directory is at the root of the file-system we will need administrative permissions. To do this so we run it prefaced with the sudo command. The mode of the directory is set with the -m option:
- 1: sets the sticky bit. This set on a directory ensures that users can only delete files they own.
- 7: sets RWX read , write and execute for the user owner
- 7: sets RWX read , write and execute for the group owner
- 7: sets RWX read , write and execute for others
This directory will be empty at the moment and, of course, the root file-system is limited in size on the Pi to that available from the SD cards. We can use an external drive connected to the USB ports and have this mounted to the /data directory. This then can provide effective storage for your network
Edit the Samba Configuration, smb.conf
With samba installed we can edit the configuration to allow us to share out the directory /data. The location for the file is /etc/samba/smb.conf. The default file is, shall we say well documented. This makes for a fairly bulky file that is not the easiest to navigate. You can expect 300 lines in this file with 90% being documentation. I would suggest renaming this file and to copy only the active configuration back.
sudo mv /etc/samba/smb.conf /etc/samba/smb.conf/$(date +%F)
The above command is a rather neat way to give a sate based extension to a file. The resulting file will look like this:
Now we may as well make life a little easier for us and start up a root shell as many commands will need rights. I would not running this often as it defeats the purpose of the command sudo. We use it here to make life easier when running redirection with sudo.
Now at a full root user prompt, and moving into the /etc/samba directory, we can filter the contents of the backup file back into the smb.conf. we can use grep for this. We want to remove the extra comments and blank lines from the file to make it easier to read. We use grep, other may oprefer to used the command sed.
grep -ve ^# -ve ‘^;’ -ve ^$ smb.conf.2103-11-26 > smb.conf
Understanding the GREP Filter
The command may look a little messy but is really useful to us.
- -ve ^# : First we remove lines that start with a #
- -ve ‘^;’ : The we do the same for lines beginning with ; Note the quotes here as the ; has other meanings so we escape the semi-colon in the quotes
- -ve ^$ : Finally we remove empty lines
- > : This is used to redirect the output to the new smb.conf.
We go from and incredible 333 lines in the original file to a minute 34 lines in the new file. This way, the result is quick and we have not lost out documentation as it is in the backup.
Adding a share definition
We will now edit the /etc/samba/smb.conf and add a share definition for the /data directory.
[data] comment = Data share path = /data browseable = yes read only = no
The share definitions are towards the end of the file and we can add this at the end. Just make sure it is after the last line of any other share definition.
Once we have saved the file we should test the configuration using testparm. This was added from the package, samba-common-bin.
This checks the samba configuration file. If not errors are reported we can then restart the server. We are still runing as root so we do not need to use sudo.
service samba restart
This will restart both the smbd and nmbd. The Raspberry Pi Samba Server and NetBIOS Name Server.
Creating samba users
The last part of this tutorial will involve us creating samba users. These users must represent Linux users but their password can and should be different to the Linux password for security. This is not enforced, though. Using the command smbpasswd as the root user we can add new samba users. A user must have a password in samba to access shares.
smbpasswd -a root smbpasswd -a pi
The above commands add root and pi as samba users. By default, a TDB database is used to store this information. You can list current samba users with the command pdbedit.
Running the above command on my system lists the three users I have added as samba accounts.
Testing the Connection with smbclient
We can now simply test the process, even from Linux on our Pi. Using the command smbclient we can list shares on a server or even connect.
smbclient -L localhost
The command lists shares from the localhost. The Raspberry Pi Samba Server you are using. We could also use a Windows client and authenticate as one of the uses we have created.