Ubuntu Technical

Ubuntu technical problems and solutions reference, a modern cookbook.

Ubuntu auto mount drives on login

I know there are a lot of people who are adding their drives in /etc/fstab. I personally don’t like that approach, because in Nautilus I will see 2 copies of the same drive. One mounted and the other unmounted and when it gets pressed I got an error saying that the drive is already mounted.
I prefer a solution where the drive gets mounted exactly as Nautilus does it, when the drive is mounted by simply pressing the unmounted drive.

First we need to find out where each drive is located, so that we know where each drive is located on the disk. Using the following command, we get the desired outcome:

ls /dev/disk/by-label -lah

The output look something like this:

lrwxrwxrwx 1 root root  10 2011-11-13 14:58 Storage -> ../../sda6

Assuming we need to auto mount the drive called Storage, we create it’s mounting point:

sudo mkdir /media/Storage

Now, a script needs to be created which mounts the drive:

vi ~/mountscript.sh
....
#!/bin/bash
sudo mount /dev/sda6 /media/Storage

Need to make the script executable and then test it

sudo chmod +x mountscript.sh
./mountscript.sh

You will notice that the script requires us to introduce the password. That’s not good when we are going to add this script to be run at start-up. So we need to exclude the 2 commands we are using (mount and the script we’ve just created) from sudo to ask us for the password.

sudo visudo

Add this line at the end replacing your user with the name of your own user:

your user   ALL=(ALL) NOPASSWD: ~/mountscript.sh, /bin/mount

Now both commands, mountscript.sh and mount are excluded from being prompted a password.

All you need to do is to add mountscript.sh to your start-up scripts and log-out and back in again.


Flattr this

Advertisements

10 responses to “Ubuntu auto mount drives on login

  1. Devesh Agarwal November 28, 2011 at 8:04 am

    hi arzki,
    I did as you describe above but my drive is not automounting and when i try to run “sudo visudo ” command or try to run script using ch mod then got an error saying

    “>>> /etc/sudoers: syntax error near line 28 <<<
    sudo: parse error in /etc/sudoers near line 28
    sudo: no valid sudoers sources found, quitting"

    now please tell me what to do?

    i am able to mount my drive using Nautilus.

    Thanks in advance.

    • arzki November 28, 2011 at 9:17 am

      Hi

      I will investigate this after gatting back from the office.
      Thanks

      Edit: You should check this answer
      If you just created the script I told you about (without changes in the sudoers) and that script does return this error, then the link might help you.

  2. Devesh Agarwal November 28, 2011 at 2:33 pm

    Thank you for quick reply but i have done by using live usb.
    i did it almost the same way as the link says but in a different manner.

    • Devesh Agarwal November 28, 2011 at 2:38 pm

      Sorry for double commenting but can you tell me how can i automount my drives if this error comes again because this error comes when making changes as you described above.
      and after using solution i have to remove that line from sudoers.

      • arzki November 28, 2011 at 3:13 pm

        Are you able to run the script which mounts?
        This is the only thing which needs to be in the script:

        #!/bin/bash
        sudo mount /dev/sda6 /media/Storage

        Where /dev/sda6 is your drive to mount (you can get this from the output of the command: ls /dev/disk/by-label -lah) and /media/Storage is the directory created by you, where you want to mount the drive.

      • Devesh Agarwal November 28, 2011 at 4:48 pm

        no worries,
        now i use mount manager for automounting.
        Thanks for ur support.

  3. Thorsten November 7, 2012 at 4:45 pm

    Hi, all is going well here and ofcourse every one is sharing information,
    that’s actually fine, keep up writing.

  4. ubuntu_buff December 1, 2012 at 8:37 am

    the problem reported by Devesh is because of the relative address given for mountscript.sh… the address should be changed to : /home/your name/mountscript.sh

  5. Alexander Sashnov March 29, 2013 at 5:35 am

    Why just don’t use ‘udisks’ programm?

    $ udisks –mount /dev/sdc1

    in autostart.

  6. Kees Boekel February 3, 2015 at 9:18 pm

    I have been looking for a solution where every user will mount his/her own home share on logon. Tried fstab, but thats for all users. autostart scripts didn’t work etc. Finally found this after more than a year.
    How to automount CIFS/NFS share on Linux upon logon?
    https://www.sit.auckland.ac.nz/How_to_automount_CIFS/NFS_share_on_Linux_upon_logon%3F
    Steps:
    1. Install pam_mount
    On Debian/Ubuntu:
    apt-get install libpam-mount

    On RHEL/CentOS/Fedora:
    yum install pam_mount

    2. Configure pam_mount
    2.1 For CIFS/SMB:

    Example: mounting smb://files.fos.auckland.ac.nz/$USER on /home/$USER

    /etc/security/pam_mount.conf.xml:

    <!– The elements determine which options *may* be specified for . –>


    – username = %(USER) (a specific variable for pam_mount to be substituted with the logged in user)
    – workgroup = $Domain
    – mountpoint = (where to be mounted)
    – path = $Share
    – server = $Server (CIFS/SMB server)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: