Commands.page Logo

How to Safely Unmount a File System in Ubuntu Terminal

This article provides a step-by-step guide on how to safely unmount a file system in Ubuntu using the command line. It covers identifying mounted devices, executing the unmount command, and resolving common errors where a device appears busy to ensure no data is lost during the process.

Identify the Mount Point

Before unmounting, you must know the device name or the mount point. Open your terminal and list all mounted file systems using the following command:

df -h

Alternatively, you can use lsblk to see a tree view of your block devices:

lsblk

Locate the specific drive or partition you intend to unmount in the output list. Note the path under the “Mounted on” column, such as /mnt/usb or /media/user/drive.

Execute the Unmount Command

To unmount the file system, use the umount command followed by the mount point or the device name. You typically need superuser privileges for this operation.

sudo umount /mnt/usb

Replace /mnt/usb with the actual path or device identifier you found in the previous step. Once executed successfully, the command returns no output, and the file system is detached.

Handle Target is Busy Errors

If you receive an error stating “target is busy,” it means a process is currently accessing the drive. You cannot safely unmount until access stops. Find the processes using the mount point with:

sudo lsof +f -- /mnt/usb

Close any open files or terminal sessions accessing that directory. If you cannot identify the process, you can force a lazy unmount, which detaches the file system immediately and cleans up references once they are no longer busy:

sudo umount -l /mnt/usb

Use the lazy unmount option with caution as it may interrupt active data transfers.

Verify the Unmount

Confirm that the drive has been successfully unmounted by running the list command again.

df -h

The device should no longer appear in the list of mounted file systems. It is now safe to physically remove the hardware or repurpose the mount point.