In a network environment, it’s common to share folders between different operating systems to facilitate file and resource exchange. Let’s see how to connect to a Windows shared folder from a Raspberry Pi, allowing you to access and manage files.
If you are using a Raspberry Pi and need to access a shared folder on a Windows system, the SMB (Server Message Block) protocol, also known as CIFS (Common Internet File System), is the right tool for the job.
- SMB (Server Message Block) is a network protocol used for sharing files, printers, and other resources on a local network.
- CIFS (Common Internet File System) is an implementation of SMB that allows machines to access files and folders remotely.
Windows uses SMB/CIFS to share files over a network, making it easy to access these folders from Linux-based systems like the Raspberry Pi.
Prepare the Shared Folder in Windows
First, we must configure the shared folder in Windows. To do this,
- Right-click on the folder you want to share.
- Select Properties and go to the Sharing tab.
- Click Share and then select the users you want to share the folder with or choose Everyone for broader access.
- Click Apply and OK to save the changes.
You must remember the network path of the shared folder. It generally has the format \\PCName\FolderName or \\IPAddress\FolderName.
For example, if your PC’s IP address is 192.168.1.100 and the shared folder is named Documents, the network path will be \\192.168.1.100\Documents.
Install the CIFS Client on the Raspberry Pi
To access the shared folder from your Raspberry Pi, you will need to install the CIFS client. First, we ensure the system is up to date:
sudo apt update sudo apt upgrade
Now, run the following command to install the necessary package for working with CIFS:
sudo apt install cifs-utils
Mount the Shared Folder
To mount the shared folder on your Raspberry Pi, follow these steps:
First, create a Mount Point. The mount point is a directory on your Raspberry Pi where the shared folder will be accessed. Create a directory for it:
sudo mkdir /mnt/shared
Where shared is any name you want to give to your folder. Now use the mount command to connect to the shared folder.
sudo mount -t cifs //PCName/FolderName /mnt/shared -o username=user,password=password
- Replace
PCNamewith the name of your PC or its IP address. - Replace
FolderNamewith the name of the shared folder. - Replace
userandpasswordwith the access credentials if necessary.
If the shared folder does not require authentication, you can omit the username and password options.
You can now use the shared folder through the mount point:
ls /mnt/shared
You should see the contents of the shared folder in this directory.
Persistent Configuration
To have the shared folder mount automatically on each boot, add an entry to the /etc/fstab file.
Open the /etc/fstab file with a text editor:
sudo nano /etc/fstab
Add the following line to the end of the file:
//PCName/FolderName /mnt/shared cifs credentials=/home/pi/.smbcredentials,uid=pi,gid=pi 0 0
- credentials=/home/pi/.smbcredentials: File containing the username and password.
- uid=pi,gid=pi: Adjusts permissions for your user on Raspberry Pi.
Create the Credentials File
Create a file named .smbcredentials in the directory of the Raspberry Pi user you are using and add the credentials:
nano /home/pi/.smbcredentials
Where pi is the name of the user you want to configure (by default it is pi on Raspberry Pi).
Add the following lines to the file:
username=user
password=password
Save the file and adjust its permissions:
chmod 600 /home/pi/.smbcredentials
Apply the changes in the fstab file with:
sudo mount -a

