Automatically Mounting Windows SMB Shares in Ubuntu v3

Mounting SMB SharesThis post is an update to two posts I wrote previously on how to make your network shares mount automatically when you start up your Ubuntu Linux computer. In this method we modify the fstab file so that when all the file systems are mounted the network shares are mounted as well.

This method works for me in Ubuntu Gusty Gibbon 7.10 and is probably the most secure. I have only tested this on a wired connection, so I don’t know exactly what will happen if you use this method when using a wireless connection.

First you have to install smbfs:

sudo apt-get install smbfs

Then create a folder inside of the /media directory to mount the share on:

sudo mkdir /media/Storage

You can call it whatever you want, I call it Storage. If you change the name remember to change it below when we edit the fstab file.

Create a credentials file in /root so that you can save your password and have it protected by the root account:

sudo gedit /root/.cifscredentials

Add the following information to the file, but change Guest to your SMB username and add your SMB password.

username=Guest
password=

Note: If your fileserver allows Guest access you can just leave the file as above. If it is password protected you have to put in your username and password.

Save and close the .cifscredentials file.

Now open up your fstab file so that you can add mounting instructions:

sudo gedit /etc/fstab

Add the following line to the bottom of the file:

//192.168.0.10/SHARENAME /media/Storage cifs auto,iocharset=utf8,uid=USER,gid=users,credentials=/root/.cifscredentials,file_mode=0775,dir_mode=0775 0 0

You have to change the following information:
Change 192.168.0.10 to the IP address or DNS name of your server
Change SHARENAME to the share you want to mount
Change USER to your Ubuntu username

The file_mode=0775,dir_mode=0775 part sets the mounted directory as read/write for all users so long as the SMB username you set in .cifscredentials has read/write access.

Now save the file and run the following command to test to see if you mount now works:

sudo mount -a

If that worked you should see a new drive icon on your desktop that lets you access your share. Now try rebooting and see if your shares mount automatically. If everything went as planned you will have a nice little drive mounted on your desktop every time you start up.

SMB Shares with Spaces in the Names

If you have a share with a space in the path you can replace the space with \040. For example:

//192.168.0.10/Storage\040130 /media/Storage\040130 cifs auto,iocharset=utf8,uid=matt,gid=users,credentials=/root/.cifscredentials,file_mode=0775,dir_mode=0775 0 0

This mounts my 130GB drive on my fileserver to a local directory called Storage 130.

28 Responses to “Automatically Mounting Windows SMB Shares in Ubuntu v3”

  1. Automatically Mounting SMB Shares in Ubuntu Linux : Matt Van Stone Says:

    [...] This article is out of date. I have written a new post for a better method. Please read my new post before trying anything in this one Mounting Windows SMB Shares v3. [...]

  2. Automatically Mounting SMB Shares v2.0 : Matt Van Stone Says:

    [...] article is outdated! I have written a new post with a better mounting method. Please read my new Mounting Windows SMB Shares v3 post before trying anything in this [...]

  3. Neglas Says:

    Is mounting the share in fstab instead of an init script necessarily BETTER? What if the share is unreachable - won’t that prevent the machine from booting, if it cannot mount the drives listed in fstab?

  4. Matt Says:

    In my experience if the drives are inaccessible then it just shoots back an error code and continues booting.

    I read a comment from a guy who said when he put his shares in his fstab file they connected automatically when he connected to wireless. I have not tested this out but that could be one benefit over a startup script.

  5. Shaan Says:

    Thanks for this! I’ve been looking for a while now for a way to do this.

    I wanted to know, is it possible to have the share mount in your home folder, so that is shown in the places menu (in ubuntu)?

  6. Matt Says:

    Thats a good question… I dont know, I’ve never tried myself. I would assume you could mount at any point by changing the /media/ path to whatever you want. I’ll give that a try.

  7. Jo Says:

    Worked ok for me (7.10) but I still had to use SMB instead of CIFS with my Freecom Network Drive.

  8. Adam Says:

    In Kubuntu Gutsy,
    Main Menu->System Settings->Advanced (Tab)->Disk & Filesystems->Administrator Mode->New->Browse->Network Folders->Browser Shares->Samba Shares->(Select Share)

    Check ‘Enable At Startup’

    You may still have to install the samba software, I’m not sure if that was out of the box or if I did it myself.

  9. Oliver Says:

    Thanks! Exactly what I was looking for :)

  10. jay Says:

    Thanks a bunch man, couldn’t get my drive to mount until i read your directions!

  11. Tommasino Says:

    These worked great for me!
    A question: what if I want to do the same with a networked Linux partition? I have a Linksys NSLU2 attached to my network, is it just a matter of replacing the cifs in fstab with smbfs??>

  12. Mahdi Sadeghi Says:

    Useful & Clear, Thank you.

  13. Blah Says:

    Hey man, thanks for a great how to. Miserable weather we are having here in calgary.

    Ever since a permanently mounted my networked shares, i get an error when i restart my pc. Basically it hangs and displays a bunch of text about it having tried to close connections and cfs… something to that effect? Does this affect you too?

  14. Root Kid Says:

    How would you modify this procedure if wanted to associate the Linux login to a specific login name/pwd on the NAS?

    My NAS is set up with multiple users/pwds with specific permissions to the directories on the NAS. So when I log in on my desktop, I want it to mount the NAS using one set of unames and pwds, when my wife logs in, I want it to mount the NAS using another set of unames and pwds.

    Thanks, your article helped me understand NAS mounting.

  15. Rasta Says:

    @Shaan

    Just create a symlink to the folder you mount the drive, like this:

    ln -s /path/to/share /home/user/link_name

    Then, your drive is safely mounted in /media, but you have a quick link to it in your home folder. Not sure if it will show up in the menu or not, I don’t use a desktop environment.

  16. Matt Says:

    Tommasino:
    I am using smb sharing on my windows server right now and it works with cifs. You can use smbfs if you install it first I believe, but I have never tested it with anything but cifs.

    Blah:
    I didn’t get the error you were experiencing, but I did start getting an error after logging in that had something to do with gnome settings manager not starting. If I waited a few minutes after booting my computer to log in I wouldn’t get this error. I think what was happening was the mount was taking a long time and it was holding up the other services from starting. I think someone else mentioned this might be an issue, but I never looked into a fix for it.

    Root Kid:
    I don’t think the configuration I have outlined here will work in your scenario. You need to look into using login scripts to mount shares. If you look in my previous post on this subject I use a script that runs when you log in as opposed to using the fstab file. You can see those instructions here. Basically you want to create a different .smbcredentials file for each of your users and make the .mountshares.sh script run when you log into linux. Make sure you ignore the last few steps where I add .mountshares.sh to the init process. You want it to run at login not boot.

  17. Kevin Says:

    I just wanted to say thanks man. I’ve found these instructions in various other places but yours are by far the most accurate and clear. Awesome!

  18. Mike Shafer Says:

    Matt,

    Thanks for the great directions for taking care of a necessary task. I’ve been working around Linux since 1995 but only with Slack and Red Hat. The Ubuntu approach is of course good security (and can be done with the other distros) but for one used to working as root with vim the clearly written details were a great help and time saver.

    Cheers!

    Mike Shafer
    Pittsburgh, PA

  19. Matt Says:

    Thanks guys. This is one task that took me quite a while to figure out how to do nicely on Linux. So I have been happy to provide my experience to others so they don’t have to spend hours troubleshooting like I have.

    It would be really nice if there was a feature built in that would allow you to do something like this more automatically.

  20. Grey Randall Says:

    Is there any way to make this share access able to all users so they can see it when they log on, or would I have to make an entry for everyone?

  21. Webhamer Weblog: Search & ICT-related blogging » links for 2009-03-26 Says:

    [...] Automatically Mounting Windows SMB Shares in Ubuntu v3 | Matt Van Stone This post is an update to two posts I wrote previously on how to make your network shares mount automatically when you start up your Ubuntu Linux computer. In this method we modify the fstab file so that when all the file systems are mounted the network shares are mounted as well. (tags: ubuntu mount smb) [...]

  22. StauthamerNet :: Staut’s Family Blog» Blog Archive » links for 2009-03-26 Says:

    [...] Automatically Mounting Windows SMB Shares in Ubuntu v3 | Matt Van Stone This post is an update to two posts I wrote previously on how to make your network shares mount automatically when you start up your Ubuntu Linux computer. In this method we modify the fstab file so that when all the file systems are mounted the network shares are mounted as well. (tags: ubuntu mount smb) [...]

  23. edpatterson Says:

    Thanks a million ! Just want I was looking for.

  24. Effective Moodle Backups - Page 2 Says:

    [...] If i remember at the college, i setup a NAS as a auto mount disk and used the onboard moodle backup script. Ubuntu tut for setting up a auto mount Automatically Mounting Windows SMB Shares in Ubuntu v3 | Matt Van Stone [...]

  25. Tony Says:

    i can’t get this to work, i’m using ubuntu 9.04, when i follow these instructions all i get is:

    mount error(13): Permission denied
    Refer to the mount.cifs(8) manual page (e.g.man mount.cifs)

    i’ve heard of the ’space’ issue in the .credentials file, but I have no space.

  26. Peter Freeman Says:

    Hi Matt,

    Thanks for your instructions. I have been successfully auto mounting a remote Network Attached Storage ( LinkSys NAS200) from a number of machines running Ubuntu 9.04 using your method.

    However, on all of the machines I have the problem of being unable to unmount them by right-clicking on the desktop icon and choosing “Unmount Volume” from the pop-up menu. In addition, all the machines do not complete a shutdown due to errors and just hang with a black screen and these messages:

    CIFS VFS: Server not responding
    CIFS VFS: No response for cmd 50 mid 35050

    I suspect that the volumes are being mounted by root which passes the user credentials to the NAS200, but which retains ownership of the mount so I cannot unmount them as a user.

    Is there a way to unmount them as root when I shutdown, or is there a way to mount them as a user instead of root?

    Cheers,

    Peter

  27. Peter Freeman Says:

    Hi Matt,

    I have solved part of the problem. I now have the ability to unmount them by right-clicking on the desktop icon and choosing “Unmount Volume” from the pop-up menu. I did this by adding the “users” option

    BEFORE

    //server/sharename /media/sharename cifs auto,iocharset=utf8,uid=username,gid=users,credentials=/root/.cifscredentials,file_mode=0775,dir_mode=0775 0 0

    AFTER

    //server/sharename /media/sharename cifs users,auto,iocharset=utf8,uid=username,gid=users,credentials=/root/.cifscredentials,file_mode=0775,dir_mode=0775 0 0

    If I manually unmount them before shutdown, my machines shutdown normally, if I leave them up, I get the errors on a black screen at the end of the shutdown process as noted above.

    So it now seems that although any user can unmount them manually, the shutdown processes is unable to unmount.

    Thanks for your help on this one.

    Peter

  28. Matt Says:

    Before 7.04 I did experience the shutdown hangs like you have. However, when I installed 7.04 and figured out this method I didn’t have that issue anymore.

    After 7.04 I changed my desktop over to Mac OS x86, so I haven’t attempted this on any version of Ubuntu since. If I have some spare time I’ll see about setting up a virtual machine with 9.04 and see if I have the same issues as you.

    I do remember that before this method worked for me there used to be a problem where in the Ubuntu boot process it would attempt to mount the drives before starting up the network. Obviously this wouldn’t work. In that setup I had created a startup script and shutdown script to mount and unmount the drives. If you can find any information on this method it might be a better route on 9.04 if my method isn’t working.

Leave a Reply