Mounting an SMB Path on Raspberry Pi or Linux Using CIFS Utilities
Accessing shared folders and files over a local network can be convenient, especially when you have a Raspberry Pi or any Linux machine as part of your network. In this guide, we will walk you through the step-by-step process of mounting an SMB path on a Raspberry Pi or Linux machine using the CIFS utilities. This will allow you to access files and directories shared on other machines, such as NAS or Windows computers, in a seamless manner.
Step 1: Install cifs-utils
The first step is to install the CIFS utilities, which will enable you to mount SMB paths. Open the terminal on your Raspberry Pi or Linux machine and run the following command:
sudo apt-get update
sudo apt-get install cifs-utils
Step 2: Create a Credentials File
To securely authenticate and access the shared SMB folder, you need to create a credentials file containing the necessary login information. Follow these steps to create the credentials file:
Open a terminal and create an empty credentials file using the `nano` text editor:
nano ~/.smbcredentials
Add the following lines to the credentials file, replacing `smbuser1`, `passwordforsmbuser1`, and `WORKGROUP` with your actual SMB username, password, and domain, respectively:
username=smbuser1
password=passwordforsmbuser1
domain=WORKGROUP
Step 3: Update Credentials File Permissions
For security reasons, we need to restrict access to the credentials file so that only the owner can read and modify it. Run the following command to set the appropriate permissions:
chmod 0600 ~/.smbcredentials
Step 4: Update the Fstab Configuration
The `/etc/fstab` file contains information about filesystems and is used to automatically mount them at boot time. We will add an entry to this file to mount the SMB share. Open the `/etc/fstab` file using the `nano` text editor:
sudo nano /etc/fstab
Step 5: Add Configuration to Fstab
Assuming you want to mount the shared folder from the SMB server at `//192.168.1.12/documents` to the local directory `/home/user1/documents`, add the following line to the end of the `/etc/fstab` file:
//192.168.1.12/documents /home/user1/documents cifs credentials=/home/user1/.smbcredentials,x-systemd.automount,iocharset=utf8,rw,uid=1035,gid=100,vers=2.1 0 0
Let’s break down the options used in the configuration:
//192.168.1.12/documents
: The remote SMB path you want to mount./home/user1/documents
: The local directory where you want to mount the shared folder.cifs
: The filesystem type for the SMB protocol.credentials=/home/user1/.smbcredentials
: The path to the credentials file you created earlier.x-systemd.automount
: This option allows the system to automatically mount the SMB share on access.iocharset=utf8
: Sets the character encoding to UTF-8, which is ideal for most cases.rw
: Mounts the share as read-write, allowing you to both read and write files.uid=1035,gid=100
: Sets the user and group IDs for the mounted share. You should replace these with the appropriate user and group IDs for your system.vers=2.1
: Specifies the SMB protocol version. If your server supports a higher version, you can change this accordingly.
Step 6: Mount the SMB Directory
Finally, to mount the SMB share without the need to reboot, you can run the following command:
sudo mount -a
This command will mount all filesystems listed in the `/etc/fstab` file, including the newly added SMB share.
Congratulations! You have successfully mounted an SMB path on your Raspberry Pi or Linux machine. You can now access the shared folder and its contents as if they were located on your local machine, making it easier to work with files and data across the network. Remember that the mount will persist across reboots, so you don’t need to remount it manually each time. Enjoy seamless access to your shared resources!