105

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?

1
  • 5
    Related: the opposite of eject /dev/sdX is sg_start -s /dev/sdX (from sg3_utils package), both use SCSI commands to send to the device.
    – Yeti
    Commented Feb 9, 2018 at 12:09

11 Answers 11

124

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.
5
  • 2
    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
    Commented Jan 31, 2016 at 5:16
  • Under-appreciated answer in this thread. Just look at a FAT32 mounted thumb drive, and notice how unmounting and ejecting have a world of difference just in the file explorer (ie: nautilus) alone. A simple Sansa clip would also help demonstrate this.
    – Cloud
    Commented Sep 21, 2016 at 5:17
  • The device is still present when executing lsusb. So you don't have to reset the whole USB-Subsystem. Just the device is sufficient. You can use usbreset to do this github.com/jkulesza/usbreset github.com/CWempe/usbreset The usbreset.c files are identically in both projects,
    – Hannes
    Commented Jun 15, 2019 at 7:17
  • How do I know that "sda" is the right one? Can I map sda, sdb, etc to the real device names?
    – Jürgen K.
    Commented Oct 31, 2019 at 13:29
  • you can use blkid as in sudo blkid /dev/sda1 to get the label of your usb drive, if it has one. Otherwise you can always revert to the dumb way, which is to check the size of drives listed with sudo lsblk. Of course this assumes you know the drive's size, and it differs significantly from other devices... Commented May 17, 2020 at 1:19
80

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

So first, you eject /dev/sdb (or umount everything).

And then, after proper unmounting, the best way to unplug a USB external hard-drive 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 & has been criticised for unnecessary spin up/down).

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 (deprecated?)

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.

4
  • udisksctl power-off -b /dev/sdb will deconfigure the usb device even if multiple disks are accessible by it (i.e. usb external raid enclosure). I am not sure that the other drives are safely stopped.
    – Timesquare
    Commented Aug 28, 2021 at 18:25
  • @Timesquare Yes, that may be a limitation of the tool, as udiskctl's documention (which I quote) points out (using the 4-in-1 USB flash card reader as an example).
    – Totor
    Commented Sep 1, 2021 at 23:22
  • @Totor, thank you, I was not very attentive, had lots of info to read, however, I think many like myself want simple complete solution. I've already wrote my answer for both commands run via shell (bash) function unix.stackexchange.com/a/679472/446998. Commented Nov 29, 2021 at 4:14
  • I find that this works better than using eject. I have several USB drives who’s block devices don’t disappear when I run eject but do disappear when I run udisksctl power-off. Commented Jan 12, 2023 at 0:07
31

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.

4
  • 3
    FWIW this is the only thing that works for me in a Xen dom0 with an external USB cradle - the eject and udisks commands gave errors. Commented Jan 13, 2018 at 10:19
  • 1
    I was searching for this, i'm in dracut emergency mode and I don't have access to any of the other functions. Thanks!
    – Nassiel
    Commented Oct 25, 2018 at 12:03
  • hdparm -Y is also handy for SATA/IDE drives.
    – Walf
    Commented Feb 1, 2019 at 6:39
  • 1
    This worked for me on Debian 11; other things suggested here did not work Commented Aug 20, 2023 at 23:12
16

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.

5
  • Hmm, I saw eject hdd as in /dev/sda somewhere. I guess the reference was wrong then.
    – Joe Barr
    Commented Apr 2, 2012 at 0:49
  • You can do it, but it usually doesn't do very much if anything at all. Commented Apr 2, 2012 at 0:51
  • 2
    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. Commented Oct 5, 2012 at 15:03
  • 1
    If I'm ever unsure, I sync before yanking it out
    – EkriirkE
    Commented May 14, 2014 at 6:51
  • 3
    Big, fat WARNING: This is NOT completely true. If you do not eject it underlying USB Device Driver may still (try to) write AFTER umount has returned. This can be clearly seen if drive has a status LED. Yanking the USB stick right after umount terminates is RECIPE FOR DISASTER.
    – ZioByte
    Commented Sep 11, 2019 at 9:49
7

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

   -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
5

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

diskutil eject /Volumes/<LABEL>
4

"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*
/dev/sdc

Note that /dev/sdc1 has disappeared.

2
  • 1
    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
    Commented Apr 2, 2012 at 4:23
  • @Alexios interesting.
    – Renan
    Commented Apr 2, 2012 at 16:14
2

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

1
  • 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
    Commented Oct 4, 2015 at 3:25
1

Based on two top answers. Just using udisksctl results in error if still mounted and eject alone does not power-off, only unmounts USB. Therefore I wrote simple one-liner to create a function in bash:

 e_ject() { 2>/dev/null eject $1;udisksctl power-off -b $1; }; export -f e_ject

To use:

e_ject /dev/sdX

Even more complex and user friendly:

'e_ject() { dev=$(mount| grep $1 | awk --field-separator " " '{ FS = " " ; print $1 ; exit }');2>/dev/null eject $dev;udisksctl power-off -b $dev; }; export -f e_ject'

To use:

e_ject name

name is searched (grepped) in mount output of all mounts, expected to be present on one line only, but seems to work fine even if on multiple mounts e.g. for e_ject sdb with multiple partitions USB stick. On my system both eject and udisksctl work with partition (/dev/sdbx) parameter.

P.S.

2>/dev/null is added due to eject outputing an error each time, but result seemed correct.

Not sure a function need to be exported each time of login, but count not find an asnwer to that in man bash and do not want to google now. Therefore to be on the safe side below (adding to user profile) is expected to be Ok (multiple re-export did not output any errors):

echo 'e_ject() { 2>/dev/null eject $1;udisksctl power-off -b $1; }; export -f e_ject' >> /home/$(id -u -n)/.profile
0

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

0

In gnome,

$ gio mount -e /media/dzmanto/MYUSB

ejects a drive MYUSB. No root privileges are required.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .