Detecting and mounting USB external drives with CLI

Hello,

I have some USB external drives. When running the nautilus (GUI filter app), the disks are listed on the left side of the menu. And when clicking the disk name, it is mounted.

However, how to list up and mount USB external disks as commands as CLI, without using the nautilus GUI?

I found this thread Listing and mounting external USB drives from CLI . The lsblk and the udisksctl are clues. But I am not still sure what to do. Thanks.

My environment is

  • OS: Fedora 36
  • Window manager: i3 and Sway
1 Like

One possible way is to use ls /dev before and after plugging in the usb device. The difference will be the new device connected. (dmesg should also show the new connection.)

I think that udev automatically mounts it and with fedora it seems to mount at /run/media/username/device. You can see if it is mounted with the mount command.

You could also make an entry in /etc/fstab for each device (using UUID) and specify where it should be mounted then udev will use that location instead.

The man mount command gives a lot of info about how to mount devices, but you still need to know the name of the device to mount. An example mount command that would work is sudo mount /dev/device /mnt which would mount the named device at /mnt for access. If the ls /dev command showed me that the new device was /dev/sda and it had a partition /dev/sda1 then I would use the partition name (sda1) in place of ‘device’ in the mount command above.

3 Likes

You can use many tools.
Exmaples

To mount/umout

udisksctl unmount -b /dev/sdb1

udisksctl mount -b /dev/sdb1

To lock unlock encrypted volume

udisksctl lock -b /dev/mapper/fedora_fedora-home

udisksctl unlock -b /dev/mapper/fedora_fedora-home

To list devices and volumes

PAGER=cat udisksctl dump |grep “PreferredDevice:”

or

sudo blkid

You can also check gio

gio mount

2 Likes

Guys, thanks for your info!

I was able to list up and mount the USB external drive with the commands.

I was able to list up USB external devices including the not-mounted ones. I am masking the UUID as “XXXX-XXXX”. So far the lsblk + options are the best to print what I want to see. However, I am still looking for how to print “USB volume label” that is printed in the left menu of the “nautilus” filter. The “LABEL” is not the USB drive’s name. Do you know how to print the label name from the lsblk?

$ rpm -qf /bin/lsblk
util-linux-2.38-1.fc36.x86_64

$ lsblk -l -p -o NAME,TYPE,FSTYPE,UUID,SIZE,LABEL
...
/dev/sda                                              disk                                                   232.9G 
/dev/sda1                                             part  vfat        XXXX-XXXX                            232.9G 
...

I mounted the USB external drive.

With the mount command, it’s a bit harder.

$ cd /run/media/jaruga/
$ sudo mkdir XXXX-XXXX
$ sudo mount /dev/sda1 /run/media/jaruga/XXXX-XXXX
$ sudo umount /dev/sda1
$ sudo rm -rf XXXX-XXXX

With the udisksctl command, it’s convenient!

$ udisksctl mount -b /dev/sda1
Mounted /dev/sda1 at /run/media/jaruga/XXXX-XXXX
2 Likes

I see that the gio mount -l command can list the volume name, “250 GB Volume” on the result!

$ gio mount -l
Drive(0): WD_BLACK  SN750 2TB
  Type: GProxyDrive (GProxyVolumeMonitorUDisks2)
Drive(1): USB DISK 3.2
  Type: GProxyDrive (GProxyVolumeMonitorUDisks2)
  Volume(0): 250 GB Volume
    Type: GProxyVolume (GProxyVolumeMonitorUDisks2)
2 Likes