Configuring Samba on Raspberry Pi, to share USB drives

Today it is time to use Samba for USB Drives sharing.

Mounting USB Drive

1. Create new directory where your drive will be mounted

sudo mkdir /media/usbHDD
sudo chmod 744 /media/usbHDD

2. List your storage devices and save UUID of your drive

ls -l /dev/disk/by-uuid/

(Update) more readable command:

sudo lsblk -o UUID,NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL,MODEL
or
sudo blkid

3. Mount that drive to /media/usbHDD, use proper user and group (uid, guid), adjust drive path

sudo mount -o uid=pi,gid=pi /dev/sda1 /media/usb

4. Edit fstab

sudo nano /etc/fstab

and add line with UUID of your drive

UUID=18A9-9943 /media/usbHDD vfat auto,nofail,noatime,users,rw 0 0

5. Verify if drive is mounted

ls -l /media/usbHDD

restart and verify if drive is mounted after reboot

sudo reboot now
ls -l /media/usbHDD
Update:

If you have problems with auto mount you may need to install:

sudo apt-get install exfat-fuse 
or
sudo apt-get install ntfs-3g

You may also need to change partition type in command in fstab file from vfat to ntfs

Installing Samba

The process of installation is very simple

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install samba samba-common-bin

Configuring new Samba share

First make backup of configuration file

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

Edit configuration file

sudo nano /etc/samba/smb.conf

Add the following lines and save the file

[drive]
comment = Public Storage
path = /media/usbHDD
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no
guest ok = no

Explanations:
[drive] – name of your share, folder with this name will be visible in network shares
path = /media/pendrive – path to your mounted drive
valid users = @users and force group = users – used user groups
create mask = 0660 and directory mask = 0771 – default permissions when file/directory will be created
read only = no – allows to write to drive, (no – disallows)
guest ok = no – disallow guest users

Add user

sudo smbpasswd -a pi

Restart Samba and verify

Changes will be applied only after Samba service restart

sudo /etc/init.d/samba restart

On Windows Machine go to Network an scan/refresh your network,
you should see new device present with name as your RPi host (in my case RPI)
it should contain two directories pi and drive.
pi is a default directory that shares users home directory – /home/pi
You should be able to read and write to your drive directory

Restricting home directory only for certain user

TODO

Performance

There are several things that limit your network performance:

  • Flash drive or HDD drive write speed – you can write as fast as your drive can, some flash drives have slow write speeds ~3-6MB/s
  • Raspberry network interface – raspberry has 100Mb/s network intercase so it can handle up to 12MB/s in my case usb HDD drive was capable of writing with such seed. In real time I got ~9MB/s write speed.
  • Samba service limitations – I read some opinions, that Samba is slow and gives about ~30-35Mb/s but in my case I got 9-11,5MB/s in reading (80-96Mb/s) and 9-10 MB/s in writing. So almost maximum what network interface can give.

Watching a FullHD 1080p video from Samba network share was flawless.
At the beginning of video playback Video player buffers some data that network connection of RPi is fully utilized (90-100Mb/s),
then in small time intervals it downloaded more video data at 32-50Mb/s
Even when jumping in video timeline there was no problem with buttering and the playback jumped without any glitch.

SMB and NMB deamons

When I inspected the process list on my RPi to check how much of processor power Samba is using during file transfer I noticed that there are smbd and nmbd processes.
So I looked for an answer what they are:

NMB is the NetBIOS name resolution protocol. When you configure your
system for Samba, it will run both smbd and nmbd daemons. One of them (smbd)
will take care of the file transfer, etc., while the other (nmbd) will take
care of the NetBIOS name resolution thus making it possible for the hosts
to show up in your „Network Neighborhood” in the Windows boxes.

https://www.redhat.com/archives/redhat-list/2001-February/msg01240.html

Sources

Samba: Set up a Raspberry Pi as a File Server for your local network


https://www.samba.org/samba/docs/using_samba/ch06.html
https://www.samba.org/samba/docs/using_samba/appa.html
https://www.redhat.com/archives/redhat-list/2001-February/msg01240.html