Setting up a SAMBA Server on Raspberry Pi

You may be looking a NAS, Network Attached Storage, for your home network. You may find that the Raspberry Pi provides a more than adequate solution with external storage. The Raspberry Pi is very quiet with no fans and takes little power to run, so is not going to break the ecological for monetary banks. Using external drives to act as the storage the samba server will provide file and printer services to Windows clients on the network

Installing Samba

As with many of these tutorials the examples provided will start with an install. 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

From the above commands you can see that we first run the update and then we install 2 packages, samba and samba-common-bin. We will see later that the common-bin package is required if we would like to use testparm (/usr/bin/testparm) to verify samba configuration before a restart.

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(/bin/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

From the above command we can see the easy part, creating the directory /data. As it 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 date 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.

sudo bash

Now at the root prompt, and in the /etc/samba directory, we can filter the contents of the backup file back into the smb.conf. we can use grep (/bin/grep) for this.

grep -ve ^# -ve ‘^;’ -ve ^$ smb.conf.2103-11-26   >  smb.conf

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 333 lines in the original file to 34 lines in the new file. This way, the result is quick and we have not lost out documentation.

Adding a share definition

We can now edit the /etc/samba/smb.conf and add in the /data directory that we wish to share.


        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.

Restarting Samba

Once we have saved the file we should test the configuration using testparm(/usr/bin/testparm). This was added with the package, samba-common-bin.


This checks the samba configuration file. If not errors are reported we can then restart the server.

service samba restart

This will restart both the smbd and nmbd. The 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(/usr/bin/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(/usr/bin/pdbedit).

pdbedit -L

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(/usr/bin/smbclient) we can list shares on a server or even connect.

smbclient -L localhost

The command lists shares from the localhost, as we can see from the following screenshot.

The video for this demonstration can be viewed on YouTube. The PDF can be downloaded from here

This entry was posted in Raspberry Pi, SAMBA. Bookmark the permalink.