Mounting USB Key Fobs and Drives via Command Line in Linux

Accessing data on external storage devices, such as a Usb Key Fob or a portable hard drive, is a common task for Linux users. While graphical file managers often handle this seamlessly, understanding how to mount these devices via the command line offers greater control and is essential for system administrators and advanced users. This guide will walk you through using udisks and dbus to mount and unmount drives without needing root privileges.

For those who frequently work with removable media, knowing how to interact with these devices directly from the terminal can significantly streamline workflows. Whether you are dealing with a simple usb key fob to transfer files or managing larger external drives, the following commands provide a robust and efficient method. Before proceeding, it’s helpful to identify two key pieces of information about the drive you intend to mount:

  1. Device Path: This is the system’s identifier for your drive, typically something like /dev/sdb1.
  2. Filesystem Type: The format of the drive’s partition, such as ext4, vfat, or ntfs.

If you are unsure about these details, don’t worry, we’ll cover how to find them. Once you have this information, mounting your drive becomes straightforward with the gdbus command.

To mount a drive, execute the following command in your terminal:

gdbus call --system --dest org.freedesktop.UDisks --object-path /org/freedesktop/UDisks/devices/<device> --method org.freedesktop.UDisks.Device.FilesystemMount "<filesystem>" []

Replace <device> with the device identifier (e.g., sdb1) and <filesystem> with the filesystem type (e.g., vfat). Upon successful execution, this command will output the mount point, indicating where your drive’s contents can be accessed within the file system.

Unmounting a drive mounted in this manner is equally simple. Use this command:

gdbus call --system --dest org.freedesktop.UDisks --object-path /org/freedesktop/UDisks/devices/<device> --method org.freedesktop.UDisks.Device.FilesystemUnmount []

Again, replace <device> with the appropriate device identifier. It’s important to note that <device> in these commands refers to the end part of the device path. For instance, if your drive is /dev/sdb2, you would use sdb2 in place of <device>.

If you are unsure about the device path or filesystem type of your usb key fob or drive, you can easily retrieve this information using the gdbus introspect command. This command queries the udisks service and lists properties of all detected devices:

gdbus introspect --system --dest org.freedesktop.UDisks --object-path /org/freedesktop/UDisks/devices --recurse --only-properties | grep -E "(readonly .+ (IdLabel|IdType|Device(IsMounted|IsDrive|File) ).*|}|.*{)"

Executing this command will generate a detailed output, similar to the example below, listing various devices and their properties:

node /org/freedesktop/UDisks/devices {
  node /org/freedesktop/UDisks/devices/sda {
    interface org.freedesktop.UDisks.Device {
      readonly s DeviceFile = '/dev/sda';
      readonly b DeviceIsDrive = true;
      readonly b DeviceIsMounted = false;
      readonly s IdLabel = '';
      readonly s IdType = '';
      readonly s IdUsage = '';
    };
  };
  node /org/freedesktop/UDisks/devices/sda1 {
    interface org.freedesktop.UDisks.Device {
      readonly s DeviceFile = '/dev/sda1';
      readonly b DeviceIsDrive = false;
      readonly b DeviceIsMounted = false;
      readonly s IdLabel = 'SYSTEM';
      readonly s IdType = 'ntfs';
      readonly s IdUsage = 'filesystem';
    };
  };
  node /org/freedesktop/UDisks/devices/sda2 {
    interface org.freedesktop.UDisks.Device {
      readonly s DeviceFile = '/dev/sda2';
      readonly b DeviceIsDrive = false;
      readonly b DeviceIsMounted = true;
      readonly s IdLabel = 'Windows7';
      readonly s IdType = 'ntfs';
      readonly s IdUsage = 'filesystem';
    };
  };
  node /org/freedesktop/UDisks/devices/sda3 {
    interface org.freedesktop.UDisks.Device {
      readonly s DeviceFile = '/dev/sda3';
      readonly b DeviceIsDrive = false;
      readonly b DeviceIsMounted = false;
      readonly s IdLabel = 'Recovery';
      readonly s IdType = 'ntfs';
      readonly s IdUsage = 'filesystem';
    };
  };
  node /org/freedesktop/UDisks/devices/sda4 {
    interface org.freedesktop.UDisks.Device {
      readonly s DeviceFile = '/dev/sda4';
      readonly b DeviceIsDrive = false;
      readonly b DeviceIsMounted = false;
      readonly s IdLabel = '';
      readonly s IdType = '';
      readonly s IdUsage = '';
    };
  };
  node /org/freedesktop/UDisks/devices/sda5 {
    interface org.freedesktop.UDisks.Device {
      readonly s DeviceFile = '/dev/sda5';
      readonly b DeviceIsDrive = false;
      readonly b DeviceIsMounted = true;
      readonly s IdLabel = '';
      readonly s IdType = 'ext4';
      readonly s IdUsage = 'filesystem';
    };
  };
  node /org/freedesktop/UDisks/devices/sda6 {
    interface org.freedesktop.UDisks.Device {
      readonly s DeviceFile = '/dev/sda6';
      readonly b DeviceIsDrive = false;
      readonly b DeviceIsMounted = false;
      readonly s IdLabel = '';
      readonly s IdType = 'swap';
      readonly s IdUsage = 'other';
    };
  };
  node /org/freedesktop/UDisks/devices/sda7 {
    interface org.freedesktop.UDisks.Device {
      readonly s DeviceFile = '/dev/sda7';
      readonly b DeviceIsDrive = false;
      readonly b DeviceIsMounted = true;
      readonly s IdLabel = '';
      readonly s IdType = 'ext4';
      readonly s IdUsage = 'filesystem';
    };
  };
  node /org/freedesktop/UDisks/devices/sdb {
    interface org.freedesktop.UDisks.Device {
      readonly s DeviceFile = '/dev/sdb';
      readonly b DeviceIsDrive = true;
      readonly b DeviceIsMounted = false;
      readonly s IdLabel = '';
      readonly s IdType = '';
      readonly s IdUsage = '';
    };
  };
  node /org/freedesktop/UDisks/devices/sdb1 {
    interface org.freedesktop.UDisks.Device {
      readonly s DeviceFile = '/dev/sdb1';
      readonly b DeviceIsDrive = false;
      readonly b DeviceIsMounted = false;
      readonly s IdLabel = 'USB DRIVE';
      readonly s IdType = 'vfat';
      readonly s IdUsage = 'filesystem';
    };
  };
  node /org/freedesktop/UDisks/devices/sr0 {
    interface org.freedesktop.UDisks.Device {
      readonly s DeviceFile = '/dev/sr0';
      readonly b DeviceIsDrive = false;
      readonly b DeviceIsMounted = false;
      readonly s IdLabel = '';
      readonly s IdType = '';
      readonly s IdUsage = '';
    };
  };
}

Focus on the entries where IdUsage = 'filesystem'. These represent partitions that can be mounted. For example, to mount the device labeled ‘USB DRIVE’, which the output shows as /dev/sdb1 with filesystem type vfat, you would use:

gdbus call --system --dest org.freedesktop.UDisks --object-path /org/freedesktop/UDisks/devices/sdb1 --method org.freedesktop.UDisks.Device.FilesystemMount "vfat" []

These commands leverage the dbus messaging system, the same mechanism used by graphical file managers like Nautilus for automatic mounting. By sending messages to udisks objects through dbus, we instruct the system to mount or unmount devices. The success of these operations depends on the permissions configured in PolicyKit, ensuring user-level control over device access.

Beyond mounting drives, the dbus system allows for controlling numerous aspects of your Linux environment, from system shutdown to volume adjustments. Understanding these command-line tools empowers users to interact deeply with their systems and automate tasks that would otherwise require graphical interfaces. Using these techniques, managing your usb key fob and other removable storage from the command line becomes both efficient and insightful.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *