Samba (SMB/CIFS)
Samba (SMB/CIFS)
Guide on setting up Samba on a Raspberry Pi
What you will need
This project assumes that you have already followed our Raspberry Pi installation project, and have the resulting configured Raspberry Pi. If you are installing on different hardware or a different OS, these steps may not all be the same for you.
System Preparation
Before continuing with the project, make sure your system is up to date.
sudo apt updatesudo apt upgrade -yInstall a text editor of your choice. Here we use vim as an example.
sudo apt install vimConcepts
What is Samba?
Samba is a free, open-source software suite that facilitates the sharing of folders between Windows, macOS, and Linux systems over a network using the Server Message Block (SMB) protocol.
What is SMB/CIFS?
SMB (Server Message Block) is a network protocol used to share files, printers, and other resources between computers. It operates using a client-server model, where a client connects to a server to access shared resources over a network.
Modern SMB typically communicates over TCP port 445 and supports features such as authentication, access control, and encryption.
CIFS (Common Internet File System) refers to an older dialect of SMB, commonly associated with SMB1. Although the terms SMB and CIFS are sometimes used interchangeably, modern implementations generally use SMB2 or SMB3, which offer improved performance and security.
Basic Storage Commands1/2
Here are some basic storage commands that are commonly used when working with Samba and shared directories:
ls: List the contents of a directory.cd: Change the current directory.mkdir: Create a new directory.rm: Remove a file or directory.cp: Copy files or directories.mv: Move or rename files or directories.chmod: Change the permissions of a file or directory.chown: Change the owner of a file or directory.
Basic Storage Commands2/2
Samba
Package Installation
To install Samba on your Raspberry Pi, use the following command:
sudo apt install samba samba-common-bin smbclient cifs-utilsIn order to also run the tcpdump command to show network traffic at the end of this guide, you will need to install the tcpdump package:
sudo apt install tcpdumpConfigure Network Connection1/3
For the purposes of our demonstration, we are going to create a local network connection for our Pi’s to communicate on so that we can demonstrate file sharing using Samba. If you are using a different network setup, you may need to skip or adjust these steps accordingly.
-
Check the existing network connections:
Checking Network Connections nmcli connection show
Configure Network Connection2/3
-
Create a new network connection:
Replace
<IP_ADDRESS>with an IP address for your Pi. For the purposes of our demonstration, we will assign each Pi a static IP address within the10.10.10.xrange, such as10.10.10.11,10.10.10.12, etc.Creating Network Connection sudo nmcli connection add \type ethernet \ifname eth0 \con-name samba-lab \ipv4.method manual \ipv4.addresses <IP_ADDRESS>/24 \ipv4.never-default yes \ipv6.method disabled
Configure Network Connection3/3
-
Activate the new network connection:
Activating Network Connection sudo nmcli connection up samba-lab -
Verify the network connection:
Verifying Network Connection nmcli connection show samba-labChecking IP Address ip -4 addr show eth0
-
In the top right, click the network icon and turn off Wi-Fi to ensure the Pi uses the wired network connection
-
Test the network connection by pinging another device on the same network:
Testing Network Connection ping <OTHER_DEVICE_IP>
Share a Folder from your Raspberry Pi
The first step is to create a folder to share. For the purposes of this guide, we will create a folder called shared in the home directory of the current user (which in this guide we assume is itclub):
cd ~mkdir sharedchmod 0700 sharedSet Samba Password for User
sudo smbpasswd -a itclubConfigure Samba Share1/3
-
Open the Samba configuration file for editing:
Configuring Samba Share sudo vim /etc/samba/smb.conf -
Add the following under the
[global]section in thesmb.conffile:interfaces = lo eth0bind interfaces only = yes
Configure Samba Share2/3
-
Add the following at the end of the
smb.conffile to define the shared folder:Defining Shared Folder [shared]path = /home/itclub/sharedbrowseable = yesread only = novalid users = itclubcreate mask = 0600directory mask = 0700
Configure Samba Share3/3
-
Test the Samba configuration:
Testing Samba Configuration sudo testparm -s -
Restart the Samba service to apply the changes:
Restarting Samba Service sudo systemctl restart smbd
-
Verify that the Samba service is running:
Verifying Samba Service sudo systemctl status smbd
Access the Shared Folder
To access the shared folder from another device on the same network, use the following steps:
-
On a Linux machine (such as another Raspberry Pi), you can use the
smbclientcommand:Accessing Shared Folder from Linux smbclient //<IP_ADDRESS>/shared -U itclubReplace
<IP_ADDRESS>with the IP address of the Raspberry Pi hosting the shared folder you want to access. -
Enter the password for the
itclubuser when prompted.
Using smbclient Commands1/2
put: upload files from your local machine to the shared folder.ls: list the contents of the shared folder within thesmbclientprompt.get: download files from the shared folder to your local machine within thesmbclientprompt.
Using smbclient Commands2/2
-
Create a file on your local machine:
Creating a File on Local Machine cd ~echo "Hello from <your-first-name>!" > <your-first-name>.txtReplace
<your-first-name>with your actual first name. -
Upload the file to the shared folder using
smbclient:Uploading File to Shared Folder smbclient //<IP_ADDRESS>/shared -U itclub -c "put <your-first-name>.txt" -
Verify that the file has been uploaded to the shared folder using
smbclient:Listing Files in Shared Folder smbclient //<IP_ADDRESS>/shared -U itclub -c "ls" -
Once someone else has uploaded a file to your shared folder, verify the contents of the shared folder on your local machine:
Verifying File in Shared Folder ls -l /home/itclub/shared -
Verify the contents of the file on your local machine:
Verifying File Contents cat /home/itclub/shared/<their-first-name>.txt
Capture Samba Network Packets
To capture network packets related to Samba traffic, you can use the tcpdump command. For example, to capture packets on the eth0 interface, use:
sudo tcpdump -i eth0 -nn 'arp or tcp port 445'You might see:
- ARP requests and replies: Mapping an IP address to a MAC address.
- TCP SYN, SYN-ACK, and ACK: Establishing the TCP connection.
- TCP port 445: Traffic associated with the SMB service.
Additional Resources
Up next
Self Hosted Git