How to Mount and Unmount Filesystems in Linux?


Introduction

In Linux, everything (picture, binary file, text file, directory etc.) is treated as file. It is important to know how to organize and access files in a better way. Mount and umount commands are very handy in this case.

In this article, we will learn these two commands. In short, using mount command we can mount a file system into a directory and using umount command we can umount the same file system from that directory. These can be done for hard disk and USB drive also. We have remember that all mount and umount commands works in “sudo” or “root” user only.

List Down all Storage Devices

Before we learn about mount and umount command we need to list down all storage devices in Linnux system.

Command1

sudo fdisk -l

Output

[sudo] password for rian:
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.

Command 2

$ lsblk

Output

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 /

“mount “command to know all currently mounted file systems

If we just type “mount” command, we can get all information like what are the currently mounted file system.

Command

$ mount

Output

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)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=202976k,mode=755)
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)

------Many lines-----

cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=22,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=13500)
mqueue on /dev/mqueue type mqueue (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
………………………………………

We can also view the above information using below command also.

$ cat /proc/mounts

Now, let us understand only below line from all these output of “mount” command

/dev/sda1 on / type ext4

/dev/sda1 => This is file system name.

on / => This is called mount point. “/” means it’s mounted in root directory.

type ext4 => Here type of file system is ext4.

“mount -t” command to know specific file systems information

If we use –t option then we can get information on specific filesystem (Ex: ext4)

Command

mount -t ext4

Output

/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)

“mount” Command to mount a file system

This is very simple .We can use below two commands to mount any file system.

  • Create the mount point directory using “mkdir” command.

  • Mount the required filesystem.

mount -t Type Device <Directory name created in step1>

Here “Type” can be ext4 and “Device” can be /dev/sda1.

“umount” Command to unmount a file system

After mounting a filesystem we can umount the same filesystem using “umount” command.

Command

$ umount /dev/sda1

Or

$ umount <mount point directory>

After this command we are no longer see the files inside the last mounted mountpoint directory.

We can also use the same “umount” command to unmount multiple filesystem in same time.

Command

$ umount /dev/sda1 /dev/sda2

“umount -l” command to unmount a file system

“umount –l” command is used to unmount a filesystem when user is not sure if any read or write operation in going on the targeted filesystem. This is command waits for any ongoing operation to get finished and then do the unmount. This is also called as lazy unmount.

As per man page

-l, --lazy              detach the filesystem now, clean up things later

Command

$ umount –l /dev/sda1

“umount -f” command to forcefully unmount a file system

“umount –f” is used to forcefully unmount a filesystem evenif there is ongoing read or write operation in that filesystem.

Command

$ umount -f /dev/sda1

This is used when a network share is not reachable.

Conclusion

From this article, we have gained knowledge on “mount” and “umount” commands with many arguments and understood the importance of these two commands. Now, depending on situation we can use these commands and do our job much faster way in Linux.

Updated on: 08-May-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements