Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know that the eject command can be used to eject almost any hardware component attached, but can it be used to eject USB drives?

Is it possible to eject USB drives and external HDD's with the eject command?

share|improve this question
up vote 9 down vote accepted

No. Nor do they need to be; eject is used for opening optical drives, where one cannot pull the media from directly.

Unmounting is sufficient for USB/eSATA/etc. storage devices.

share|improve this answer
Hmm, I saw eject hdd as in /dev/sda somewhere. I guess the reference was wrong then. – Joe Barr Apr 2 '12 at 0:49
You can do it, but it usually doesn't do very much if anything at all. – Ignacio Vazquez-Abrams Apr 2 '12 at 0:51
This assumes that the device is mounted in the first place, and that carries along another big set of assumptions (it's got a filesystem that you can read, for one). Imagine that you're erasing a bunch of external drives - they probably never get mounted. eject is the right thing to use. – James Moore Oct 5 '12 at 15:03
If I'm ever unsure, I sync before yanking it out – EkriirkE May 14 '14 at 6:51

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.
share|improve this answer
I also had to use sudo to get this command to finish (all it would do was unmount the drive before saying it was unable to open the file). Other than that, helpful answer. – GDP2 Jan 31 at 5:16
Thanks, @GDP2, I have included that in my answer. – danorton Feb 3 at 1:47

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.

Precisely, the current implementation:

  • 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.

share|improve this answer
On my ubuntu onyl udisksctl which does not have --detach otion is available. What to do in this case? thank you. – Wakan Tanka Jan 11 at 14:37
@WakanTanka you install udisks. – Totor Jan 20 at 0:37
How can I install udisks? It is not on my system. I found the package udisks2, but that did not make udisks or udisks2 available. – Martin Thoma May 14 at 8:07

"Ejecting" has no meaning for hardware without a tray or other loading mechanism (I assume it works with tape drives too).

However, testing with an external USB flash drive tells that eject works much like umount - with the side effect of making the device nodes disappear, e.g.

% ls  /dev/sdc*
/dev/sdc  /dev/sdc1
% sudo eject /dev/sdc
% ls  /dev/sdc*

Note that /dev/sdc1 has disappeared.

share|improve this answer
Never tried it with tapes (mt rewoffl is more convenient because it also rewinds), but it does work with motorised floppy drives like those found on old Macs and Sun workstations. – Alexios Apr 2 '12 at 4:23
@Alexios interesting. – Renan Apr 2 '12 at 16:14

If you carefully read eject(1) man page you can see that there are 4 methods of ejecting:

   -r   This  option specifies that the drive should be ejected using a CDROM
        eject command.

   -s   This option specifies that the drive should  be  ejected  using  SCSI

   -f   This option specifies that the drive should be ejected using a remov‐
        able floppy disk eject command.

   -q   This option specifies that the drive should be ejected using  a  tape
        drive offline command.

When you call eject on HDD/SCSI it issue ioctl(fd, SG_IO, (void *)&io_hdr); command (copy from eject.c sources).

This is equivalent as you safely remove device in MS Windows or MaxOSX.

For some devices this have special mean. For example Kindle 3 after eject command has being moved to charging mode and allow browsing on device, while before screen was locked.

Another utilities do same thing, like this

scsi-spin –force –down /dev/sda
share|improve this answer

In osx command line you should use diskutil where LABEL is label of your usb drive.

diskutil eject /Volumes/<LABEL>
share|improve this answer

udisks --detach /dev/sdX where (X) is the last letter of your usb device. It works fine on any linux system.

share|improve this answer
Your answer is okay, although maybe it is a little bit short. I would suggest to elaborate more: what this command does, how, why is this what you suggest, etc. – peterh Oct 4 '15 at 3:25

OK i will try to explain this better:

udisks command completely remove and power off any usb device mounted or attached in the system unmount command just unmount the partition ie: dev/sdb1 or whatever but the usb is still present in the system.

So is not the same unmount, eject and detach

udisks = power off the usb

umount = just unmount the partition not the whole pendrive

eject = the same or very close to umount command

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.