How to Mount and Unmount Filesystem / Partition in Linux (Mount/Umount Command Examples)

Once you insert new hard disks into your system, you’ll typically use utilities like fdisk or partedto create partitions. Once you create a partition, you’ll use mkfs command to create ext2, ext3, or ext4 partition.

Once you create a partition, you should use mount command to mount the partition into a mount point (a directory), to start using the filesystem.

This tutorial explains everything you need to know about both mount and umount command with 15 practical examples.

The general mount command syntax to mount a device:

1. Mount a CD-ROM

The device file for CD would exist under /dev directory. For example, a CD-ROM device will be mounted as shown below.

In the above example, the option “-o ro” indicates that the cdrom should be mounted with read-only access. Also, make sure that the destination directory (in the above example, /mnt) exist before you execute the mount command.

2. View All Mounts

After you execute mount a partition or filesystem, execute the mount command without any arguments to view all the mounts.

In the example below, after mounting the USB drive on a system, the output of mount looks like the below. As seen below, the USB device (i.e:/dev/sdb) is mounted on /media/myusb, which is displayed as the last line in the mount command.



You can also use df command to view all the mount points.

3. Mount all the filesystem mentioned in /etc/fstab

The filesystems listed in /etc/fstab gets mounted during booting process. After booting, system administrator may unmount some of the partitions for various reasons. If you want all the filesystems to be mounted as specified in /etc/fstab, use -a option with mount as shown below:

Example /etc/fstab file entries:

Execute mount command with -a option to mount all the /etc/fstab entries.

The same -a option can be used with umount to unmount all the filesystems mentioned in /etc/mtab

Some filesystem are not unmounted as its busy or currently in use. Note that the files /etc/mtab and /proc/mounts contents would be similar.

4. Mount only a specific filesystem from /etc/fstab

When you pass only the directory name to mount, it looks for mount point entries, if not found, then search continuous for a device in /etc/fstab and gets mounted.

As seen above, /mydata directory is not a mountpoint, but it is present in /etc/fstab.

If you execute the same again, you would get the error message as follows:

Here you may also pass the device name instead of directory name (to be picked up from /etc/fstab file).

Note that the files /etc/mtab and /proc/mounts contents would be similar.

5. View all mounted partitions of specific type

It is possible to list only the specific type of filesystem mounted using the option -l with -t as shown below:

As seen above, /dev/sda6 is the only ext2 partition and /dev/sda5 is the only ext4 partition accordingly.

6. Mount a Floppy Disk

The device file for floppy disk would exist under /dev directory. For example, a floppy disk will be mounted as shown below.

After the successful mount, you would be able to access the contents of the floppy disk. Once you are done with it, use umount before you physically remove the floppy disk from the system.

7. Bind mount points to a new directory

The mountpoint can be binded to a new directory. So that you would be able to access the contents of a filesystem via more than one mountpoints at the same time.

Use -B option with olddir and newdir to be binded as follows,

Now the bind is done and you might verify it as follows,

As seen above the bind is done properly. So when you do modification in filesystem in one place, you can see those reflection of it in other mount point as shown below:

8. Access contents from new mount point

Mount allows you to access the contents of a mount point from a new mount point. Its nothing but move a mounted tree to another place.

In the example below, the mount point /mydata will be accessed from /mnt using the option -M as shown below:

Once its done, you cant use the old mount point as its moved to a new mount point and this can be verified as shown below:

9. Mount without writing entry into /etc/mtab

During read only mount of /etc/, the /etc/mtab file entries cannot be modified by mount command. However, mount can be done without writing into /etc/mtab by using the option -n as follows,

You cannot see any entry for this /mydata in mount command output and as well from /etc/mtab file as follows:

Access the contents of a mounted directory /mydata:

10. Mount filesystem with read or read/write access

To mount partition as read only, use -r option which is synonym to -o ro.

ext3 and ext4 filesystem would still allow you to do write operation when the filesystem is dirty. So, you may have to use “ro,noload” to prevent these kind of write operation.

To mount a partition with read/write access, use -w option which is same as “-o rw” (i.e : default).

11. Remount the mounted filesystem

In order to mount the already mounted filesystem, use remount option and its normally used to remount the filesystem with read/write access when its previously mounted with read access.

The /mydata mount point is going to be remounted with read/write access from read access as shown below:

12. Mount an iso image into a directory

The iso image can be mounted as shown below:

13. Unmount more than one mount points

Umount allows you to unmount more than mount point in a single execution of umount of command as follows:

14. Lazy unmount of a filesystem

This is a special option in umount, in case you want to unmount a partition after disk operations are done. You can issue command umount -l with that partition and the unmount will be done after the disk operations gets finished.

For instance, consider a scenario that a task (i.e: script or any other command) is doing a copy operation on a disk and at the same time you are allowed to issue a unmount with -l, so that unmount would be done once the copy is over (i.e: the disk operation).

15. Forcefully unmount a filesystem

umount provides the option to forcefully unmount a filesystem with option -f when the device is busy as shown below:

If this doesn’t work for you, then you can go for lazy unmount.

Meanwhile, you can also have a look at ps command output that which process is presently using the mountpoint as shown below:

You can also execute fuser command to find out which process is holding the directory for operations.

It gives you the process id with username (nothing but the owner of the process). If you know what that process is, you may want to stop that process and then try the umount again.



Leave a Reply

Your email address will not be published. Required fields are marked *