Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Mount and Unmount Filesystems in Linux?
In Linux, everything (pictures, binary files, text files, directories, etc.) is treated as a file. Understanding how to organize and access files efficiently is crucial for system administration. The mount and umount commands are essential tools for this purpose.
In this article, we will explore these two commands in detail. The mount command allows us to attach a filesystem to a directory in the filesystem hierarchy, while the umount command detaches it. These operations can be performed on hard disks, USB drives, and other storage devices. Important: All mount and unmount operations require sudo or root privileges.
Listing Storage Devices
Before mounting or unmounting filesystems, we need to identify available storage devices in the Linux system.
Using fdisk Command
sudo fdisk -l
[sudo] password for user: Disk /dev/sda: 298.1 GiB, 320072933376 bytes, 625142448 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disklabel type: dos Disk identifier: 0x0002d5a1 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 620969983 620967936 296.1G 83 Linux /dev/sda2 620972030 625141759 4169730 2G 5 Extended /dev/sda5 620972032 625141759 4169728 2G 82 Linux swap / Solaris Partition 2 does not start on physical sector boundary.
Using lsblk Command
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 298.1G 0 disk ??sda2 8:2 0 1K 0 part ??sda5 8:5 0 2G 0 part [SWAP] ??sda1 8:1 0 296.1G 0 part /
Viewing Currently Mounted Filesystems
The mount command without arguments displays all currently mounted filesystems:
mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime) proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) udev on /dev type devtmpfs (rw,nosuid,relatime,size=985120k,nr_inodes=246280,mode=755) /dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev) ...
Alternatively, you can view mount information using:
cat /proc/mounts
Understanding the mount output format:
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)
/dev/sda1Device/filesystem nameon /Mount point (directory where filesystem is attached)type ext4Filesystem typeOptions in parentheses Mount options (read-write, etc.)
Filtering Specific Filesystem Types
Use the -t option to display information about specific filesystem types:
mount -t ext4
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)
Mounting a Filesystem
To mount a filesystem, follow these steps:
-
Create a mount point directory:
sudo mkdir /mnt/mydisk
-
Mount the filesystem:
sudo mount -t filesystem_type device_name mount_point
Example:
sudo mount -t ext4 /dev/sdb1 /mnt/mydisk
For USB drives and other removable media, the process is identical. Once mounted, you can access the filesystem contents through the mount point directory.
Unmounting a Filesystem
The umount command detaches a mounted filesystem. You can specify either the device name or mount point:
sudo umount /dev/sdb1
Or:
sudo umount /mnt/mydisk
After unmounting, files in the mount point directory are no longer accessible.
Unmounting Multiple Filesystems
You can unmount multiple filesystems simultaneously:
sudo umount /dev/sdb1 /dev/sdc1
Lazy Unmount
Use the -l (lazy) option when you're unsure if read/write operations are ongoing:
sudo umount -l /dev/sdb1
This option detaches the filesystem immediately but cleans up references later, waiting for ongoing operations to complete.
Force Unmount
The -f (force) option forcefully unmounts a filesystem even during active operations:
sudo umount -f /dev/sdb1
Warning: Force unmounting can cause data loss. Use this option primarily for unreachable network shares or when the system is unresponsive.
Common Mount Options
| Option | Description |
|---|---|
-o ro |
Mount as read-only |
-o rw |
Mount as read-write (default) |
-o noexec |
Prevent execution of binaries |
-o user |
Allow ordinary users to mount |
Conclusion
The mount and umount commands are fundamental tools for Linux filesystem management. Understanding these commands enables efficient handling of storage devices, from permanent hard drives to temporary USB media. Always ensure proper unmounting to prevent data corruption and maintain system integrity.
