In a network environment, it is common to share folders between different operating systems to facilitate file and resource sharing. 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 to share 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 on a network, making it easy to access these folders from Linux-based systems like the Raspberry Pi.
Prepare the Shared Folder on Windows
First, we need to set up the shared folder on Windows. To do this,
- Right-click on the folder you want to share.
- Select Properties and go to the Sharing tab.
- Click on 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 should remember the network path of the shared folder. It usually has the format \\ComputerName\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, ensure that the system is updated:
sudo apt update
sudo apt upgrade
Now, run the following command to install the necessary package to work with CIFS:
sudo apt install cifs-utils
Mount the Shared Folder
To mount the shared folder on your Raspberry Pi, follow these steps:
- 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 this:
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 //ComputerName/FolderName /mnt/shared -o username=user,password=password
Replace,
ComputerName
with the name of your PC or its IP addressFolderName
with the name of the shared folderuser
andpassword
with the access credentials if necessary.
If the shared folder does not require authentication, we 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 ensure that the shared folder mounts automatically on each boot, add an entry in the /etc/fstab
file.
Open the /etc/fstab
file with a text editor:
sudo nano /etc/fstab
Add the following line at the end of the file:
//ComputerName/FolderName /mnt/shared cifs credentials=/home/pi/.smbcredentials,uid=pi,gid=pi 0 0
credentials=/home/pi/.smbcredentials
: File that contains the username and password.uid=pi,gid=pi
: Adjusts the permissions for your user on Raspberry Pi.
Create the Credentials File
Create a file named .smbcredentials
in the home 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 the permissions:
chmod 600 /home/pi/.smbcredentials
Apply the changes in the fstab
file with:
sudo mount -a