How to Mount and Umount the FileSystems in Linux
Mounting File Systems Manually
A ﬁle 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 ﬁle system. The ﬁrst argument of the mount command speciﬁes the ﬁle system to mount. The second argument speciﬁes the directory to use as the mount point in the ﬁle-system hierarchy.
There are two common ways to specify the ﬁle system on a disk partition to the mount command:
- With the name of the device ﬁle in /dev containing the ﬁle system.
- With the UUID written to the ﬁle system, a universally-unique identiﬁer.
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 speciﬁed block device or all the available devices.
[[email protected] ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT 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 ﬁle system in the /dev/vdb1 partition on the directory /mnt/ data.
[[email protected] ~]# mount /dev/vdb1 /mnt/data
To mount a ﬁle 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 speciﬁc location in the ﬁle-system hierarchy.
This approach works ﬁne 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 ﬁle system.
Mounting by File-system UUID
One stable identiﬁer that is associated with a ﬁle system is its UUID, a very long hexadecimal number that acts as a universally-unique identiﬁer. This UUID is part of the ﬁle system and remains the same as long as the ﬁle 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 ﬁle system in the partition. If the ﬁle system is not mounted, the mount point will be blank.
[[email protected] ~]# lsblk -fp NAME FSTYPE LABEL UUID MOUNTPOINT /dev/vda ├─/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/vdb └─/dev/vdb1 xfs 46f543fd-78c9-4526-a857-244811be2d88
Mount the ﬁle system by the UUID of the ﬁle 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 identiﬁer, often the name given to the ﬁle 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 ﬁle systems automatically. As part of this process, any ﬁle system data cached in memory is ﬂushed to the storage device thus ensuring that the ﬁle system suffers no data corruption.
To unmount a ﬁle system, the umount command expects the mount point as an argument.
[[email protected] ~]# umount /mnt/data
Unmounting is not possible if the mounted ﬁle 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 ﬁle 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 ﬁles and the process accessing them in the provided directory. It is useful to identify which processes currently prevent the ﬁle system from successful unmounting.
[[email protected] data]# lsof /mnt/data COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME 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 identiﬁed, 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 sufﬁcient to change the current working directory to a directory outside the mount point.
[[email protected] data]# cd [[email protected] ~]# umount /mnt/data