Eject USB drives / eject command

Yes. For example:

sudo eject /dev/sda

Other answers here that indicate that you require mechanical ejection hardware are incorrect.

Unmounting is not the same thing as ejecting.

  1. If you unmount a volume, you can immediately mount it back, because the underlying device is still available. In some situations, this could present a security risk. By ejecting the device, only a reset of the USB subsystem (e.g. a reboot) will reload the device.
  2. By ejecting the device, you effectively disable any further access to the device. Only a reset of the USB subsystem (e.g. a reboot) will reload the device. Otherwise, you must physically disconnect the USB device and reconnect it in order to access it again.
  3. Before ejecting, this command will unmount all volumes on the device that were mounted.
  4. If volumes are in use, this command will fail as with unmount, except that some volumes might be unmounted and some volumes might remain mounted.

eject will work, but will not really "finish the job" regarding USB rotating drives.

The best way to unplug a USB external hard-drive, after proper unmounting, is:

udisksctl power-off -b /dev/sdb

or

udisks --detach /dev/sdb

This usually causes the drive to spin down gracefully.

Note: udisksctl might be a more "mainstream" tool, compared to udisks (the former is already installed on my Debian, the latter isn't).

Some details

udisksctl

The documentation states (about the power-off option):

Arranges for the drive to be safely removed and powered off. On the OS side this includes ensuring that no process is using the drive, then requesting that in-flight buffers and caches are committed to stable storage. The exact steps for powering off the drive depends on the drive itself and the interconnect used. For drives connected through USB, the effect is that the USB device will be deconfigured followed by disabling the upstream hub port it is connected to.

Note that as some physical devices contain multiple drives (for example 4-in-1 flash card reader USB devices) powering off one drive may affect other drives. As such there are not a lot of guarantees associated with performing this action. Usually the effect is that the drive disappears as if it was unplugged.

udisks

Precisely, the current implementation (as of 2014):

  • sends SCSI sync-cache command,
  • sends SCSI stop command,
  • unbinds the usb-storage kernel driver,
  • suspends the USB device (power),
  • logically disables/removes it from its USB port.

This process is close to the manual procedure that is suggested here. Initial answer was on askubuntu.


Manual steps for unmounting disk /dev/sdb (Requires sudo):

echo 'offline' > /sys/block/sdb/device/state
echo '1' > /sys/block/sdb/device/delete

This will completely power-off the device and detach it from the system. It won't be detected again till it is disconnected and re-attached.