Safest way to remove USB drive on Linux

7,322

Quote from Eject USB drives / eject command:

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:

udisks --detach /dev/sdb

This usually causes the drive to spin down gracefully.

EDIT: Also, as g.rocket points out, this command may be equivalent:

udisksctl power-off -b /dev/sdb
Share:
7,322

Related videos on Youtube

Pkkm
Author by

Pkkm

Programmer (Python, C, Lisp), Emacs and GNU/Linux user.

Updated on September 18, 2022

Comments

  • Pkkm
    Pkkm over 1 year

    Is umount $mountpoint always enough to safely remove a single-partition USB drive on a modern GNU/Linux system? Or should I use sync && umount $mountpoint && sync, or perhaps umount $mountpoint && udisks --detach $device? I'd like to use the safest way possible, but on the other hand I don't want to introduce superfluous dependencies or delays into my program.

    EDIT: Does umount calls sync to complete any pending writes indeed answers a part of this question (whether sync && umount $mountpoint && sync provides any benefit over umount $mountpoint), but not the whole question (whether udisks --detach $device or some other command provides improved safety), so it's not an exact duplicate.

    • xhienne
      xhienne about 7 years
      SHort answer: umount is safe and doesn't need anything else, but of course, you have to check its return value to see if the command was effective.
    • Tim Kennedy
      Tim Kennedy about 7 years
      Unmounting a USB drive only partially makes it safe to remove. It stops I/O to the filesystem, but it doesn't power down the device, making it possible to remount the device. It also doesn't flush any I/O cache to the device, so the data you think may be on the device, may not be. In a gui, you'll often see an option to "Safely Remove" a USB device. That option sync's first, then unmounts, then powers down the device.
  • Pkkm
    Pkkm about 7 years
    Does executing this command after unmounting improve protection against data loss/corruption or only reduce power usage?
  • user2948306
    user2948306 about 7 years
    You'd hope it never makes a difference, but I don't know. It does help in the sense that a spinning drive is more vulnerable (docs on mine say "don't move it"), & with a short thick cable it is very hard to unplug without also moving the drive around a bit.