How to Mount and Unmount Drives in Linux

Effective Linux storage management requires knowing how to Mount Drives in Linux. When you connect a hard disk, SSD, USB drive, or network storage, Linux doesn’t automatically make its files accessible. You must first mount the device, which links its filesystem to a directory in the system’s directory structure. Once mounted, users and applications can easily read from and write to the device.

When you no longer need a storage device, you should unmount it before removing it from the system. First, unmounting ensures that the system writes all pending data to the disk correctly. As a result, it prevents data corruption. However, if you remove the device without unmounting it, the system may not finish writing the files properly. Consequently, some files can become corrupted or even get lost. To manage storage devices safely, Linux provides several utilities. For example, tools such as mount, umount, lsblk, df, and blkid help administrators detect drives, mount filesystems, and unmount storage devices properly.

This tutorial covers the various ways to mount and unmount drives in Linux. Here you will learn to identify storage devices, mount partitions, automatically mount at boot time, and safely unmount drives.

Understanding Mounting in Linux

On Linux, there is only one hierarchical file system, a hierarchy starting from the root directory(/), where every file and directory exists. Linux has a slightly different approach; unlike some of the other operating systems, it does not assign distinct drive letters like C:, D:, etc. Instead, Linux mounts storage devices onto directories called mount points. For example, if we mount a USB drive to /mnt/usb, the system makes all files on that drive accessible through that directory. As a result, we can view and manage the drive’s contents directly from the mount point.

Mounting attaches the filesystem of a device at a specific point in the directory tree. Unmounting breaks that connection, safely disconnecting the device from the system.

Common mount locations include several key directories. For instance, /mnt serves as a temporary mount point for storage devices, while /media is the standard mount point for removable media. Additionally, /home stores partitions containing user data. Therefore, understanding mount points is essential when managing disks and filesystems in Linux, as it helps organize and access storage efficiently.

Identify Available Drives

It is necessary to identify the attached storage device on the system before mounting a drive. Linux exposes storage devices by names like /dev/sda,/dev/sdb, or /dev/nvme0n1. Use the next command to get a list of all available storage devices.

lsblk
Identify Available Drives

This command shows a tree listing of the disks and partitions attached to the system. The output shows disk name, partition names, filesystem type, mount points, and disk sizes. 

You can also use another command, such as fdisk with the l utility, to identify drives in a sequence:

sudo fdisk -l
Identify Available Drives with fdisk

This command shows detailed information about all disks and partitions recognized by the system. You can observe the partitions you want to mount by critically analyzing this output.

Check Filesystem Information

You may still want to confirm what filesystem the partition is using before you mount a drive. Some filesystems have different options when you mount. The following command allows you to see filesystem information.

sudo blkid
Check Filesystem Information

The blkid command lists the filesystem type, uuid and partition labels of all storage media.

How to Mount Drives in Linux

The most common Linux filesystem types include ext4, XFS, Btrfs, VFAT, and NTFS. Understanding these types is important because it enables the system to apply the appropriate mount options for each filesystem. As a result, the system can handle data more efficiently and maintain compatibility with different storage devices.

Method 1: Manually Mount Drives in Linux Using the Mount Command

The primary command used for attaching a filesystem to a directory is the mount command.

Step 1: Create a Mount Point

To mount a drive, you’ll first need to create a directory that will serve as the mount point. The command will create a mount directory.

sudo mkdir /mnt/drive1

This directory serves as the point where you can access the drive’s contents.

Create a Mount Point

Step 2: Mount Drives in Linux

After creating the mount point, you can use the mount command to mount the drive.

sudo mount /dev/sda2 /mnt/mydrive1
Mount Drives in Linux

Where /dev/sda2 is the partition you want to mount. It will mount the drive in the/mnt/drive1 directory. Once you run the command, you will be able to access the filesystem through its mount point.

Step 3: Verify Drive is Mounted

Once you mount the drive, it is important to verify that everything worked correctly. To do this, first navigate to the mount point, and then list the mounted filesystems. This way, you can confirm that the drive is properly connected and accessible:

df -h

Here’s how you can immediately see all mounted filesystems and their disk usage.

Verify Drive is Mounted

You can also verify the mount with the grep utility by specifying the name of the storage device:

mount | grep sda2
verify the mount with the grep

In the output, if the system lists the partition, it indicates that the drive has mounted successfully. Consequently, you can start accessing its files and using the storage without issues.

Method 2: Mount a Disk of Certain File System

In some cases, when you mount a drive, you need to specify what filesystem type it is. You use the -t option with the mount command to do this.

sudo mount -t ext4 /dev/sda2 /mnt/drive1

The ext4 argument here tells the system to mount this partition in such a way. You can set the correct type for other filesystems.

Mounting a FAT filesystem, you can execute the following command:

sudo mount -t vfat /dev/sdb1 /mnt/mydrive

Mounting an NTFS filesystem, you can run the command below:

sudo mount -t ntfs /dev/sdb1 /mnt/mydrive

Declaring the filesystem type avoids any compatibility issues.

Method 3: Automatically Mount Drives in Linux at Boot

To auto-mount a drive when the system boots, you will configure it in /etc/fstab. The fstab file controls how the system mounts filesystems. It determines which drives and partitions the system automatically makes available at startup.

Step 1: Find the Partition UUID

First, we need to find the partition UUID. Device names can change after a reboot, so avoid using them. Instead, use UUIDs, which you can fetch for a partition with the following command:

sudo blkid
Find the Partition UUID to Mount Drives in Linux

This will show the UUID of each partition.

Step 2: Edit the fstab File

We will open the fstab configuration file using any text editor.

sudo nano /etc/fstab

This allows you to mount the partition automatically on boot. Here, you specify the UUID and other relevant information:

<device>   <mount_point>   <filesystem_type>   <options>   <dump>   <pass>
edit fstab file to mount drives in linux

This entry instructs the system to mount the partition at /mnt/drive1 on boot time. Edit the file with your favorite editor and save the changes.

Step 3: Test the fstab Configuration

Now we need to test the configuration before we reboot the system. To test the fstab entries, you can use the command mount with the -a utility.

sudo mount -a
mount drives in linux

If there are no errors, the configuration is correct, and the drive will automatically mount whenever you start up your system next.

How to Unmount Drives in Linux

To safely disconnect a drive, one can run the umount command to unmount it. Unmounting your storage device to prevent data corruption. Before you remove a storage device, it always needs to be unmounted.

Unmount the Drive

You can use the umount command to unmount a drive.

sudo umount /mnt/drive1

The output shows how to unmount a filesystem from the mount point.

unmount a filesystem

You can also use the name of the device to unmount the drive.

sudo umount /dev/sda2
unmount the drive

Both approaches are safe to detach the drive from the system.

Handle Busy Device Errors

There may be occasions when the system will show the device busy error. This typically indicates that files on the disk are currently open for use. You can use the command below to get processes that are using the drive.

lsof /mnt/drive1
Handle Busy Device Errors

After you close the active processes, attempt to unmount the drive once more.

Forcefully Unmount a Drive

If the system cannot unmount a device normally, you may need to forcefully unmount it. You can do this using the following command:

sudo umount -f /mnt/drive1

It forces the system to unmount a drive even if it’s already in use.

unmount Drives in Linux

However, this approach can be risky, so use it with caution. If the system is still writing files, forcefully unmounting the device may cause data loss.

Best Practices to Mount Drives

Local storage is a complex storage subsystem, and following best practices ensures safe and efficient use of storage. Some recommended practices include:

  • Make sure you are mounting/unmounting the correct device every time.
  • To ensure consistent mounting of file systems, reference the file system by its UUID in the fstab file rather than by device name.
  • Unmount removable devices before unmounting them.
  • df and lsblk commands to regularly check mounted filesystems.
  • Establish logical mount points for improved maintenance on the system.

Such practices prevent filesystem errors and increase storage reliability.

Conclusion

As a Linux user and system admin, mounting and unmounting drives are one of the basics. Mounting attaches a storage device to the Linux directory tree and makes its files accessible at that point, while unmounting disconnects it in a way that avoids data corruption. There are many commands in Linux for managing storage devices, such as mount, umount, lsblk, df, and blkid. Through the /etc/fstab file, administrators are able to manually mount drives, define what filesystem types to use, or automatically mount them. The concept of mounting and unmounting drives safely in Linux systems, which helps administrators manage disks, external storage devices, secondary partitions, etc.

FAQs

What does mounting a drive mean in Linux?
How to mount drives in Linux?
How do I check mounted drives in Linux?
What happens to your data when you remove a drive?
What is the purpose of the/etc/fstab file?
How do I mount a drive at startup automatically?

Related Post

How to Create a Linux Swap File

A swap file is a designated space on a hard disk drive ...

How to use Linux Shutdown Command

The shutdown command is a powerful tool in the Linux op...

How to List Linux Users on Ubuntu

Among the complexity of Linux systems user accounts ser...

How to Install Plesk on Linux

Plesk is a comprehensive web hosting control panel desi...

How to Analyze Server Logs for Performance Is...

Analyzing server logs is one of the best ways to see ho...

Exploring chsh Command in Linux with Examples

The chsh command a utility commonly found in Linux dist...

Leave a Comment