Auto Mount Windows Share on Linux using CIFS

People store data in so many ways today. Some have an external USB drive, some store them in “Clouds” such as OneDrive or DropBox, and others have a server at home or on the workplace where all data is stored.

In this article we will concentrate on the last part; “Data stored on a windows network share”.
But what if you are that one Linux user? Around you are all those other windows users who has this share mounted by default, or can at least get to it in 5 seconds. What do you do?

Prerequisites

Before we start, we need to install some tools.
(If the windows share is correctly set up, nothing needs to be done here to make this work for us Linux users. If it works for them, it works for us!)

First of all we need to install the “CIFS-utils”. This is the system that will allow us to use a share over different platforms.
(Hence, this tutorial is based on Debian so if you use systems such as Fedora, Mandriva, Arch, or any other, the setup might be a bit different.)

To install “CIFS-utils” simply go to the Terminal and type;

sudo apt-get install cifs-utils

This will install every dependencies you will need for this tool.

NEXT, we need a resolving utility tool to make the Linux system able to resolve windows machine names.
This is installed by typing;

sudo apt-get install libnss-winbind winbind

 

Configure CIFS

As with almost everything, we need to modify some settings on CIFS.
There is a file in the /etc/ folder that is named “nsswitch.conf”, this needs a bit of code to make all this work.

Let’s change it by typing;

sudo nano /etc/nsswitch.conf

This will open the editor, and what we want to look for is the line that says “Hosts:”
Usually this line will say:

files mdns4_minimal [NOTFOUND=return] dns myhostname”

This needs to be changed to:

files mdns4_minimal [NOTFOUND=return] wins dns”

Click “Ctrl+O” to write the change we just did to the file, and then “Ctrl+X” to exit from the editor.
To enable the change we just did we need to restart the network service by entering;

sudo service networking restart

(Tip: I have noticed some times that it won’t work after this, so should the network service restart not work, simply reboot the computer)

Prepare for incoming folder

Now that the tool we need is installed, it’s time to create a landing folder for the network share.
We usually use the /media/ folder for this, but this is up to you.

To do so, type;

sudo mkdir /media/netshare

“Netshare” is the name we found most describing, but you can use what ever you want.

Make changes to “fstab” to auto mount on Boot

“The fstab file is a dangerous place! You must NEVER go there user

Before doing anything in this file, be SURE to make a copy of it.
We do that with the command;

sudo cp /etc/fstab /etc/fstab.bakup

This will create a copy of the file in case we mess things up and need to restore it for things to work again. Should you need to restore it, simply type;

sudo cp /etc/fstab.backup /etc/fstab

Now as the backup is present, we can start the dangerous journey into the fstab file. To edit this we simply type;

sudo nano /etc/fstab

This will open the editor, and we want to add the following line to the bottom of the file;

//IPaddress/sharedfoldername /media/netshare cifs user=,password=,iocharset=utf8,gid=0,uid=0,file_mode=0777,dir_mode=0777 0 0

(Notice: everything goes on the same line!!)

Before we add it, let’s break it down a bit and look at all the different segments.

The first section “//IPaddress/sharedfoldername” is the IP address and name of the folder on the computer where the network share is stored.

The second section “/media/netshare” is the mounting point we made on our Linux system.

The third section “cifs” is just the utility we want to use.

The forth section “user=,password=” is the credentials used to obtain the share. On this tutorial the share is public and requires no credentials so hence the “user=” and “password=” is empty. We would suggest that if you have a public share with no credentials, the folder should be at least “Read-Only“.
Should there on the other hand be credentials to get to these files, this is where it should be stored. Then it would look like; “user=brekke,password=abc123

(NO MY PASSWORD IS NOT “abc123”)

The fifth section “iocharset” is the character set used to translate the letters used and UTF8 is a universal language.

The sixth section “gid=0,uid=0” is the user and group ID on the Linux machine. This determine which user the share is going to be mounted as. I might be wrong about this section, but as long as it’s mounted by UserID 0 and GroupID 0 (which is root), everyone can reach it and access it depending on the next section. To find your UID you can type; “sudo id <a username>”

The final section “file_mode=0777,dir_mode=0777 0 0” is who is allowed to access the share. Since there is a read-only set on the folder on the windows side, none of these files can be deleted anyway. But should this section be set up with full access on the windows side, this access can be restricted here with changing “0777” to either “0744“, “0644” or “0444“. All depending on how you want it to be locked down. As this is regulated on the windows machine and is now “Read-only” we leave it at “0777“.

Finish it off!

Okay, if the changes are made and we are happy with them we write the file out with “Ctrl+O”, and exit the editor with “Ctrl+X”.
Do a reboot of the computer, and check if the network share get’s mounted in the specified mounting folder.

Should anything be wrong, do look in the “fstab” file, and pay attention to the booting sequence as this will some times reveal what is wrong.
Remember: Should it go horribly wrong, we always have that backup file to go back too and start over again.

Good luck!

NB! – NB! – NB! – NB!

DigitalBrekke is not responsible for any damage to your system/software that may occur.
Messing around in “FSTAB” without knowledge
could harm  your computer
!
– What you choose to do is on you! –