Setting up Samba on Raspberry Pi 5

Experimenting with a Raspberry Pi 5 to create a NAS device using Samba for file sharing.

8 min read
#homelab#raspberry-pi

Cover - Setup Samba on Raspberry Pi 5

Introduction

Having a network-attached storage (NAS) device can be very useful for storing and sharing files across a network. I thought it would be a good idea to set up a NAS using a Raspberry Pi 5 and Samba to learn more about how it works. In this article, we will see how to set up Samba on a Raspberry Pi 5. We also need to consider that a NAS device created using a Raspberry Pi 5 may not be as powerful as a dedicated NAS device, but it can still be useful for small-scale file sharing.

Requirements

To follow this article, you will need the following:

  1. Raspberry Pi 5 (Any model with a network interface will work)
  2. MicroSD card (16GB or more)
  3. Power supply for Raspberry Pi (preferably 5V 3A)
  4. External storage (SSD or hard disk)
  5. Active USB adapter (if using a USB hard disk)
  6. Ethernet cable (optional but recommended for faster and more reliable connection)

Step 1: Setup External Storage

Connect the external storage device to Raspberry Pi. If you are using a USB hard disk, make sure to use an active USB adapter to provide enough power to the hard disk. If you are using an SSD, you can directly connect it to the Raspberry Pi.

To identify the external storage device, you can use the following command:

lsblk

This will list all the block devices connected to the Raspberry Pi. You should see the external storage device listed in the output.

lsblk output
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sdb           8:16   0 465.8G  0 disk
└─sdb1        8:17   0 465.8G  0 part
mmcblk0     179:0    0  29.8G  0 disk
├─mmcblk0p1 179:1    0   512M  0 part /boot/firmware
└─mmcblk0p2 179:2    0  29.3G  0 part /

In this example, the external storage device is /dev/sdb.

Now if the external storage device is already formatted, we can directly mount it to a directory. If it is not formatted, you can format it using the following command:

sudo mkfs.ext4 /dev/sdb1

This will format the external storage device with the ext4 filesystem. Replace /dev/sdb1 with the correct device name.

Next, create a directory where you want to mount the external storage device. For example, you can create a directory named share in the mount directory.

sudo mkdir /mnt/share

Now we can mount the external storage device to the directory we created using sudo mount /dev/sdb1 /mnt/share. But we need to make sure that the external storage device is mounted automatically when the Raspberry Pi boots up. To do this, we need to add an entry to the /etc/fstab file.

First, find the UUID of the external storage device using the following command:

sudo blkid /dev/sdb1

The output will look something like this:

blkid output
/dev/sdb1: UUID="d2fb3a86-6d18-4e3c-876e-005569878cb5" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="25877b7d-5e70-4880-9bb0-dd17ab64f16e"

Copy the UUID value, modify the /etc/fstab file, and add an entry like this:

/etc/fstab
UUID=d2fb3a86-6d18-4e3c-876e-005569878cb5 /mnt/share ext4  defaults  0 2

Replace the UUID value and mount directory with the correct values. Save the file and run the following command to mount the external storage device:

sudo mount -a

Now the external storage device should be mounted to the directory we created. You can verify this by running the df -h command:

df -h

This will list all the mounted filesystems. You should see the external storage device mounted to the directory we created.

df -h output
Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           806M  7.9M  798M   1% /run
/dev/mmcblk0p2   29G  3.6G   24G  14% /
tmpfs           4.0G   16K  4.0G   1% /dev/shm
tmpfs           5.0M   48K  5.0M   1% /run/lock
/dev/mmcblk0p1  510M   56M  455M  11% /boot/firmware
/dev/sda1       458G  387G   48G  89% /mnt/share
tmpfs           806M     0  806M   0% /run/user/1000

Here, the external storage device is mounted to /mnt/share.

Step 2: Install and Configure Samba

Now, its time to install Samba on the Raspberry Pi. Install Samba and some additional utilities using the following command:

sudo apt update
sudo apt install -y samba samba-common-bin

Once the installation is complete, we need to configure Samba. First, create a backup of the original Samba configuration file:

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

Then modify the /etc/samba/smb.conf file using a text editor like nano:

/etc/samba/smb.conf
[samba_share]
  comment = Samba Share
  path = /mnt/share
  browseable = yes
  writable = yes
  create mask = 0755
  directory mask = 0755
  valid users = pi

Replace the path value and valid users value with the correct values.

Now, ensure that pi user (or the user you want to give access to the Samba share) has access to the Samba share directory:

sudo chown -R pi:pi /mnt/share

Next, ser a Samba password for the user:

sudo smbpasswd -a pi

You will be prompted to enter a password for the user. Enter the password and confirm it.

Finally, restart the Samba service to apply the changes:

sudo systemctl restart smbd

Now, the Samba share should be accessible from other devices on the network.

Step 3: Access Samba Share

To access the Samba from a macOS machine, open Finder and click on Go > Connect to Server. Enter the address like smb://<RPI_HOSTNAME_OR_IP>.local/samba_share and click Connect.

Connect to Samba Share on macOS

You will be prompted to enter the username and password. Enter the username and password you set earlier and click Connect.

Enter Username and Password

The Samba share should now be mounted on your macOS machine.

Workflow

Here is a sequence diagram that shows the workflow of reading and writing files to the Samba share on Raspberry Pi:

This diagram shows how the user device interacts with the Samba server to read and write files to the external storage. The Samba server acts as an intermediary between the user device and the external storage.

Experiments

Objectives

  • Measure sequential read and write performance of the Samba share on Raspberry Pi 5.
  • Analyze the performance based on different block sizes.
  • Measure the CPU load during read and write operations.

Test environment

  • Raspberry Pi 5, 8GB RAM
  • External Storage: Samsung T5 SSD, 500GB (Connected using USB 3.0)
  • Network: Ethernet connection, 1 Gbps link speed
  • User Device: MacBook M2 Air

Methodology

Test Types

  1. Sequential Read Performance
# on Raspberry Pi (create test file)
dd if=/dev/zero of=dummyfile bs=1G count=10
 
# on User Device (read test file)
dd if=./samba_share/tests/dummyfile of=/dev/null bs=<BLOCK_SIZE> status=progress
  1. Sequential Write Performance
# on User Device
dd if=/dev/zero of=./samba_share/tests/dummyfile_write bs=<BLOCK_SIZE> count=<COUNT>

Test Parameters

  • File size: 10GB
  • Block sizes: 1KB, 1MB, 4MB, 16MB, 1GB

Results

Sequential Read Performance

Block SizeTime TookAverage SpeedCPU Load
1KB112s92 MB/s29%
4MB92s110 MB/s5%
16MB96s106 MB/s5%
1GB97s106 MB/s4%

Sequential Write Performance

Block SizeTime TookAverage SpeedCPU Load
1KB95s108 MB/s23%
1MB94s108 MB/s2%
4MB95s108 MB/s2%
1GB94s108 MB/s2%

Observations

  • The read and write speed of the Samba share is around 100 MB/s.
  • The read and write speed is consistent across different block sizes.
  • The CPU load is minimal during read and write operations.
  • The Ethernet connection is the bottleneck for the read and write speed (Because the baseline speed to the external storage is around 400 MB/s).

Conclusion

Here, we have setup basic Samba share on a Raspberry Pi 5. We can further customize the Samba configuration based on our requirements. This setup can be useful for creating a simple NAS device for small-scale file sharing. We can also explore other features of Samba to enhance the functionality of the NAS device.