In Linux or Unix, mount command is used to mount(attach) a filesystem or storage device to a specific directory in the directory tree. The directory to which it is mounted is known as the mount point for the filesystem.
The umount command is used to safely unmount(detach) a mounted filesystem from the directory tree.
In this article, we will discuss to mount or unmount a mounted filesystem in Linux by using the terminal.
List the mounted filesystems using the mount command
You can list the mounted filesystems by using mount command without any option. It displays the information of all filesystem including virtual ones such cgroup, sysfs, etc. Now use the following command –
mount
You can refine the output of mount command by using the option -t
.
For example –
mount -t ext4
Mount a filesystem
To mount a filesystem in the existing directory structure we will use the mount command. The syntax of mount command is given below –
mount [options] filesystem_or_device_name directory
For example –
To mount /dev/sdb1
filesystem to /mnt/media
directory we use –
mount /dev/sdb1 /mnt/media
Usually, when you mount a device with a common filesystem such as ext4, it detects filesystem type automatically. If not detected you can explicitly mention the filesystem by using the option -t
.
How to mount an ISO file
You can mount an iso file to a specific directory so that it’s content available as the part of existing filesystem. I have Ubuntu 20.04 LTS iso in my system I will mount it to /mnt
directory. To mount iso use the following command –
sudo mount -t iso9660 -o loop ubuntu-20.04-desktop-amd64.iso /mnt
Now you can access the content by navigating to /mnt
directory.
How to mount the USB drive
In most of the distributions, a USB drive is automatically get mounted when you insert it in a USB port. If not mounted automatically you can manually mount it. When you manually mount a USB drive you can mount it to a specific directory.
Suppose we have USB /dev/sdb1
and want to mount it on /media/usbdrive
. First, create the directory usbdrive
inside media directory by using the following command-
mkdir -p /media/usbdrive
Now use the following command to mount /dev/sdb1
to /media/usbdrive
–
sudo mount /dev/sdb1 /media/usbdrive
Unmount a filesystem
To unmount a filesystem or storage device umount command is used. Following is the syntax of umount command –
umount directory_or_device
If a filesystem is in use umount command will fail to unmount. You can use the fuser
command to see which process is accessing the filesystem.
fuser -m directory_or_device
Once you identify the process you can stop it and unmount the filesystem.
Lazy unmount
The --lazy
or -l
option can be used to unmount a busy filesystem safely. This option causes the umount to wait until the filesystem is able to be safely unmounted. Run the following command if you want to use lazy unmount-
umount -l filesystem_or_device
Force unmount
You can use -f
or --force
option to forcefully unmount a filesystem. Use the command as given below to force unmount a filesystem.
umount -f filesystem_or_device
Conclusion
Now I hope you got a basic understanding of how to mount or unmount a filesystem in Linux or Unix. If you want to say something on this topic then write to us in the comments below.