Safest way to remove USB drive on Linux
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
Related videos on Youtube
Pkkm
Programmer (Python, C, Lisp), Emacs and GNU/Linux user.
Updated on September 18, 2022Comments
-
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 usesync && umount $mountpoint && sync
, or perhapsumount $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 overumount $mountpoint
), but not the whole question (whetherudisks --detach $device
or some other command provides improved safety), so it's not an exact duplicate.-
xhienne about 7 yearsSHort 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 about 7 yearsUnmounting 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 about 7 yearsDoes executing this command after unmounting improve protection against data loss/corruption or only reduce power usage?
-
user2948306 about 7 yearsYou'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.