Unmounting a filesystem or device is required when a mounted filesystem is no longer used. For example, external devices such as USB flash drives, CD-ROM, and floppy disks are generally required to be unmounted from the Linux file system so that the device can be removed safely without corrupting any data on the flash drive.
The umount command in Linux and Unix-based operating systems helps in performing the unmount operation for any mounted filesystem at a given mount point by instructing the kernel to detach the file system from the Linux filesystem hierarchy.
In this tutorial, we learn about umount command in Linux to unmount Filesystem.
For unmounting a filesystem or device:
- We should have a mounted filesystem at defined mount points.
- Assume administer role to unmount.
- The file system to be unmounted should be available for unmounting.
- Change the directory in a different filesystem.
- Stop all processes accessing that filesystem if necessary.
- Inform users if they are using the filesystem.
Umount command detaches a filesystem from the file system hierarchy of Linux. It works by removing the connection to the mount point of the Linux default filesystem so that the external device or the directory is no longer a part of the Linux file system hierarchy.
The kernel automatically unmounts all mounted filesystems on system reboot. Filesystems such as the /, /var, and /usr can be unmounted only during the system shutdown.
The mount point is specified by providing the path of the directory where the file system was initially mounted.
Umount command come into use when the user has to manually unmount a file system while the system is in use. For example, unmounting a USB flash drive.
Once a directory or a filesystem has been unmount, the user can no longer access the data stored on the detached filesystem. This is because the detached filesystem is no longer available at the mount point and is not the part of the Linux filesystem root at / and so the Linux file system is unable to read the data.
When the computer is shut down, all mounted file systems which were automatically or manually mounted get unmounted by the system default in a pre-defined order.
The general syntax of umount command is:
umount [option] path_to_mount_point
The path_to_mount_point is the name of the filesystem to get unmount. It can be the any of the following:
- Mount point could be directory name where file system mounted.
- Can use device name path (for example /dev/sdb1) of the filesystem
- A directory path where NFS file system mounted
- For Loopback mount, could a directory name
Umount command also provides the flexibility to add multiple file systems that need to be unmounted.
sudo umount path_to_mount_point1 path_to_mount_point2
There are multiple options that umount command support to help with additional flexibility while unmounting a filesystem or a directory.
|Unmounts all file systems available in /proc/self/mountinfo (or in /etc/mtab for deprecated versions).|
|Used for unmounting loop devices. It subsequently frees the loop device.|
|Forces unmount on a filesystem in case of an unreachable NFS system.|
|Displays the help message along with the different possible options for unmount command.|
|- Indicate lazy unmount. |
- This option deatches the mount point from the file system hierarchy instantaneously and cleans up the references to the unmounted filesystem once its status is no longer busy.
- A system reboot is required if lazy unmounting is used either with a network filesystem or local file system.
|Unmounts all the file systems which have the given option set in /etc/fstab.|
|Remount the file system as read-only if the unmount operation was unsuccessful.|
|- Indicates file system type which should be used for unmounting.|
- Option accepts multiple filesystem type as input which can be passed as a comma separated list.
- Use the prefix no for filesystem type which does not require any action to be taken by unmount.
|Returns the version of the unmount command. Note: This might differ from the Linux kernel version.|
Unmount file system
A mounted filesystem on the Linux file hierarchy can be unmounted using umount command. Unmounting is necessary to detach the file system from the Linux filesystem.
Umount works by instructing the kernel to remove the mounted file system highlighted though either the mount point. Kernel detaches the link of the filesystem from the Linus file hierarchy and the system is no longer able to access any data from the unmounted file system.
We can use the below syntax for unmounting a filesystem:
You can find mounted drives in /proc/mounts file, using mount, df, or findmnt command.
For example to unmount iso which was mounted to /media/iso, type:
sudo umount /media/iso
Another example is that once the data on the USB flash drive has been accessed, it needs to be unmounted from the Linux filesystem hierarchy to safely detach the device. If your USB is mounted to /media/usb, you unmount it using the following command:
sudo umount /media/usb
Remember all mount points saved in /etc/fstab are mounted again on reboot automatically by the kernel. So if any mount point which was unmounted manually is listed in /etc/fstab file, the device or the filesystem will automatically be mounted back again on system reboot.
Unmount a device
You can also umount using the USB device name. You can find device name by using the fdisk -l command. For example, if the device name of the usb flash drive is /dev/sdb1, unmount USB using the following command:
After umount, you can verify by filtering the mount list:
sudo mount | grep sdb1
Unmounting all file systems
Umount command allows users to detach all file systems present with -a parameter. It communicates to the Linux kernel to detach all the mounted file systems listed in /proc/self/mountinfo file apart from these 6 - nfsd, sysfs, devpts, devfs, rpc_pipefs and proc filesystem types.
The /proc/self/mountinfo file gets updated with every mount and unmount operation.
For all file systems to be unmounted, there should not be any process using any of the file systems.
If a process is using a file system, Linux will find the status of the file system as busy and will not allow the user to unmount from the Linux file system hierarchy.
Once the process no longer accesses the mount point, the status of the mounted filesystem is not marked as busy and it can be unmounted.
To unmount all filesystems, type:
Remounting the mounted file systems
Umount command in linux provides supplementing functionality to handle the events when the system fails to unmount the filesystem.
For example, umount command executed with the -r option will remount the filesystem as read-only if it fails to unmount the filesystem. This helps the filesystem being unmounted to be removed without corrupting the data that has been written to it.
For remounting the file system use the following command:
umount -r /path_to_filesystem_to_be_unmounted
Linux does not permit unmounting a file system that is currently in use by a process. It throws an error to the user stating the "device is busy" status of the filesystem and prevents it from being unmounted.
Generally, the status of the filesystem is busy when either the mount point or any subdirectory is being accessed or referenced by some process on the system. This can also happen by umount command itself as it relies on C libraries which may further have dependency on some locale files on the device which is being unmounted and so it will not allow the kernel to unmount the device.
Preventing unmount on such file systems allows the system to prevent any loss of data by not allowing the kernel to detach the filesystem which is currently in use.
However, the umount command comes with an option to force (-f) unmounting the filesystem in case the users are certain that the mount is no longer needed or the loss of file data is not a concern.
The syntax for forcing unmount is:
umount -f /path_to_mount_point
Things to keeping mind when performing force umount:
- Forcing an unmount on a file system can disrupt and kill the process accessing the file. This can cause the loss of data or might corrupt the file which is being accessed by the process on the file system which is being unmounted
- unmounting should not be forced if the current working directory lies on the file system which is being unmounted. For example, while forcing unmount on usb flash drive, ensure to move the working directory outside the flash drive before unmounting its filesystem
Alternatively, umount provides lazy unmounting of the filesystems. It executes by instantaneously detaching the filesystem from file hierarchy and performs the clean up for all references to the unmounted filesystem as soon as its status is no longer marked as busy.
Lazy unmounting does not corrupt files which are currently being accessed. Lazy unmounting is available with Linux kernels version 2.4.11 or above.
For lazy mounting use the following format:
umount -l /path_to_mount_point
Two commands which come in handy when you get "device is busy" error while trying to umount is lsof and fuser. Using lsof you can find the list of open files and processes associated with them on the filesystem. With fuser you can display user processes that are associated with files, directories or filesystem mount points. Once able to find the process ID associated, you may use the kill command to stop the action.
In this tutorial, we briefly discussed umount command in Linux. We covered different use cases of umount command for unmounting filesystems and external devices and covered unmounting multiple mount points and remounting filesystems.
Learned the device busy status code returned by the command and the different options available with umount. Further, towards the end, we discussed different ways to force unmounting on the file systems mounted in Linux.