How To Automatically Mount A Network Share In Ubuntu

Mounting a shared drive in Ubuntu can be quite a confusing process. With many of us having multiple machines and NAS drives at home, auto mounting network locations is becoming a very common requirement in most homes and offices. I have a NAS drive at home that I use for multimedia and I make Ubuntu auto mount the NAS as soon as I log on. Here’s how I do it:

First of all you need to run this command in terminal:

sudo apt-get install gvfs-bin

Then, once the application is installed you need to paste the text below into a text editor window and then save it as .ShareMount.sh in your home folder. The file name needs to be exactly the same, it’s case sensitive and requires the dot at the start of the name.

gvfs-mount smb://%Path_to_share%

NOTE: Where “%path_to_share%” is listed this will be replaced with the actual share path. So, if there is a share called ‘share’ on computer01 then the actual command would be gvfs-mount smb:computer01/share

Once you have your .ShareMount.sh file in your home folder (you may have to press ctrl+h to show hidden files in order to see it) right click on it, select properties and then within the permissions tab select the “Allow executing file as program” check box.

Now, double click on the file and select the run button. You should now see the share mount within Ubuntu – this means that our script is working.

The only thing left to do now is create a new start up item so that the scripts runs when you log in. To do this, click on the dash and search for “start” then open Startup Applications. Click the add button and fill it in the fields exactly as shown below:

Name: Share Mount
Command: /home/<username>/.ShareMount.sh
Comment: This will mount my share on boot.

In the command section, please ensure that you replace <username> with your exact username. If you don’t know this you can find this out by opening terminal. The prompt in terminal will say <username>@<computername>:~$. So the bit before the @ symbol is your username.

That’s it. All you need to do now is reboot your machine to test that it works. Not quite as simple as Windows but simple nonetheless. You also learnt how to make a basic script. :)

How To Automatically Mount A Network Share In Ubuntu
User Rating: 2.9 (4 votes)
  • Dominik M

    How do i do this, if the network shrare have a password?

    • http://www.refugeeks.com/ Kev Quirk

      I don’t know for certain as I don’t password protect any network shares. I manage access too them via user permissions. However, it will do one of two things I imagine.

      1) It will mount anyway and prompt for a password when you try to access it.
      2) It will ask for a password when it mounts.

      After the first time you can set the credentials to be remembered in the keychain so you don’t get prompted every time you boot.

      • Anupal

        Hi, I also want to map a certain folder which can be accessible from username password i created in windows. The same above i followed but when i click on the mount drive then It says you don’t have permission to access. It don’t ask me any username and password to enter. Please help me in this.

  • Patch

    Hey thanks quick easy and works.

  • Andrew McGlashan

    You have some problems with file names…..

    “save it as .ShareMount.sh in your home folder. The file name needs to be exactly the same, it’s case sensitive and requires the dot at the start of the name.”

    “Command: /home//.Sharemount.sh”

    Also, can’t you just use $HOME for the path? And why does this file need to hidden or have any specific name? You could just as easily call the script anything you like, so long as each reference uses the right name. And you should probably make the permissions 700 so that only the user can run the script.

    Oh and to make it even simpler, couldn’t the “Command:” simply be the contents of the script file? No need for a file at all…?

    • http://www.refugeeks.com/ Kev Quirk

      Using the script instead of putting the command straight into the startup application allows for more flexibility. If a user wants to add more locations (as I have) then they can simply add a line to the script instead of having numerous startup apps.

      Saving the script as a hidden file is just so it’s nice and neat. You won’t see it every time you go into your home folder. I like things to be tidy and means that it will just run in the background for users. The name needs to be such so that my example works. It’s not designed for advanced users or coders, it’s designed to allow ‘average Joe user’ to mount a LAN share. So the less flexibility in the example the better.

      You’re absolutely right, I could have used $HOME it didn’t even occur to me to use that environment variable – thanks for pointing it out. There are no “problems with the file names” though. They will work as is whether you use the variable or not.

      Thanks for the comment. :)

      • Andrew McGlashan

        “save it as .ShareMount.sh in your home folder”

        “Command: /home//.Sharemount.sh”

        The names don’t match …
        – case sensitivity is important, you even mention that ;)

        .ShareMount.sh is not .Sharemount.sh …

        And yes, multiple shares would lend itself to a script.

      • http://www.refugeeks.com/ Kev Quirk

        Ah crap haha. I’ll boot my computer up and edit the article. Thanks for pointing that out Andrew. I can’t believe after proof reading it and you even pointing it out, I still didn’t notice… What an idiot haha. Thanks again!

  • http://twitter.com/solodg Kevin Bessey

    yeah this is okay, but it is not as good as mounting it in /etc/fstab beacuse with that it will mount it as /media/”whatever” which is better for searching with the dash. To my knowledge the dash only checks local disks for files/folders even in your history – to my knowledge this will only mount it as a network drive for access. but mounting it as a /media/”place” will search as if it were local.
    Good job though. Nice easy script that works well.