How to eject CD/DVD tray from command line on Linux

If you're new to the Linux command line, you may have heard that it's one of the most powerful features of the operating system, and that’s absolutely true. Nearly everything you can do through the graphical user interface (GUI) can also be accomplished via the command line.

In fact, you can even eject or close your system's CD/DVD drive using command line commands. If this has caught your attention and you’d like to learn more, keep reading as this article will guide you through the process.

Please note that all commands and instructions in this guide have been tested on Ubuntu 24.04. However, these commands should work on any other modern Linux distribution as well.

Eject CD/DVD drive from the command line in Linux with "eject" command

Yes, you can do this with the "eject" command. The command's man page describes it as follows:

"Eject allows you to eject removable media (typically a CD-ROM, a floppy disk, a tape, or a JAZ or ZIP disk) under software control. The command can also control some multi-disc CD-ROM changers, the auto-eject feature supported by some devices, and the closing of the disc tray of some CD-ROM drives."

To operate, simply run the following command:

eject

In case this command doesn't work, you can try the following:

eject /dev/cdrom
eject /dev/cdrw
eject /dev/dvd
eject /dev/dvdrom
eject /dev/dvdrw

And if none of these work, you'll have to look for the correct device in the list generated by the following command:

ls -l /dev | grep '\->'

To close the drive, use the following command:

eject -t

Please note that not all devices support the aforementioned close command. For more information on eject, head to its man page.