Table of Contents#
- Introduction
- Basics of the
ejectCommand - Syntax and Options
- Common Usage Examples
- Advanced Options and Use Cases
- Best Practices
- Troubleshooting Common Issues
- Conclusion
- References
Basics of the eject Command#
The eject command serves two core purposes:
- Safely remove media: For USB drives, SD cards, or optical discs, it ensures all data is written to the device before disconnection.
- 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.
ejectflushes 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)#
| Option | Short Form | Description | Example Usage |
|---|---|---|---|
--unmount | -u | Unmount the device before ejecting (safe default). | eject -u /dev/sdb1 |
--no-unmount | -n | Skip unmounting (use only if the device is already unmounted). | eject -n /dev/sr0 |
--trayclose | -t | Close the optical drive tray. | eject -t /dev/sr0 |
--traytoggle | -T | Toggle the tray (open if closed, close if open). | eject -T (toggles default drive) |
--poweroff | -p | Power off the device (if supported, e.g., USB hubs). | eject -p /dev/sdb1 |
--force | -f | Force 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):
ejectThis 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/sdb1Or use its mount point (e.g., /media/MyUSB):
eject /media/MyUSB3. Unmount Then Eject (Safe Practice)#
To ensure all data is written, unmount the device before ejecting:
eject -u /dev/sdb1This 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/sdb1Advanced 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 48x2. 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/sdb12. 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:
ejectoften requires root privileges. Prefer usingsudo:sudo eject /dev/sdb1 - Alternatively, add your user to the
cdromgroup: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
lsoforfuserto 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
ejectwithsudo:sudo eject /dev/sdb1 - Solution 2: Add your user to the
cdromgroup (as shown in “Best Practices”).
3. Tray Won’t Open/Close#
- Toggle the tray: Use
eject -Tto 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
ejectman 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!