Horje
How to Mount and Unmount Drives on Linux

In the Linux operating system, everything is treated as a file. Every Linux administrator must know how to organize and access files better. In this article, we will learn about the Mount and Unmount commands.

What are mounting and unmounting in Linux?

Mounting and Unmounting are essential tasks in any Linux Operating System. It allows users to access and manage file systems on various storage devices. Let’s see the definition of mounting and unmounting.

  • Mounting: Mounting is the process of attaching a storage device or partition to a directory or mount point so that its contents can be accessed and managed by computer system users.
  • Unmounting: Unmounting is the reverse mounting process where the storage device or partition is detached from the computer system. it is making its content no longer accessible until it is mounted again in the computer system.

Prerequisites

Before mounting or unmounting drives, you need two things.

  • Superuser Access: Mounting and unmounting typically require the superuser or root privileges.
  • Terminal Access: You should know the basics of Linux Terminal.

Step-by-Step Guide to Mounting Drives on Linux

Before mounting a dive, you need to identify it. Use the ‘lsblk’ or ‘fdisk -l’ command to list all available storage devices and their partitions.

  • lsblk: This command gives all the information related to the block of devices.

Command

lsblk

Output

Output
  • fdisk: This command utility to view and manipulate the partition table. This command requires the super user privilege
sudo fdisk -l
fdisk -l
  • parted: This command used to mange the disk partitions.
disk partitions.

Creating a Mount Point

A mount point is a directory in linux system where the partition will be attached. Create a mount point using the mkdir command in linux.

Example: Create a mount point folder using the mkdir command.

sudo mkdir /mnt/mydrive

Mouting the Drive

Use the mount command for attach the partition to the mount point.

sudo mount /dev/sdb1 /mnt/mydrive

We can verify is mounted with ‘df -h’ command

df -h

This command list all mounted file system along with their mount point

Output

list all mounted file system

Configuring /etc/fstab for Automatic Mounting at Boot

To automatically a mount drives at boot, you need to configure that ‘/etc/fstab’ file.

  • Editing /etc/fstab
sudo vi /etc/fstab
  • Adding an Entry: Add this line for your drive.
drive_name where_to_mount  Device_Format defaults  0  2

Step-by-Step Guide to Unmounting Drives on Linux

In this section, we will see the easiest way to unmount drives on Linux. We can use the “umount” command for unmount drives on Linux system.

Note: The “unmount” command should not be mispeled for “unmount” as there are no “unmount” commands on Linux command.

To unmount, you can either specify a directory of a device name.

$ sudo umount <device|directory>

The following example demonstrates the unmount command working.

sudo umount /mnt/data
command

Handling Busy Drives and Forced Unmounting

Sometimes, a drive might be busy and can’t be unmounted. You can identify the processes using the drive with the ‘lsof’ or ‘fuser’ command in the linux operating system.

  • Listing Open Files:
lsof +f -- drive_name
Listing Open Files
  • Forcefully Unmounting a Device:
sudo umount -l drive_name

The ‘-l’ option will perform a lazy unmount, detaching the drive immediately but cleaning up references when the drive is no longer busy.

lazy unmount

Conclusion

Mounting and unmounting drives on Linux is a fundamental skill for effectively managing your storage devices. Whether you are working with internal drives, external USB drives, or network file systems. Understanding these processes ensures that you can access and manage your filesystems efficiently. Matering the mount and unmount commands empower yous to mange your linux environment with confidence, making storage devices accessible and secure.

Mount and Unmount Drives on Linux – FAQs

What does “mounting” a drive mean?

Mounting a drive means making its storage accessible to the operating system so that files and directories can be read from and written to it.

How do I check which drives are currently mounted?

You can use the ‘mount’ command without any arugments to display all currently filesystems.

Do I always need to use ‘sudo’ to mount drives?

Yes, mounting usually requires superuser(root) privileges because it involves system-level operations.

Can I mount network drives or remote fileystems?

Yes, you can mount network dries using protocols like NFS(Network File System) or Samba (SMB/CIFS). The process is similar but involves specifying the remote locations and authentication detials.

How do I unmount all mounted filesystems?

To unmount all filesystems listed in ‘/etc/fstab’ use

sudo unmount -a




Reffered: https://www.geeksforgeeks.org


Linux Unix

Related
How to Open Ports in Linux Server Firewall How to Open Ports in Linux Server Firewall
How to install CloudPanel on Ubuntu? How to install CloudPanel on Ubuntu?
How to Convert Text to Speech on Linux How to Convert Text to Speech on Linux
How to Install Netbeans on a Linux How to Install Netbeans on a Linux
How to Share Files Between Linux Computers Using NFS How to Share Files Between Linux Computers Using NFS

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
20