CLI vs GUI – Differences in Mounting Hard Drives

automountcommand lineguihard drivemount

On Ubuntu 12.10, when I want to mount a hard drive, I can just click on the unmounted drive and everything works fine (by mounting to /media/username/partitionlabel).

Basically, I would like to do exactly that via command line (for a script I'm working on).

Since I do not want to automount on boot, fstab is out of the question (right?).

When I use mount on CLI, I need to specify a mountpoint (which needs to have a previously created mountpoint; also, I need to take care of permissions and whatnot) — what I don't understand is where does the GUI take all its infos from? The mountpoint seems to depend on the partition's label, but such a directory doesn't exist before mounting. Also, the GUI way doesn't seem to care too much about a user not being root.

Is there an "easy" way to mount via CLI, just like it does on the GUI by clicking on an unmounted drive?

Best Answer

Nautilus and other file managers that mount drives (i.e., the GUI) use the udisks command.

This provides a dynamic mount, where the mount point is created for the drive on-the-fly, and when the drive is unmounted, the mount point goes away.

See man 1 udisks for details on how to use this command (either manually, or called from a script/application). To learn more about how udisks works, see man 7 udisks.

To mount a device with udisks, you must specify a valid device name for the device. Specifying the device's volume name will not work. So, you can use udisks like this:

udisks --mount /dev/sdb1

When manually using udisks from the command-line, you might be best off to simply plug in a device, then run dmesg | tail shortly thereafter (see man dmesg and man tail) to discover the device name of the newly attached device.

This may not be the best way to write a script that mounts devices with udisks, however. I don't know exactly how you should do it, and it would be difficult to know since you haven't told us precisely what you want this script to do and when you want it to mount devices.

Related Question