How to Mount and Unmount File Systems in Linux

Introduction

The mount command can be used on both Linux and UNIX OS. It helps in attaching (mounting) file systems and removable devices like USB flash drives at a particular mount point in the directory tree.

In this tutorial, you will mount and unmount various file systems. We will also address few FAQs related to mount and unmount.

Step 1 – Listing the Mounted File Systems

1) If you want to list all the attached file systems, use mount command without any arguments.

mount

2) All file systems including the virtual ones like group, sysfs, and others will come in the output of the above command. Each line contains information about the device name, filesystem type, mount options, and the directory to which the device is mounted.

device_name on directory type filesystem_type (options)

3) If you want to display only certain file systems, use the -t option. For reference, if you want to print the ext4 partitions, use the below command:

mount -t ext4

Step 2 – Mounting a File System

1) To mount a file system in a given location, use the mount command in the following form:

mount [OPTION...] DEVICE_NAME DIRECTORY

2) After attaching the file system, the mount point becomes the root directory of the mounted file system. Like, to mount /dev/sdb1 file system to the /mnt/media directory, use the below command:

sudo mount /dev/sdb1 /mnt/media

3) The mount command will auto-detect the file system type when you are mounting a device with a common file system such as ext4 or xfs. There are a few file systems that are not recognizable, and you have to specify them explicitly.

You can do it by using the -t option:

mount -t TYPE DEVICE_NAME DIRECTORY

4) You will need to specify additional mount options, use -o option for that:

mount -o OPTIONS DEVICE_NAME DIRECTORY

5) You can specify multiple options as a comma-separated list. List all the mount options by typing man mount in your terminal.

👉
Do not add a space after a comma.

Step 3 – Mounting File System using /etc/fstab

1) Providing just one parameter to mount command, it will read the content of  /etc/fstab configuration file. It is to check whether the specified file system is listed or not.

2) If /etc/fstab has information about the given file system, the mount command uses the value for the other parameter. The mount options are even specified in fstab file. The /etc/fstab file has a list of entries in below form:

[File System] [Mount Point] [File System Type] [Options] [Dump] [Pass]

3) Use mount command in one of the below forms. It is to attach a file system in the /etc/fstab file:

mount [OPTION...] DIRECTORY
mount [OPTION...] DEVICE_NAME

Step 4 – Mounting the USB Drive

Usually, on most modern Linux distributions like Ubuntu, USB drives will auto mount when you insert them. But sometimes you will need to manually mount the drive. To manually mount a USB device, do the following:

1) You will first create a mount point:

sudo mkdir -p /media/usb

2) Assume that the USB drive uses /dev/sdd1 device. You will mount it to /media/usb directory by:

sudo mount /dev/sdd1 /media/usb

3) Next, to find the device as well as filesystem type, you can use any of the below commands:

fdisk -l
ls -l /dev/disk/by-id/usb*
dmesg
lsblk

4) Now, to mount exFAT formatted USB drives, install the free FUSE exFAT module and tools.

Step 5 – Mounting ISO Files

You will be able to mount an ISO file using the loop device. It is a special pseudo-device making a file accessible as a block device.

1) Firstly, you need to create a mount point using the following command:

sudo mkdir /media/iso

2) Mount the ISO file to the mount point.

sudo mount /path/to/image.iso /media/iso -o loop

3) Do not forget to replace /path/to/image.iso with a path to your ISO file.

Step 6 – Mounting NFS

1) To mount an NFS share, you will need to have a NFS client package installed on your system. So, install the NFS client on Ubuntu as well as Debian:

sudo apt install nfs-common

2) Now, install the NFS client on both CentOS and Fedora:

sudo yum install nfs-utils

3) Use the steps below to mount a remote NFS directory on your system.

First, create a directory. It will serve as the mount point for remote filesystem:

sudo mkdir /media/nfs

4) You will need to mount a remote NFS share automatically at boot. So, open the /etc/fstab file with your text editor:

sudo nano /etc/fstab

5) Then, add the below line to the file, replacing remote.server:/dir with the NFS server IP address or hostname and the exported directory:

# <file system>    <dir>       <type>   <options>   <dump>	<pass>
remote.server:/dir /media/nfs  nfs      defaults    0       0

6) Mount the NFS share, by running the below command:

sudo mount /media/nfs

Step 7 – Unmounting a File System

1) Now, detach a mounted file system using umount command followed by either the directory, where it is mounted or by the device name:

umount DIRECTORY
umount DEVICE_NAME

2) If the file system is in use, then umount command will fail to detach the file system. In such situations, you will use the fuser command. It will let you find out the processes accessing the file system:

fuser -m DIRECTORY

After, determining the processes, you can stop them and unmount the file system.

Step 8 – Lazy unmount

1) Now, use the -l (--lazy) option. It will unmount a busy file system. It will do it as soon as it is not busy anymore:

umount -l DIRECTORY

Step 9 – Force the unmount

1) Next, use the -f (--force) option to force unmount. It is useful to unmount any unreachable NFS system:

umount -f DIRECTORY

Basically, it is not a good idea to force unmount as it can corrupt the data on the file system.

FAQs based on Mount and Unmount File System

How to verify a File System is Unmounted?

To verify that you unmounted a file system or a number of the file systems, user will need to examine the output from the mount command. By, mount | grep unmounted-file-system.

Why is mounting as well as unmounting important?

Usually, when the computer is shutting down. Every mounted-storage will undergo an unmounting process. It is to ensure that all queued data is written and to preserve the integrity of file system structure on media.

Can I mount multiple file systems on the same mount point?

No, you cannot mount multiple file systems on the same mount point. Each mount point can only be associated with a single file system at a time.

Can I mount a file system that is already mounted?

No, you cannot mount a file system that is already mounted. You need to first unmount the file system before mounting it again.

Can I unmount a file system that is in use?

It is generally not recommended to unmount a file system that is in use, as it can result in data loss or corruption. You should first ensure that all files on the file system are closed before unmounting it.

What happens to files on a mounted file system when the system is shut down?

When the system is shut down, all mounted file systems are unmounted, and any changes made to the files are written to the file system. It is important to unmount the file system before shutting down the system to avoid data loss or corruption.

Conclusion

We hope this detailed guide helped you to mount and unmount File Systems in Linux.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.