Code Review Videos > Linux > [Ubuntu] How To Auto Mount a Drive on Startup

[Ubuntu] How To Auto Mount a Drive on Startup

My computer is dual use. Gaming, and work. It runs pretty much 24/7/365, but when I play a game at night in Windows, then reboot back into Linux, I have to manually log in and mount my drive(s) or my overnight backups don’t work.

Of course, I frequently forget to log in. And even when I do, the task just feels like a total chore. First open nautilus, then click each drive (of which I have 6 excluding root), and only then can I be sure that my backups will run.

mounting a drive in nautilus on ubuntu

Now, given that I’ve lived with this setup for … years(!) at this point, the fix for this was remarkably simple. Well, by Linux standards.

Which is to say you will not need to use vim or edit your /etc/fstab or anything like that.

Nope.

GUI all the way.

How To Auto Mount Drives After Reboot Or Login On Ubuntu Desktop

I’m going to walk through the process of automatically mounting my backup disk, which I have called “silver-bak”.

To ensure that your drives are automatically mounted on reboot or login in Ubuntu Desktop, you can follow these steps:

  1. Open Disks Utility:
    • Press Super (Windows key) to open the Activities Overview.
    • Type “Disks” and select the “Disks” application from the search results.
      ubuntu super select disks
  2. Select the Drive:
    • In the Disks utility, you’ll see a list of your drives on the left sidebar. Select the drive you want to configure for automatic mounting.
      select a disk for automatic mounting on ubuntu
    • There is some helpful info here, although it is tiny and very hard to read. You can see the name ‘silver-bak’ in the middle in tiny black letters on a purple background. That gives some confidence it’s the right disk (in my case).
  3. Configure Mount Options:
    • Click on the drive you’ve selected to highlight it.
    • Then, click the gear icon (⚙️) below the Volumes section.
    • Choose “Edit Mount Options…”
      edit mount options in disks on ubuntu
  4. Adjust Mount Settings:
    • In the Mount Options dialog, you can configure how the drive is mounted.
    • You will need to untoggle the ‘Use Session Defaults’ switch to change the options.
    • Ensure that the “Mount at system startup” option is enabled.
    • You can also configure other options like mount point, file system type, and mount options according to your needs. I did not change any others.
      ubuntu disk mount options
  5. Save Changes:
    • Click the “OK” button to save your changes.
  6. Authenticate:
    • You will likely be prompted to authenticate with your administrator password to make these changes.
  7. Repeat for Other Drives:
    • If you have multiple drives you want to automatically mount, repeat the above steps for each drive.
  8. Reboot or Log Out/In:
    • After configuring the mount options, either reboot your computer or log out and then log back in to test whether the drives are automatically mounted.

Your drives should now be set to automatically mount on reboot or login. If you encounter any issues or need to make further adjustments, you can revisit the Disks utility to modify the mount options.

🤓🤓 You do not need to read the stuff below unless you want to nerd out. 🤓🤓

Review Your Changes In The File System Table

Using the approach above you do not need to edit the /etc/fstab by hand.

In my opinion, this is a good thing.

However, you can confirm your changes by viewing the file system table (fstab) if you so desire. This may take you down a bit of a rabbit hole, and you do not need to know any of this stuff.

But if you’re curious:

cat /etc/fstab

# or if you prefer a version with nicer highlighting:
vim /etc/fstabCode language: Shell Session (shell)

Which should give you something like this:

ubuntu fstab listing example

It’s that final entry that we are interested in.

The various parts of the particular entry are separated by a space. Let’s look at each in turn.

Source Block Device

You can use the lsblk command in the Ubuntu command line to list the disks in your system along with their disk models:

ubuntu lsblk output example

I don’t even know what some of them are myself.

If you haven’t yet mounted the drive in the GUI, or you are just wanting to use the command line, you can do the following:

➜  ~ cd /dev/disk/   

➜  disk ls -la

total 0
drwxr-xr-x  8 root root  160 Sep 14 15:40 .
drwxr-xr-x 21 root root 5340 Sep 14 15:40 ..
drwxr-xr-x  2 root root  860 Sep 14 16:58 by-id
drwxr-xr-x  2 root root  140 Sep 14 16:58 by-label
drwxr-xr-x  2 root root  100 Sep 14 15:40 by-partlabel
drwxr-xr-x  2 root root  320 Sep 14 16:58 by-partuuid
drwxr-xr-x  2 root root  860 Sep 14 16:58 by-path
drwxr-xr-x  2 root root  280 Sep 14 16:58 by-uuidCode language: Shell Session (shell)

This will list out all the different ways you can inspect your available disks.

Our fstab entry specifies the source block device that I want to mount as:

/dev/disk/by-uuid/6702fdd0-080a-4204-8512-c8793381369f

Instead of using the device’s name like /dev/sdd1 (which can change if you add or remove drives), it uses the Universally Unique Identifier (UUID) of the device.

The UUID is a unique identifier assigned to the file system when it’s created, so it remains constant even if you move the drive or change its position.

And you can find all of them by running ls -la /dev/disk/by-uuid:

ubuntu list disks by uuid

Mount Point

To quickly recap, the fstab line we have is as follows:

/dev/disk/by-uuid/6702fdd0-080a-4204-8512-c8793381369f /mnt/6702fdd0-080a-4204-8512-c8793381369f auto nosuid,nodev,nofail,x-gvfs-show 0 0

I know that’s hard to read. Long, not human friendly UUIDs and random non English words? What’s not to like?

We are now looking at the second part:

/mnt/6702fdd0-080a-4204-8512-c8793381369f

This part specifies the mount point, which is the directory where the file system from the source device will be attached. In this case, it’s mounted at /mnt/6702fdd0-080a-4204-8512-c8793381369f.

Note: It is mounted as the same UUID as the disk by-uuid, but it is named “silver-bak”. This is a file system label:

sudo blkid -s LABEL

/dev/sdd1: LABEL="silver-bak"
/dev/sdb1: LABEL="500gb"
/dev/sdc1: LABEL="Data"
/dev/nvme1n1p2: LABEL="games"
/dev/sda1: LABEL="Games"Code language: JavaScript (javascript)

Setting a file system label can be done by using a command similar to sudo e2label /dev/sdX1 mylabel, changing the disk and label accordingly.

Remaining File System Table Entry Options

The remaining entries map to the options chosen in the GUI.

Here is what they individually mean:

  1. auto
    This field specifies the file system type. “Auto” means that the system should determine the file system type automatically.
  2. nosuid
    This is a mount option that prevents the execution of setuid programs (programs that run with the privileges of the file’s owner). It is used for security purposes to minimise the risk of privilege escalation attacks.
  3. nodev
    This is another mount option that prevents the interpretation of character or block special devices on the mounted file system. It is also used for security purposes to prevent the execution of potentially malicious code.
  4. nofail
    This option tells the system not to report errors if the device specified cannot be mounted at boot time. It ensures that the system will continue booting even if this mount point is unavailable, which can be useful for non-essential file systems.
  5. x-gvfs-show
    This is an option specific to the GNOME Virtual File System (GVFS), which is used in some Linux desktop environments. It indicates that this mount should be displayed in the file manager or desktop environment.
  6. 0 and 0
    These values represent the dump and fsck options, respectively. The 0 in both fields means that no automatic backup should be performed, and the file system should not be checked on boot.

Wrapping Up

In this blog post, we explored the process of automating drive mounting on Ubuntu Desktop after a reboot or login.

We discussed two methods: using the graphical user interface (GUI) tools and editing the /etc/fstab file.

While both methods have their advantages, for me as an every day Linux user who just wants to get the systems admin stuff done as quickly as possible, it’s clear that using the GUI tools are more than sufficient to get your drive mounting problems solved quickly.

In conclusion, while editing the /etc/fstab file remains a powerful method for advanced users and system administrators, Ubuntu Desktop’s GUI tools provide a more accessible and safer way to auto-mount drives after a reboot or login.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.