Thursday, April 2, 2015

Installing and Configuring Samba4 on Raspberry Pi Model B for file sharing

The task:

Today we will be installing and configuring Samba4 server on a Raspberry Pi Model B running Arch Linux Arm. Just in case you haven't installed Arch Linux Arm on your Pi, you can refer to my post on Installing Arch Linux Arm on a Raspberry Pi Model B. Besides learning how to install and configure Samba4 on the Pi, we will be creating a samba share!
       A samba share is basically a network file sharing service, that allows Windows, Mac and Linux clients to access files on the samba server. You can plug in a USB drive or a hard disk to the Pi and share it over the network with any computer! So you can think of it as a cloud storage service on your network that happens to be hosted by you! I can show you how to access your share over the Internet, but I will save that for some other time. So lets get started with our task.


Instructions contained in this article were written as a reminder to myself for a fresh install of Arch Linux Arm (Nov, 1, 2014). Since you will be doing things as root, you could brick your Pi if you do something wrong. So, please read through all the instructions carefully before attempting anything. Lastly please backup all the data of your Pi before you do anything.

If you have any questions about this article or find typos in it, please feel free to note it in your comment.

Things required:

  • Raspberry Pi B 512mb (or better) with Arch Linux Arm installed on it and is connected to the Internet. 
  • A USB drive (this will be your share) or a hard drive will also work if you have the SATA or ATA to usb converter for it.
  • Lastly you will need a mouse, keyboard, display and a USB hub to power the Hard disk if your are using one. (Mouse, keyboard, display is not required if you have a laptop or PC connected to the same network as the Pi. Use ssh instead!)

Downloading and installing Samba4:

If your Pi is connected to the same network as your computer, you can just ssh into it from your computer and wouldn't need a mouse, keyboard, and a display! Just use the ssh command in terminal or putty to create a remote connection into your Pi:

ssh root@"your-Pi's-ip-address"
Example: ssh root@

You can find your Pi's ip address by using the arp command:

arp -a

This will list the IP and MAC addresses of all the devices connected to the network. Look for a MAC address that starts with the following prefix:  "b8:27:eb" in the second column. The ip address corresponding to this MAC address is the ip address of your Pi!

Once you have found your way into the Pi's bash prompt, you can now begin downloading and installing Samba4. To do this use the pacman command at the bash prompt:

pacman -Sy samba

Package Manager or pacman will find Samba4 and all its dependencies and prompt you for your permission to download and install the package. Go ahead and enter Y at the prompt and Samba4 will be installed on your Pi. This process might take a few minutes depending on your Internet connection.

Once Samba4 is installed, we have to enable the smbd and nmbd services. To do this, use the systemctl command:

systemctl enable smbd nmbd

Now that Samba4 is installed on the your Pi, it is a good time to create a group and add users to it so you can share a samba share with them. Use the groupadd command to create a group:

groupadd groupname
Example: groupadd samba

To add users to this group, use the useradd command:

useradd -m -g groupname user
Example: useradd -m -g samba tony

To add this user to the samba database and create a password for this user to access the samba share, use the pdbedit command:

pdbedit -a -u tony

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  Notes  =-=-=-=-=-=-=-=-=-=-=-=-=
"pdbedit" might give the following error:
Can't load /etc/samba/smb.conf - run testparm to debug it

To fix this error navigate to /etc/samba/
Find the file smb.conf.default

Make a copy of smb.conf.default renaming it to smb.conf
cp smb.conf.default smb.conf

then type "pdbedit -a -u tony" again

It will ask you to create a password for the user tony

After you have successfully set the new password, you will get a summary of the user details. Remember, the password entered in the "pdbedit -a -u" for a user is only used by samba and won't affect the system login password of the user. If you want to sync the system login password of the user to samba, please refer to pdbedit. Now that we have created the group and added users to it, you have two more things to do i.e. to setup the shares and configure Samba4 to allow users to access these shares. So the next section will show you how to create shares using external storage.

Creating the shares:

There are two ways to create shares. You can either use your Pi's SD card to create one or you could use an external storage device to do this. I would recommend that you use an external storage device because you could just plug in a 1tb hard drive to the Pi and don't have to worry about having your SD card filled up. You can use the Pi's SD card, but that defies the purpose of setting up a Samba4 server in the first place. If we use the SD card for a share, it will eat up system storage as users add their stuff to the share. So in this article I will be showing you how to create a share using an external hard drive, which was formatted for an Apple device.
       Apple uses the NSF/NSF+ partitioning scheme, and Linux dose not support it out of the box so we will have to install some helpers before we are able to mount it to the system. The process for mounting a DOS partition does not require the helpers as Linux natively supports DOS FAT35 partitioning scheme. You could skip the next section if you plan to use a USB drive not formatted in NSF/NSF+ format.

Mounting a NSF/NSF+ formatted drive:

First we will install our helper program "hfsprogs." To install the program we will use the pacman command:

pacman -Sy hfsprogs

Pacman will find the program and list all its dependencies. Enter Y and press enter to start the installation. Enter Y and press enter to start the installation. Once the installation is complete, we have to connect the hard disk using a SATA to USB bridge/cable. If you are using an SSH connection instead of controlling the Pi directly, you will not be able to see if the hard disk was successfully detected by the Pi. When a hardware device is connected or removed from the Pi, it displays a status message on the connected screen. Since we don't have that, we can use fdisk command to list all connected disks:

fdisk -l

You should get a similar output:

Disk /dev/sda: 298.1 GiB, 320072933376 bytes, 625142448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: DA6265FD-C5DA-4D3F-B835-E6520CF19293

Device         Start       End   Sectors   Size Type
/dev/sda1         40    409639    409600   200M EFI System
/dev/sda2     409640 623872871 623463232 297.3G Apple HFS/HFS+
/dev/sda3  623872872 625142407   1269536 619.9M Apple Boot

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  Notes  =-=-=-=-=-=-=-=-=-=-=-=-=
sda  = disk name [sdb would be another hard disk]
sda1 = 1st partition [sdb1 = 1st partition of the sdb disk...]
sda2 = 2nd partition
sda3 = 3rd partition

Before we mount the partitions we need to create a mount point for the partition. This is very simple. To create a mount point, we will create a directory. We will use this directory to mount our partition. Best practice is to create a mount point in "/mnt". So, navigate to /mnt and create a directory in it. Make sure you restrict the use of symbols in the directory name and try to keep the length of the directory name less than 9 characters. Sometimes windows clients won't connect to the share if the mount point name is too long or if it contains special symbols. Once the mount point is created, use the mount command to mount your partition to the mount point:

mount -t hfsplus /dev/sda2 /mnt/mac

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  Notes  =-=-=-=-=-=-=-=-=-=-=-=-=
Command   option   FileSystemType    Partition    Mount point
mount     -t       hfsplus           /dev/sda2    /mnt/mac

In this example the 2nd partition of sda disk is being mounted
on the mount point /mnt/mac

After pressing enter, if the prompt returns without any output, than the mount was successful and no further action is required. If there is an output, it will give you a reason of the error and some pointers on how to fix the error. Now that we are done with mounting a NSF/NSF+ formatted drive, it is time to configure Samba4 for file sharing.

Configuring Samba4 for file sharing:

To configure Samba4, we will have edit the samba configuration file "smb.conf" with is located at /etc/samba/ The only thing we need to edit in the configuration file is the very last section. By default the last section looks like this:

# The following two entries demonstrate how to share a directory so that two
# users can place files there that will be owned by the specific users. In this
# setup, the directory should be writable by both users and should have the
# sticky bit set on it to prevent abuse. Obviously this could be extended to
# as many users as required.
;   comment = Mary's and Fred's stuff
;   path = /usr/somewhere/shared
;   valid users = mary fred
;   public = no
;   writable = yes
;   printable = no
;   create mask = 0765

In this section every thing is commented out. We can use this as a template for out configuration file. After populating the proper values I get this:

 comment = Tony's share
 path = /mnt/mac
 valid users = tony
 public = no
 writable = yes
 printable = no

Put your edited version under the commented out section and save the smb.conf file. Once it is saved, restart the samba server by entering the following command:

systemctl restart smbd.service

Now you should be able to connect to the samba share from any computer on the same network that supports samba. That's about it!