Linux Commands

Automount Drives on Linux

If you are a Linux user and want to automatically mount a drive to your system on boot, then it can be done by placing the UUID of the device and mount point path in the fstab configuration file. The fstab is a File System Table file located in the /etc directory.

Well, automounting a drive can be handy for various purposes. For instance, I intend to perform a backup of my system to an external storage device. To automate it, I need to keep the device connected with the system even on boot.

Similarly, many apps sync files directly from the system drives, if the drive gets unmounted then synchronizing those files again would be an inconvenience. Automounting automatically mounts the drive without going into the hassle of running the mount command or manually doing it from the GUI.

In this tutorial, I will be exploring how to make an attached drive automatically mounted on boot on Linux.

Automount Drives on Linux

There are a few steps that need to be performed carefully to automount the attached drive on Linux.

Note that, by default, Linux does not mount any attached drive on boot, it has to be mounted to a mount point to access its data. However, Linux distributions with desktop environments automatically mount the drives.

Note: The instructions given in this guide are performed on Ubuntu 22.04. However, given commands will work without any error on other distributions as well.

1. Find the UUID, and File System Type

To find the name of a drive, its unique identification number (UUID), and the type of file system, there are two ways. The first is using the built-in GUI applications, and the second is through the command line.

I personally prefer doing it on the terminal because it is more accurate.

sudo blkid

The blkid command is a command-line utility used to get information about the internal and external block devices.

Now, identify the LABEL of the drive, which is MyDrive in my case, and note down the UUID and file system type. Don’t forget to give your storage drive a name, because it will be easy to identify it.

The command output screenshot shows all the required information.

  • UUID = 65B1-FB17
  • File System Type = exfat

The UUID (Universal Unique Identifier) is an ID of the block device MyDrive (/dev/sda1) and the file system type is exfat.

Note that UUIDs can have a different number of characters depending upon the file system types. For example, the FAT file system UUID has 8 alphanumeric characters with a dash (), NTFS has a string of 16 characters with no dashes, and EXT has 32 alphanumeric characters with dashes.

Now, let’s do it on GUI, since I am on Ubuntu 22.04 with a GENOME desktop environment it has a default app for disk management called Disks. Open the app and click on the drive which can be recognized by the storage capacity.

2. Creating a Mount Point

To permanently mount an external drive to Linux, a mounting point needs to be created. It is a one-time setup unless you choose a different mount point in the future.

The mount point is a place where you put the file system to be accessed. It can be any directory anywhere on Linux; normally, /mnt or /media directories are used. I am making a directory in the root called /media/MyBackup, which will be my mount point.

sudo mkdir /media/MyBackup

Now, I will permanently mount my external drive MyDrive to /media/MyBackup mounting point.

We have found the name, UUID, and file system type of the attached drive, we have also created the mount point. The last step is accessing and modifying the fstab file.

3. Access and Edit the fstab File

The fstab file is a file system configuration file in the /etc directory that contains information about the mounted storage devices. It can be accessed in any text editor, but it requires superuser access to be modified.

sudo vim /etc/fstab

Now, it’s time to insert the information extracted above using the following general syntax.

[Device] [Mount-Point] [File-System-Type] [Mount-Options] [Dump] [Pass]

Explanation of all the parameters of the above syntax is mentioned below.

[Device] The device UUID
[Mount-Point] The mounting point directory from where the content of the attached drive is accessed [for more run man mount command]
[File-System-Type] The file system format type such as fat, exfat, ntfs, or ext4
[Mount-Options] Read and write option for the device (defaults is used for read and write access)
[Dump] To enable or disable the backing up of the attached device; if it is 0, the backing up is disabled

The fsck command is utilized to verify the drive for errors prior to initiating booting. For the root device, the fsck will always be 1.

The following format is applicable in most Linux distributions such as Arch Linux or Debian; however, the latest Ubuntu (22.04) has a different format which is mentioned below.

UUID=[UUID-of-Device] [Mount-Point] [File-System-Type] [Mount-Options] [Dump] [Pass]

I will insert the information extracted above in the fstab file using the above format.

UUID=65B1-F446 /media/MyBackup exfat defaults 0 0

Note: Use a tab to separate the fields instead of spaces.

I have set the [Mount-Options] to defaults, which means the device has read and write access. The [Dump] and [Pass] options are set to 0 because I don’t want to back up and on boot fsck check.

Ubuntu Format

The latest Ubuntu (22.04) has a different format for setting external drives in the fstab file.

/dev/disk/by-uuid/[UUID-of-Device] [Mount-Point] [File-System-Type] [Mount-Options] [Dump] [Pass]

Since I am using Ubuntu 22.04, I will use this method.

/dev/disk/by-uuid/65B1-F446 /media/MyBackup exfat defaults 0 0

Now, save and quit the file; I am using Vim, the :wq command will write and exit the editor.

4. Verification

To verify that all the information mentioned in the fstab file is correct, use the mount -a command.

sudo mount -a

If there is any error, the above command will display it, otherwise, there will be no output.

No error is encountered, which means the drive has successfully mounted.

The fstab files of various Linux distributions are given below for comparison.

Debian /etc/fstab File

Arch Linux /etc/fstab File

Ubuntu /etc/fstab File

Conclusion

If you are using a drive for your daily work. Especially if you are saving files in it or accessing files from it to your Linux system. Or if you are interested in backing up your system and do not want to resume after boot, then it is a good way to do it automatically.

Automounting is an approach to mounting a storage drive on boot because many Linux distributions do not mount drives on boot. It can be done by placing the UUID of the device and mount point in the /etc/fstab file.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.