How to Mount and Umount the FileSystems in Linux

Mounting File Systems Manually

A file system residing on a removable storage device needs to be mounted in order to access it. The mount command allows the root user to manually mount a file system. The first argument of the mount command specifies the file system to mount. The second argument specifies the directory to use as the mount point in the file-system hierarchy.

There are two common ways to specify the file system on a disk partition to the mount command:

  • With the name of the device file in /dev containing the file system.
  • With the UUID written to the file system, a universally-unique identifier.

Mounting a device is relatively simple. You need to identify the device you want to mount, make sure the mount point exists, and mount the device on the mount point.

Identifying the Block Device

A hot-pluggable storage device, whether a hard disk drive (HDD) or solid-state device (SSD) in a server caddy, or a USB storage device, might be plugged into a different port each time they are attached to a system.

Use the lsblk command to list the details of a specified block device or all the available devices.

[[email protected] ~]# lsblk
vda                       253:0    0   12G  0 disk
├─vda1                    253:1    0    1G  0 part /boot
├─vda2                    253:2    0    1G  0 part [SWAP]
└─vda3                    253:3    0   11G  0 part /
vdb                       253:16   0   64G  0 disk
└─vdb1                    253:17   0   64G  0 part

If you know that you just added a 64 GB storage device with one partition, then you can guess from the preceding output that /dev/vdb1 is the partition that you want to mount.

Mounting by Block Device Name

The following example mounts the file system in the /dev/vdb1 partition on the directory /mnt/ data.

[[email protected] ~]# mount /dev/vdb1 /mnt/data

To mount a file system, the destination directory must already exist. The /mnt directory exists by default and is intended for use as a temporary mount point. You can use /mnt directory, or better yet create a subdirectory of /mnt to use as a temporary mount point, unless you have a good reason to mount it in a specific location in the file-system hierarchy.

This approach works fine in the short run. However, the order in which the operating system detects disks can change if devices are added to or removed from the system. This will change the device name associated with that storage device. A better approach would be to mount by some characteristic built into the file system.

Mounting by File-system UUID

One stable identifier that is associated with a file system is its UUID, a very long hexadecimal number that acts as a universally-unique identifier. This UUID is part of the file system and remains the same as long as the file system is not recreated.

The lsblk -fp command lists the full path of the device, along with the UUIDs and mount points, as well as the type of file system in the partition. If the file system is not mounted, the mount point will be blank.

[[email protected] ~]# lsblk -fp
NAME        FSTYPE LABEL UUID                               MOUNTPOINT
├─/dev/vda1 xfs          23ea8803-a396-494a-8e95-1538a53b821c /boot
├─/dev/vda2 swap         cdf61ded-534c-4bd6-b458-cab18b1a72ea [SWAP]
└─/dev/vda3 xfs          44330f15-2f9d-4745-ae2e-20844f22762d /
└─/dev/vdb1 xfs          46f543fd-78c9-4526-a857-244811be2d88

Mount the file system by the UUID of the file system.

[[email protected] ~]# mount UUID="46f543fd-78c9-4526-a857-244811be2d88" /mnt/data

Automatic Mounting of Removable Storage Devices

If you are logged in and using the graphical desktop environment, it will automatically mount any removable storage media when it is inserted. The removable storage device is mounted at /run/media/USERNAME/LABEL where USERNAME is the name of the user logged into the graphical environment and LABEL is an identifier, often the name given to the file system when it was created if one is available.

Before removing the device, you should unmount it manually.

Unmounting File Systems

The shutdown and reboot procedures unmount all file systems automatically. As part of this process, any file system data cached in memory is flushed to the storage device thus ensuring that the file system suffers no data corruption.

To unmount a file system, the umount command expects the mount point as an argument.

[[email protected] ~]# umount /mnt/data

Unmounting is not possible if the mounted file system is in use. For the umount command to succeed, all processes needs to stop accessing data under the mount point. In the example below, the umount fails because the file system is in use (the shell is using /mnt/ data as its current working directory), generating an error message.

[[email protected] ~]# cd /mnt/data
[[email protected] data]# umount /mnt/data
umount: /mnt/data: target is busy.

The lsof command lists all open files and the process accessing them in the provided directory. It is useful to identify which processes currently prevent the file system from successful unmounting.

[[email protected] data]# lsof /mnt/data
bash    1593 root  cwd    DIR 253,17        6  128 /mnt/data
lsof    2532 root  cwd    DIR 253,17       19  128 /mnt/data
lsof    2533 root  cwd    DIR 253,17       19  128 /mnt/data

Once the processes are identified, an action can be taken, such as waiting for the process to complete or sending a SIGTERM or SIGKILL signal to the process. In this case, it is sufficient to change the current working directory to a directory outside the mount point.

[[email protected] data]# cd
[[email protected] ~]# umount /mnt/data