thelinuxvault blog

Eject Command in Linux: A Comprehensive Guide with Examples

The eject command in Linux is a powerful utility for safely removing or ejecting removable media (e.g., CDs, DVDs, USB drives) and controlling optical drive trays. Unlike physically yanking out a device, eject ensures all pending read/write operations complete, preventing data corruption or loss. This guide explores its syntax, use cases, best practices, and troubleshooting.

2026-05

Table of Contents#

Basics of the eject Command#

The eject command serves two core purposes:

  1. Safely remove media: For USB drives, SD cards, or optical discs, it ensures all data is written to the device before disconnection.
  2. Control optical drives: Opens/closes the tray of CD/DVD/Blu-ray drives or powers down compatible devices.

Why Use eject?#

  • Data Integrity: Abruptly removing a device (e.g., pulling out a USB) can corrupt files or damage the filesystem. eject flushes all pending data.
  • Hardware Safety: For optical drives, ejecting via software prevents mechanical damage (e.g., tray jams from forced manual opening).

Syntax and Options#

The basic syntax of eject is:

eject [options] [device|mount-point]

Key Components:#

  • options: Modify behavior (e.g., unmount, toggle tray).
  • device|mount-point: Target device (e.g., /dev/sr0, /dev/sdb1) or its mount point (e.g., /media/USB). Omit to target the default optical drive (/dev/sr0).

Common Options (With Examples)#

OptionShort FormDescriptionExample Usage
--unmount-uUnmount the device before ejecting (safe default).eject -u /dev/sdb1
--no-unmount-nSkip unmounting (use only if the device is already unmounted).eject -n /dev/sr0
--trayclose-tClose the optical drive tray.eject -t /dev/sr0
--traytoggle-TToggle the tray (open if closed, close if open).eject -T (toggles default drive)
--poweroff-pPower off the device (if supported, e.g., USB hubs).eject -p /dev/sdb1
--force-fForce eject (risky—use for stuck devices; may damage data/hardware).eject -f /dev/sr0

Common Usage Examples#

Let’s explore practical scenarios with eject.

1. Eject the Default Optical Drive (CD/DVD)#

By default, eject targets the primary optical drive (/dev/sr0):

eject

This unmounts (if needed) and opens the tray (for optical drives) or disconnects the device.

2. Eject a Specific USB Drive#

First, identify the device (e.g., via lsblk or dmesg). Suppose your USB is /dev/sdb1:

eject /dev/sdb1

Or use its mount point (e.g., /media/MyUSB):

eject /media/MyUSB

3. Unmount Then Eject (Safe Practice)#

To ensure all data is written, unmount the device before ejecting:

eject -u /dev/sdb1

This is equivalent to umount /dev/sdb1 && eject /dev/sdb1.

4. Open/Close the Optical Drive Tray#

  • Open the tray:
    eject  # Default for optical drives
  • Toggle the tray (open/close):
    eject -T
  • Close the tray:
    eject -t

5. Eject Without Unmounting (Advanced)#

If the device is already unmounted (e.g., via umount), skip unmounting with -n:

eject -n /dev/sdb1

Advanced Options and Use Cases#

Beyond basic usage, eject offers niche features.

1. Set CD/DVD Speed (For Optical Drives)#

Adjust the drive’s speed (e.g., for burning or playback) with --cdspeed (or -x):

eject -x 48 /dev/sr0  # Set CD speed to 48x

2. Force Eject (Use With Caution)#

For a stuck device (e.g., tray won’t open), force eject with -f:

eject -f /dev/sr0

⚠️ Warning: Forcing eject can corrupt data or damage hardware. Use only if necessary.

3. Network Shares (Not Supported)#

The eject command works only with local block devices and mount points. It does not support network paths (NFS/Samba). Attempting to eject a network share (e.g., eject //server/share) will fail. Use umount to disconnect network shares instead.

Best Practices#

Follow these guidelines to avoid data loss or hardware issues:

1. Always Unmount First#

Use eject -u (or manual umount) to ensure all data is written. For example:

# Safe: Unmount then eject
eject -u /dev/sdb1
 
# Alternative: Manual unmount + eject
umount /dev/sdb1
eject /dev/sdb1

2. Verify the Device Path#

Before ejecting, confirm the device with lsblk, df -h, or dmesg:

lsblk  # Lists all block devices (USB, optical, hard drives)

3. Handle Permissions#

  • Root Access: eject often requires root privileges. Prefer using sudo:
    sudo eject /dev/sdb1  
  • Alternatively, add your user to the cdrom group:
    sudo usermod -aG cdrom $USER  
    newgrp cdrom  # Apply changes without logging out  

4. Wait for Activity Lights#

For optical drives or USBs, wait for the activity LED to stop blinking before ejecting (ensures all I/O is complete).

Troubleshooting Common Issues#

1. “Device is Busy” Error#

A process is using the device.

  • Identify the process: Use lsof or fuser to find which app is accessing the device:
    lsof +D /media/USB  # List processes using files in /media/USB
    fuser -m /dev/sdb1  # List PIDs accessing /dev/sdb1
  • Close the process: Quit the app (e.g., file manager, video player) and retry eject.

2. “Permission Denied” Error#

  • Solution 1: Run eject with sudo:
    sudo eject /dev/sdb1
  • Solution 2: Add your user to the cdrom group (as shown in “Best Practices”).

3. Tray Won’t Open/Close#

  • Toggle the tray: Use eject -T to force a toggle.
  • Force eject: For a stuck tray, use eject -f (risky, but may work for mechanical jams).

Conclusion#

The eject command is vital for safely removing media in Linux. By following best practices (unmount first, verify device paths, handle permissions), you can avoid data loss and hardware issues. Experiment with its options, but always prioritize safety—especially with --force or network devices.

References#

  • Official eject man page: man eject (run in terminal).
  • Linux Documentation Project: Removable Media HOWTO.
  • Arch Linux Wiki: Eject.

This guide covers eject in depth, from basic usage to advanced troubleshooting. Use it to master safe media removal in Linux!