Detect and mount USB devices in Linux from console

Pendrive usage with Linux
Image: Pendrive usage with Linux (License: CC-BY-SA Marcelo Canina)

Mount pendrives with commands in the Linux terminal

Last modified:


This is a small guide to detect a newly attached device to an USB port and mount it in Linux.

We will explore different strategies that will help when some of the tools isn't available.

First of all, some definitions:


What does exactly mount means?

To attach these new devices filesystems we use the mount command in the form: mount -t type device dir.

In the above command, Devices (block special devices1) can be indicated in one of the following three ways:

  1. Filename
    • using the filename that is associated with the device.
    • e.g.: /dev/sdb2
  2. Filesystem label
    • Using the label associated with the device.
    • e.g.: PENDRIVE
  3. UUID
    • Universally Unique IDentifier (UUID) Uniform Resource Namespace 2.
    • e.g.: ba108o135-80bf-1cci-b2za-082eafd02y0g


To list the currently mounted devices/filesystems, findmnt.

For example, in Ubuntu 18.10 it shows:

There is also the old way, maintained for compatibility only but widely used: mount -l or just mount.

Now we have just connected a pendrive, how do we know which label or UUID it has to be able to mount the filesystem?

1. Identify the newly attached device

After we plug a pendrive, we need a method to locate the new device so we can get its label or UUID.

The following list shows the available alternatives to do it, any of them would be help you find the device information, listing most complete and easier to use first.

1.1 Using blk commands

The command lsblk prints all block devices (except RAM disks) in a tree-like format by default.

We can have a look at it to try to spot the new device with the filesystem parameter: --fs to print info of each filesystem, LABELs and UUIDs on available block devices.

Example output:

To directly find out the connected pendrive, save the above listing in a temporal folder and then look for the differences with the same command after plugging the pendrive:

1.2 Inspect Kernel ring buffer

Immediately after plugging the device, we can examine the kernel ring buffer with the command dmesg

There we can look for the string of type sda, or sdb, etc, that will belong to the most recent connected device.

In this case we can spot the sda: sda1 sda2 line that indicates it has two partitions: sda1 and sda2.

Then we use the command blkid -p device to find out its UUID, label and more properties.

1.3 dev-by- directories

Inspect the directories: /dev/disk/by-{label,uuid,partuuid,partlabel}.

And a similar approach can be done to know which one was plugged in, saving the list before and after plugging the device:

2. Mount the device

Which method should we use?

The device name of disk partitions are unstable.

They depend on what (physical) slot you connect the device, and if there are other devices already attached or not, so the filename to refer to them may change over time.

Labels are more stable than filenames, but they may change or have the same name as other label.

The best way to mount a device is using the UUID, as you can be sure it won't change over time.

We will mount it at /media/usb-stick.

Create directory: sudo mkdir /media/usb-stick

And mount it with one of these methods, preferably UUID:

sudo mount -t auto /dev/sda2 /media/usb-stick/

# mount using label
# mount -L label
sudo mount -L Ubuntu 18.10

# mount using UUID
# mount -U uuid
sudo mount -U BXXX-6XXX

3. Make it permanent

There is a special file /etc/fstab, that each line describes:

  • what devices are usually mounted
  • where,
  • using which options

After reboot each line will be mounted automatically if the device is connected.

As we are working mostly with pendrives, we should use the special option: nofail. This avoids reporting any errors for the device if it does not exist at booting time when it tries to mount them and probably the device won't be plugged..

Here we use the UUID to be sure we always refer to the same device.

In /etc/fstab:

# pendrive negro kingston 2gb
UUID=2018-XX-XX-XX-44-30-00               /media/usb-stick auto defaults,nofail

3.1 Mount a pendrive just by specifying a directory

Now that fstab knows that our USB-stick should be placed at /media/usb-stick, we can mount the pendrive just by using that directory the next time we plug the pendrive:

mount /media/usb-stick

Personally, I like to have each pendrive or device identified like /media/kingstone-2gb so I can easily mount it with the directory name after plugging it.

4. Remove pendrive

When you want To finally remove the USB-stick, you need to release it from the filesystem first with umount.

sudo umount /media/usb-stick


Set pendrive label

To set create or rename the label of a pendrive there are several programs:

  • e2label - Change the label on an ext2/ext3/ext4 filesystem
  • tune2fs - adjust tunable filesystem parameters on ext2/ext3/ext4 filesystems
  • mke2fs - create an ext2/ext3/ext4 filesystem

To set a pendrive label as usb-stick

e2label /dev/sda1 usb-stick
tune2fs -L usb-stick /dev/sda1

Or create filesystem:

mke2fs	pubsw /dev/hdb1

And change the label

mke2fs -L	pubsw /dev/hdb1


Summarizing the commands used above in a single list:

# find filesystem / list mount points
# print info about all block device filesystems 
lsblk --fs
#look for UUID
mount -t auto -U XXXXXXXXXXX


  1. block special file: A block special file is normally distinguished from a character special file by providing access to the device in a manner such that the hardware characteristics of the device are not visible.

  2. A Universally Unique IDentifier (UUID) URN Namespace

Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus