Tuesday, January 5, 2010

Mount Windows Shares on Linux (at startup)

So you can't use Linux because you have to operate in a Windows world, huh? Well, guess what... so do I. As a matter of fact, I have one of only two Linux laptops operating in our almost entirely Windows-based company. I get along just fine.

Recently, however, I made the switch from the Gnome desktop environment to XFCE. I was looking for something a little lighter and quicker, plus, I like to try new things whenever possible. Unfortunately for me, Thunar (the XFCE file manager) does not include an option to "connect to server" like it's Gnome counterpart Nautilus does. I thought, for a while, that I'd just have to suffer through the loss of the network shares that are hosted by Windows for the rest of the company.


Today, I found the answer that I'd been in search of for nearly a month. Today I stumbled on a simple but effective how-to for mounting Windows shares in Linux. Now, I feel the need to share this wisdom with others.

The first thing that you'll need to do is open a terminal window and prepare yourself for some command line fun. Next we'll add the IP address of the file server to your /etc/hosts file.

Note: these operations will need to be performed as root or using sudo.

# vi /etc/hosts

Now use the down arrow key (or 'j' if the arrow doesn't work for you) to move your cursor to the last line of the file and press the 'o' key on your keyboard. This will start a new line below your cursor and put you in to insert mode. You are now ready to add your file server information. Your entry should look something like this:

192.168.0.1 Fileserver.example.com fileserver

Use tabs for the spacing and be careful not to alter any other lines in your file. Now that you've entered the server address, you will need to save your changes. Press the 'Esc' key on your keyboard to bring up the vi command prompt and enter 'wq' to write your changes to the file and quit the vi program.

Next we'll need to create a location for the shares to be mounted. The Linux filesystem structure provides a directory named 'mnt' for housing mount points. You can really create your location anywhere, but for the purpose of this article we'll make use of the provided 'mnt' directory.

# mkdir /mnt/fileserver

Now that we have given our machine directions to the share and provided a place for the share to be mounted, we need to add the instructions that perform the mount. For that, we'll modify the /etc/fstab file so that the mount instructions will be performed during the boot process each time you start up your machine.

# vi /etc/fstab

Again, move your cursor to the last line of the file and start a new line below it. Then add the mounting instructions to the file. Your instructions will look something like this (all on one line):

//fileserver/share /mnt/fileserver cifs exec,credentials=/etc/cifspw 0 0

Use tabs for the spacing here also, and bonus points if you noticed that the instruction call for a file containing authentication credentials for the share. You can ommit ',credentials=/etc/cifspw' as well as the next step if the file share is not password protected. Now you'll need to write your changes to the file and quit vi.

So, what about those credentials? Well, we'll have to create that file also, and since it will be in a plain, human-readable format, we'll also change the permissions for the file so that only root can view or modify it after it's created.

# vi /etc/cifspw

This will start a new blank file, so there will be no need to move your cursor this time. However, you will need to enter insert mode. You want to do that by using the 'a' (for append) key. Now you're ready to enter your credentials in the following format:

username=UserName
password=PassWord

Notice that there are no tabs or spaces here, and that both the username and password are on their own lines in the file. Also make sure that you are entering the correct credentials that you use to access the file share (Hint: if you're part of an Active Directory domain, you will generally use your Active Directory username and password).

Now let's protect that credentials file from non-root access.

# chmod 600 /etc/cifspw

The final piece of the puzzle is to tell your machine to follow the mounting instructions and mount your file share to the location that you have given it. Since you are still at the command line at this point, the quickest way to do that is to use this command:

# mount -a

You could also have elected to just restart your machine. You might still want to do that to test the line that you entered in your /etc/fstab file. Either way (or both) you should now be able to change to the share location and browse the files hosted on the Windows share.

# cd /mnt/fileserver
# ls

You will also be able to browse to the share location within your file manager and other applications for normal open/save operations like with any other directory on your machine.

Hopefully this will be as helpful to you as it was to me and Thanks to starkos over at industriousone.com for this advice.