In this tutorial, I will show you how to share directories from your Raspberry Pi using the SMB/CIFS protocols.
Samba is a re-implementation of the SMB (Server Message Block) network protocol and allows Linux computers to integrate seamlessly into Microsoft Active Directory environments.
CIFS, or Common Internet File System, is an implementation of the SMB protocol. In modern setups, CIFS and SMB are used interchangeably, but most people use the term SMB.
Using Samba on our Raspberry Pi, we can easily share directories so that they can be accessed from almost any operating system.
Samba is one of the easiest file servers to install and configure, which makes it one of the best solutions for setting up a NAS, especially if you intend to use Microsoft Windows systems.
There are many other NAS configurations you can run on a Raspberry Pi. Personally, I prefer Samba because it is simpler to install, but you may well prefer other protocols such as AFP or NFS.
Setting up Samba on Raspberry Pi
Before setting up an SMB/CIFS share on the Raspberry Pi, the first thing to do is make sure everything is up to date.
We can update the package list and all our packages by running the following two commands.
sudo apt-get updatesudo apt-get upgrade
Now that Raspbian is fully up to date, we can install the Samba software on the Raspberry Pi.
You can install the packages needed to set up Samba by running the following command:
sudo apt-get install samba samba-common-bin
Before configuring network storage on the Raspberry Pi, we first need to create a folder to share.
This folder can be located anywhere, even on an external hard disk connected via USB. For this tutorial, we will create the directory in the “pi” user’s home directory.
Create the folder by running the following command.
mkdir /home/pi/shared
Now we can share this folder using Samba. To do so, we need to edit the Samba configuration file.
All the settings for shares are stored in the “smb.conf” configuration file.
We can start editing the configuration file by running the following command.
sudo nano /etc/samba/smb.conf
Add the following to this file. This text defines various settings for our share.
[fargionconsultingshare] path = /home/pi/shared writeable=Yes create mask=0777 directory mask=0777 public=no
“[fargionconsultingshare]”: defines the share itself; the text in brackets is the access point for the share. In this case, for example, it will be available at: //raspberrypi/fargionconsultingshare
“Path“: the path of the directory on the Raspberry Pi that you want to share.
“Writable“: when this option is set to “Yes“, the folder will be writable.
“create mask” and “directory mask“: these options define the maximum permissions for files and folders. Setting them to 0777 allows users to read, write and execute.
“public“: if set to “no”, the Raspberry Pi will require authentication with a valid user to access the shared folders.
With the changes made to the file, save it by pressing CTRL + X, then Y, followed by ENTER.
Next, we need to set up a user for our Samba share on the Raspberry Pi. Without one, we will not be able to connect to the shared network drive.
In this example, we will create a Samba user called “pi” with the password “raspberry“.
Run the following command to create the user. You will then be prompted to enter the password.
sudo smbpasswd -a pi
Finally, before connecting to our Samba share on the Raspberry Pi, we need to restart the Samba service so that it loads the changes made to the configuration file.
sudo systemctl restart smbd
The last thing to do before trying to connect to the Samba share is to find the Raspberry Pi’s local IP address.
First of all, make sure you are connected to a network, either via Ethernet cable or Wi-Fi.
Run the following command to display the Pi’s local IP address.
hostname -I
Now that we know our Raspberry Pi’s IP, all that’s left is to access the shared folder from any PC on the network.
Enjoy!
