Mount SMB Network Drives in Linux

Published 08-26-2017 20:11:38

In order to mount the drive, you need to have cifs-utils installed on your system.

sudo pacman -S cifs-utils
sudo apt-get install cifs-utils

Mounting network folders

You might have public folders on your network drive that can be accessed by anyone without having to provide any credentials. These are mounted in the same way as password-protected folders (we will mount these in the next section), but with a few different options.

First, let’s create a directory where we want to mount the directory. You will need a separate directory for each network folder that you want to mount. I will create the folders in the /media folder:

mkdir -p /media/networkshare/public

Then edit your /etc/fstab file (with root privileges) and add this line:

//192.168.0.18/publicshare /media/networkshare/public cifs guest,uid=1000,gid=1000,iocharset=utf8 0 0
  • The first part is the IP of your network drive, and the public folder you want to mount.
  • The second part is the folder on your local machine where you want to mount the network share.
  • The third part indicates what type of drive you want to mount (cifs).
  • The last part is the set of options you can pass, and here’s an explanation of the ones we are using:
    • guest is basically telling the network drive that it’s a public share, and you don’t need a password to access it (not to confuse with username),
    • uid=1000 makes the Pi user with this id the owner of the mounted share, allowing them to rename files,
    • gid=1000 is the same as uid but for the user’s group,
    • iocharset=utf8 allows access to files with names in non-English languages.

Note: If there is any space in the server path, you need to replace it by \040, for example //192.168.0.18/My\040Documents

To find the uid and gid for your username, use the following command:

id username

Now that we have added this to our /etc/fstab, we need to (re)mount all entries listed in /etc/fstab:

mount -a

Now you can access your network drive from /media/networkshare/public (or wherever you chose to mount it).

Mount password-protected network folders

Mounting a password-protected share is very similar to mounting a public ones, with some minor changes to the options we pass. Let’s start by making a new folder where we want to mount the password-protected share:

mkdir -p /media/networkshare/protected

Again, open /etc/fstab (with root privileges), and add this line:

//192.168.0.18/protectedshare /media/networkshare/protected cifs username=msusername,password=mspassword,uid=1000,gid=1000,iocharset=utf8 0 0

While this is a perfectly valid way of mounting your protected folder, it’s not very secure. Since /etc/fstab is readable by everyone, so are your credentials in it. So, let’s make it more secure by using a credentials file. This file will contain nothing else but your username and password, but we will make readable only to you. This way, other users on the system won’t be able to see your credentials.

Using a text editor, create a file that will contain the credentials for your protected network share:

nano ~/.smbcredentials

Enter your username and password for the protected share in the file:

username=msusername
password=mspassword

Save the file.

Change the permissions of the file to make sure only you can read it:

chmod 600 ~/.smbcredentials

Then edit your /etc/fstab file and change the line where we are mounting the protected share to look like this:

//192.168.0.18/protectedhare /media/networkshare/protected cifs credentials=/home/username/.smbcredentials,uid=1000,gid=1000,iocharset=utf8 0 0

Save the file.

Again, we need to (re)mount all entries listed in /etc/fstab:

mount -a

That’s it!