File Manager

Linux, mount external drives at login!

I recently solved an issue that has plagued me for a while in using Linux and external drives. I have a 1TB drive partitioned in  two with both using EXT4. Every time I plug in the drive, it is mounted and works fine. When I reboot, the drive and partitions are recognized but they are not mounted. I need them mounted so that my backup will function automatically.

I have recently switched to Elementary OS Luna, but I still had the same issue in Ubuntu 13.04 and 13.10. I found many different ways to mount the drives but they all required editing the FSTAB file and I prefer to leave that alone. Plus I prefer a solution that the user can do within the confines of their user account.

Below you can see that my drives are available upon reboot but not mountedUnMountedDrives

Clicking on the individual drives will mount them with no issue and they are ready for use.
MountedDrives

The solution is in utilizing the UDISKS program, UUID and a very simple script stored in your Home directory.

So a quick synopsis of what we are in the middle of. Let’s use an external USB drive as an example.  When you plug in an external drive  into a linux machine, that drive will be assigned a device  name, for instance sda1. But let’s say you remove the drive then plug it in to another USB port, well the system may reassign the device name to sdb1 this time and anything you had mapped to that drive would not be able to find it.  So, one of the many things UDISKS does is assign a “uuid” (universally unique identifier) to each drive. So that no matter how or where that drive is added to a system it will always be mapped  to the same device name.

So let’s get started!

First thing we need to do is get the device name for the drive we want to have mounted at login and we do this in the command line.

Open a Terminal and just type “mount“, you should get an output similar to the one below. Notice that my drives are listed at the bottom as /dev/sdb2 and /dev/sdb3 and they are mounted in /media/
MountCommand

Make a note for yourself of the device names or print out a screenshot.

Next, we need to know the uuid of our drive/drives and for that another terminal command;

ls -al /dev/disk/by-uuid/

uuid

As you can see from the screenshot above, the long, blue alpha/numeric code is the uuid and at the end of each line tells you which device it belongs to. Now, we have the device name and the uuid!

Next, create a folder in your home directory called “Scripts” and create a new file called “MountDrives.sh” and add the following to the file.

#!/bin/bash
  /usr/bin/udisks --mount /dev/disk/by-uuid/
  /usr/bin/udisks --mount /dev/disk/by-uuid/
exit 0

MountScript

Now, you can have one drive or as many as you want, just as long as they are listed after “#!/bin/bash” but before “exit 0“.

Next, make the file executable. From a terminal, navigate to your Home Scripts folder and type:

chmod 774 MountDrives.sh

Now, test the script…unmount your drives if they are mounted, from a terminal, navigate to your Scripts folder and type :

sh MountDrives.sh

You should see your drives mounted, now just need to make the script execute at login.

Depending on your distribution, the next step may differ, so I will use the Elementary OS screenshots, but you will get the idea.

From my Control Center, select “Startup Applications”
ControlCenter

Next, click “Add”, to add an item that will start/execute when you login. Give it a name, then for the “command” use the “browse”  button to find you script but then add “sh” with a space at the beginning and click save. It should now mount your drives at login.
Add_StartUp

2 thoughts on “Linux Mount External Drives at Login

  1. Janus

    Hi Joao

    I Couldnt get this to work – when i run – sh MountDrives.sh

    get error about udisk
    ending in See the udisks man page for details ? any ideas

  2. Jake

    With a little tweaking, works with udisks on Linux Mint 21 with zsh shell 🙂

    “`
    #!/bin/zsh
    /usr/bin/udisksctl mount –block-device /dev/disk/by-uuid/b2935aa3-f1aa-4cb5-982e-37730b194628

    exit 0
    “`

What's on your mind?