Linux eject Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux eject command to eject removable media devices, such as CD/DVD drives and USB drives. The lab covers the purpose of the eject command, how to eject a removable media device, and how to eject a CD/DVD drive. The eject command is a useful tool for safely removing storage devices from your system, and the lab provides practical examples to help you understand its usage.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") subgraph Lab Skills linux/cd -.-> lab-422660{{"`Linux eject Command with Practical Examples`"}} linux/sudo -.-> lab-422660{{"`Linux eject Command with Practical Examples`"}} end

Understand the Purpose of the eject Command

In this step, you will learn about the purpose and usage of the eject command in Linux. The eject command is used to eject removable media devices, such as CD/DVD drives, USB drives, and other types of removable storage.

The eject command can be used to:

  • Eject a removable media device, such as a CD/DVD drive or a USB drive.
  • Eject a CD or DVD from the optical drive.
  • Unlock a device that has been locked by the system.

To use the eject command, simply run the following command in the terminal:

sudo eject [device]

Replace [device] with the path to the device you want to eject, such as /dev/cdrom for a CD/DVD drive or /dev/sdb1 for a USB drive.

Example:

sudo eject /dev/cdrom

Example output:

Ejecting /dev/cdrom

The eject command can also be used without any arguments to eject the default removable media device, which is usually the CD/DVD drive.

sudo eject

Example output:

Ejecting /dev/cdrom

Eject a Removable Media Device

In this step, you will learn how to eject a removable media device, such as a USB drive, using the eject command.

First, let's insert a USB drive into your Docker container. You can use the lsblk command to list all the block devices connected to your system, including the USB drive.

sudo lsblk

Example output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   100G  0 disk
├─sda1   8:1    0    99G  0 part /
├─sda2   8:2    0     1K  0 part
└─sda5   8:5    0     1G  0 part [SWAP]
sdb      8:16   1   7.5G  0 disk
└─sdb1   8:17   1   7.5G  0 part /media/labex/MYUSB

In the example output, you can see that the USB drive is assigned the device name sdb1.

To eject the USB drive, use the eject command with the device path:

sudo eject /dev/sdb1

Example output:

Ejecting /dev/sdb1

After running the eject command, the USB drive should be safely ejected and can be removed from the container.

Eject a CD/DVD Drive

In this step, you will learn how to eject a CD/DVD drive using the eject command.

First, let's insert a CD/DVD into your Docker container's CD/DVD drive. You can use the lsblk command to list all the block devices connected to your system, including the CD/DVD drive.

sudo lsblk

Example output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   100G  0 disk
├─sda1   8:1    0    99G  0 part /
├─sda2   8:2    0     1K  0 part
└─sda5   8:5    0     1G  0 part [SWAP]
sr0     11:0    1  1024M  0 rom  /media/labex/DVDDRIVE

In the example output, you can see that the CD/DVD drive is assigned the device name sr0.

To eject the CD/DVD, use the eject command with the device path:

sudo eject /dev/sr0

Example output:

Ejecting /dev/sr0

After running the eject command, the CD/DVD should be safely ejected and can be removed from the container.

Summary

In this lab, you learned about the purpose and usage of the eject command in Linux. The eject command can be used to eject removable media devices, such as CD/DVD drives and USB drives, as well as unlock devices that have been locked by the system. You also learned how to use the eject command to safely eject a USB drive from your system.

The lab covered the following key points:

  1. Understanding the purpose of the eject command and how to use it to eject removable media devices.
  2. Ejecting a CD/DVD drive using the eject command.
  3. Ejecting a USB drive using the eject command and the lsblk command to identify the device.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like