The default behavior of most Linux systems is to automatically mount a USB storage device (such as a flash drive or external drive) when it gets plugged into the computer. However, this is not the case across every distro, or sometimes configurations go awry and you may find that your device is not being automatically mounted. You may also just want your storage device to mount when you plug it in before booting.

In this guide, we'll go over the step by step instructions to configure a USB storage device to be automatically mounted on Linux. There are a couple different ways to go about this, which will be covered below, so choose whichever you find more appropriate for your scenario.

In this tutorial you will learn:
  • How to mount USB drive automatically with autofs
  • How to mount USB drive automatically via UUID
Configuring a USB drive to mount automatically in Linux
Configuring a USB drive to mount automatically in Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software autofs
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

Automatically mount USB external drive with autofs

Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.

If your external USB drive mounts when it is attached before booting you may have a line in your /etc/fstab configuration file which mounts it during the boot time. If this is the case and you plug in your external USB drive after the boot, execute as a root user:

# mount -a

However, this may not be the most reliable solution since the base device file name for your drive can be different every time you plugin your USB disk. Your base device file name for your USB disk can be anything like: /dev/sdb1, /dev/sdd1 or /dev/sdXn.

A very simple and neat solution to the problem is a tool called autofs. We'll go over the setup and configuration in the following steps. But first, you will need to install the software on your system.

To install autofs on Ubuntu, Debian, and Linux Mint:

$ sudo apt install autofs

To install autofs on CentOS, Fedora, AlmaLinux, and Red Hat:

$ sudo dnf install autofs

To install autofs on Arch Linux and Manjaro:

$ sudo pacman -S autofs
  1. To avoid any confusion whether base name for your USB block device is /dev/sdb1, /dev/sdd1 or /dev/sdXn, we can make it permanently /dev/myusb any time you plug it in. This can be done with help of udev device manager. Start by identifying the name of your USB drive:
    # fdisk -l
    This will return something like this:

    Disk /dev/sdc: 2000.3 GB, 2000396746752 bytes
    255 heads, 63 sectors/track, 243201 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0x001425a0
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdc1               1      243201  1953512001    b  W95 FAT32
    The base name for our external USB device in this example happens to be /dev/sdc, but yours may be different.
  2. Next, use udevinfo command with /dev/sdc as an argument to get model attribute:
    $ udevinfo -a -p /sys/block/sdX/ | grep model
        ATTRS{model}=="Ext HDD 1021    "
  3. Now that we have the model attribute, we can add it to /etc/udev/rules.d/custom.rules with the following line:
    SUBSYSTEM=="scsi", ATTRS{model}=="Ext HDD 1021    ", SYMLINK+="myusb%n"
  4. At this point all we need to do is restart udev device manager:
    # systemctl restart udev
  5. Now when we plug in our USB device, it will be accessible under the following base name:
    # ls -l /dev/myusb*
    lrwxrwxrwx 1 root root  3 2011-02-23 12:36 /dev/myusb -> sdc
    lrwxrwxrwx 1 root root 12 2011-02-23 12:36 /dev/myusb0 -> bsg/14:0:0:0
    lrwxrwxrwx 1 root root  4 2011-02-23 12:36 /dev/myusb1 -> sdc1
    lrwxrwxrwx 1 root root  3 2011-02-23 12:36 /dev/myusb3 -> sg3
    Please note that /dev/myusb points to a /dev/sdc1, which is exactly the partition we are interested in and we use it next to configure autofs.
  6. Configuring autofs is rather simple task. All we need to do is to edit two simple files. Let's start with master file /etc/auto.master by appending the following line:
    /media/   /etc/auto.ext-usb --timeout=10,defaults,user,exec,uid=1000
  7. Next, edit /etc/auto.ext-usb file which we included in a master configuration file configuration:

    myusb            -fstype=auto           :/dev/myusb1
  8. Restart autofs for the changes to take effect:
    # systemctl restart autofs
  9. Every time you now plug in your external USB drive, autofs will add your device to a list of Active Mount Points. Plugin your external USB drive now and execute:
    # autofs status
    Configured Mount Points:
    /usr/sbin/automount --timeout=10 /media file /etc/auto.ext-usb ,defaults,user,exec,uid=1000
    Active Mount Points:
    /usr/sbin/automount --pid-file=/var/run/autofs/ --timeout=10\ 
     /media file /etc/auto.ext-usb ,defaults,user,exec,uid=1000
  10. Please note, although our drive is now listed as an active mount point the disk is not mounted yet! autofs only waits for user to access specified mount point directory and once that happens it will mount the filesystem. For example:
    $ cd /media/
    $ ls
    $ cd myusb
    $ ls
    lost.dir  music  picture  ps3  video mystuff
    $ cd ..
    $ ls
    From the output above you can see that myusb directory was created only when I tried to access it. Every time you now plug in you USB external disk you can instantly access it via some sort of Desktop or Bookmark shortcut.

Automatically mount USB drive by UUID

An alternative solution to the above is to mount a device automatically by its UUID.

  1. Use the following command to retrieve the UUID of all storage devices plugged into your system.
    # blkid
  2. Once you have indentified the proper UUID, edit the /etc/fstab file and append the following line:
    UUID=17c1210c-8a88-42d6-b394-03f491415d5c /mnt/usb ext4    defaults        0 0
    Of course, replace our example UUID with your own, and you may also use a different directory other than /mnt/usb if you want to mount your partition somewhere else.
  3. Your USB device should now mount automatically at boot (assuming it's plugged in). Otherwise, just run the following command to mount it at any time:
    # mount -a

Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.

Closing Thoughts

In this guide, we saw how to automatically mount a USB external drive at boot time in Linux. We used two separate methods to accomplish this task, leaving you free to choose whichever one you find most convenient. The UUID method is quicker and doesn't require any extra software, but autofs may already be used on your system to mount NFS shares and the like. If that's the case, it may be more convenient for you to add another mount point to your autofs config. The choice is yours.

Submit your RESUME, create a JOB ALERT.
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
Get extra help by visiting our LINUX FORUM or simply use comments below.