Setting up Samba on Raspberry Pi 5
Experimenting with a Raspberry Pi 5 to create a NAS device using Samba for file sharing.
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:
- Raspberry Pi 5 (Any model with a network interface will work)
- MicroSD card (16GB or more)
- Power supply for Raspberry Pi (preferably 5V 3A)
- External storage (SSD or hard disk)
- Active USB adapter (if using a USB hard disk)
- 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:
This will list all the block devices connected to the Raspberry Pi. You should see the external storage device listed in the output.
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:
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.
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:
The output will look something like this:
Copy the UUID value, modify the /etc/fstab
file, and add an entry like this:
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:
Now the external storage device should be mounted to the directory we created. You can verify this by running the df -h
command:
This will list all the mounted filesystems. You should see the external storage device mounted to the directory we created.
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:
Once the installation is complete, we need to configure Samba. First, create a backup of the original Samba configuration file:
Then modify the /etc/samba/smb.conf
file using a text editor like nano
:
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:
Next, ser a Samba password for the user:
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:
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
.
You will be prompted to enter the username and password. Enter the username and password you set earlier and click Connect
.
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
- Sequential Read Performance
- Sequential Write Performance
Test Parameters
- File size: 10GB
- Block sizes: 1KB, 1MB, 4MB, 16MB, 1GB
Results
Sequential Read Performance
Block Size | Time Took | Average Speed | CPU Load |
---|---|---|---|
1KB | 112s | 92 MB/s | 29% |
4MB | 92s | 110 MB/s | 5% |
16MB | 96s | 106 MB/s | 5% |
1GB | 97s | 106 MB/s | 4% |
Sequential Write Performance
Block Size | Time Took | Average Speed | CPU Load |
---|---|---|---|
1KB | 95s | 108 MB/s | 23% |
1MB | 94s | 108 MB/s | 2% |
4MB | 95s | 108 MB/s | 2% |
1GB | 94s | 108 MB/s | 2% |
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.