Back in the 1990s, Microsoft developed a protocol that allows one Windows machine to access the files and folders on another Windows machine. The protocol, which is known as the Common Internet File System (CIFS) – but was originally called Server Message Block (SMB), has been implemented on other operating systems including Linux. The most popular implementation is known as Samba and it allows devices like the Raspberry Pi to act as a CIFS file server. To put it another way, it allows a Windows PC to mount a folder on a Raspberry Pi and then copy, delete, read and write files on the it.
Installing and configuring Samba on a Raspberry Pi for basic file sharing is quite simple. First the Samba software needs to be installed. To do this, use the following command:
sudo apt-get install samba samba-common-bin
All the configuration information about Samba is stored in the file
/etc/samba/smb.conf. The default configuration file only needs a little tweaking so that the user’s home directories can be shared.
The first change that needs to be made is to define what type of security mechanism should be used to control who has access to the shared files. The simplest is what is called “user-level security” and basically it means that to gain access to a shared folder the connecting client needs to provide a valid username and password.
By default, Samba exports the home directories as read-only. The second change alters this so that when you access a shared folder, you have the ability to read and write (meaning you can also create new files, delete files and so on).
Edit the Samba configuration file using:
sudo nano /etc/samba/smb.conf
Now scroll down until you find the line
# security = user and remove the hash (#) from the beginning of the line:
Scroll down further until you find the
[Homes] section and change
read only = yes to
read only = no as shown below:
Press “Ctrl + X” to leave the nano editor and type “Y” to confirm that the file should be saved. Press ENTER to keep the current filename (i.e. /etc/samba/smb.conf).
To reboot the Samba server use the following command:
sudo service samba restart
It is also possible to tell the Samba server to re-read the configuration file without restarting the server (which means any current connections won’t be lost). However the downside is that the command will report
OK even if the server isn’t running. If you are unsure about the running state of the server, use the
restart command above.
sudo service samba reload
There is one last step before you can access the shared folders. Since the server is using “user-level security”, a password needs to be defined for when CIFS is used to access files on the Raspberry Pi. To set the Samba password for the default
pi user, type:
sudo smbpasswd –a pi
This will add the user
pi to the list of Samba users and prompt for a password. You need to repeat this step for any other users you may have added to Raspbian.
Connect from Windows
You can now connect to your Raspberry Pi from a Windows machine. Open the “Computer” window (on Windows 7) or “This PC” (on Windows 8) and click the “Map Network Drive” button on the toolbar (Windows 7) or click the “Map Network Drive” button under “Computer” (on Windows 8).
In the Map Network Drive dialog, select and unused drive letter from the “Drive:” drop-down list and enter
\\raspberrypi\pi in the “Folder:” field. Check “Connect using different credentials” and click “Finish”.
pi in the Username field when you are asked to enter the network credentials. Enter the password you set previously in the password field and click OK.
Once connected you can manipulate the files on the Raspberry Pi using Windows Explorer.
By default all the user home directories are shared and as long as a Samba password has been set then every user on the Pi can access their files. It is also possible to connect to the Pi over the same “Windows networking” protocol from OS X and Linux, as both of these support CIFS.