Mount network drives in Ubuntu

"blades" by gothopotam, on Flickr

Network drives sitting in a server rack. Credit to Flickr user gothopotam.

The situation

A workstation is running Linux Mint 17 (based on Ubuntu 14.04), and the user needs permanent access to network drives stored on a Windows Server. The drives appear on Windows workstations like so:

M: Data

N: Secret Files

What we need to do is make sure that each network drive is automatically mapped and mounted on boot. Luckily for us, doing this is easy with a little help from the CIFS package and a few tweaks.

The solution

Mapping each network drive

First of all, we create directories for each network drive that we want to map. For simplicity, we’ll stick with the typical drive letters:

sudo mkdir /media/workstation/M /media/workstation/N

Next we need to modify /etc/fstab, which contains mount commands that need to be run on boot.

sudo nano /etc/fstab

Add the mount path for each network drive. For paths that contain spaces, you need to replace the space with \40 , as seen below:

# M Drive
//mywindowsserver/Data/ /media/workstation/M cifs username=windowsdomainuser,password=windowsdomainpass,domain=windowsdomainname

# N Drive
//mywindowsserver/Secret\40Files/ /media/workstation/N cifs username=windowsdomainuser,password=windowsdomainpass,domain=windowsdomainname

Notice anything? With the example above, the username & password would be blatantly displayed (clearly a bad idea).

Creating a credentials file

A better way to handle credentials is to store them in a hidden text file. We’ll create this file in the workstation’s home directory and add our credentials:

sudo nano ~./sharecredentials
username=windowsdomainuser 
password=windowsdomainpass
domain=windowsdomainname # As in 'DOMAINNAMEUserName'

Save and exit the file. For some extra security, we can change the user permissions of that file (to restrict access) and change the file’s permissions like so:

chmod 600 ~/.smbcredentials

And finally, modify our /etc/fstab lines accordingly (note that you need to use the full path):

# M Drive
//mywindowsserver/Data/ /media/workstation/M cifs credentials=/home/workstation/.sharecredentials

# N Drive
//mywindowsserver/Secret\40Files/ /media/workstation/N cifs credentials=/home/workstation/.sharecredentials

Now we can test that everything is set up correctly by running the following command:

sudo mount -a

Which should map the drives without error.

Thanks goes to this page of the Ubuntu wiki, which also gives a more comprehensive guide of the mounting process.