How to Mount and Unmount File Systems in Linux

In this guide, we are going to be learning How to mount file systems in Linux using mount command and then we will learn How to unmount a file system using umount command.

Introduction

One of the key differences between Linux and other operating systems like Windows is the fact that you need to mount a filesystem on Linux before you can use it.

Now this is one of those things that really confuses new Linux system administrators.

Understand that on Linux you must mount hard disk partitions, DVDs, USB drives, etc. before you can actually use them.

Now the good news is the process of mounting a file system on Linux is pretty easy.

It’s done using the mount command.

How to use mount command in Linux

The mount utility mounts the filesystem that’s already been created on a partition or on an optical disk or on a flash drive into an existing directory within the Linux file system.

That way when you switch into that directory what you’re actually doing is switching to the filesystem on that storage device.

To use the mount command, you enter mount and then use the -t option to specify which file system you want to use then you point to the partition that you want to mount.

Remember that here you mention the same file system configured in your partition. you can’t just use whatever file system you want.

Mounting a Partition

If I’ve created an ext4 file system on /dev/sdb1 then that’s the type of file system I have to specify with the mount command, then after the device file that we’re mounting we specify where in the file system we want to mount it at.

Mount a Hard drive
Mount a Hard drive

In this case I’m mounting it in a directory called /data you can see that I already have a directory named data inside the root (/).

# mount -t ext4 /dev/sdb1 /data

That way when I switch to the /data directory what I’m really doing is switching to /dev/sdb1 partition the first partition on the second hard disk in the system.

Display only the devices with a certain file system type

To display only the devices with a certain file system type, supply the -t option on the command line.

The following command will list all devices that are configured with the ext4 file system.

# mount -t ext4
/dev/sda1 on /boot type ext4 (rw,relatime,seclabel)
/dev/sdb1 on /data type ext4 (rw,relatime,seclabel)

Mounting a Flash Drive/USB Drive

Now here’s a little tip there may be times when you need to mount a storage device say a flash drive and because it’s a flash drive you’re not the one that actually partitioned and formatted it and so you may not know what file system it uses.

In these situations, you don’t have to specify a file system type.

You still specify the device file name and the directory you want to mount it in.

For example, let’s say I connected a USB drive whose device file name is /dev/sdb2. To mount this USB device, you can run the following command.

# mount /dev/sdb2 /media

Note: You can use the fdisk -l command to check the device file name of a USB or Flash drive.

Now be aware that on most distributions the /mnt directory is the default directory that’s used for mounting both local and remote file systems.

When I say local, I’m talking about locally connected devices such as the USB Drive or an Optical Disk while a remote file system is one that’s being accessed on another computer over a network connection.

Now you don’t have to use /mnt to mount a file system if you don’t want to.

You can mount a partition into any directory in the Linux file system that you want.

However, by convention /mnt is used by default for this purpose.

Now on most distributions /mnt is used to mount not only partitions on a hard drive but also external flash storage devices like a USB Drive. It’s also used for mounting optical devices such as a DVD.

However, be aware that some distributions will kind of split things up and they will use /mnt for mounting partitions on hard drives and they will use the /media directory for mounting removable media like an optical disk or a USB flash drive.

Mount a file system with specific permissions

You can use the -o option to specify mounting options that are used as that device is mounted in the file system.

A good example would be -ro which mounts whatever device you specify into the directory you specify but mounts it read-only so you can read information from it, but you can’t write information to it.

Here’s an example:

The following command will mount /dev/sdb1 with read permission in the /data directory.

# mount -o ro /dev/sdb1 /data

Mounting a CD-ROM drive

The following command will mount CD-ROM drive in the /media/cdrom directory.

# mount /dev/cdrom /media/cdrom

Remount a file system as Read and Write

To remount a already mounted file system with read and write permission type the following command.

# mount -o remount,rw /dev/sdb1 /data/

View all of the mounted file systems

Now after mounting the partition you can then use the mount command with no options and no parameters to simply view all of the mounted file systems.

# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
/dev/sda3 on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
/dev/sda1 on /boot type ext4 (rw,relatime,seclabel)
/dev/sdb1 on /data type ext4 (rw,relatime,seclabel)

You can also get the same output by running the following command.

# mount -l

This is a very useful command that you should remember.

You can then look through the output to verify that the device you specified is actually mounted in the directory you specified.

In this case we mounted /dev/sdb1 in the /data directory using the ext4 file system.

List of currently mounted file systems

Now you need to be familiar with this file right here /etc/mtab.

This is an important file, understand that whenever you mount or unmount a device in the Linux file system this file gets updated.

Therefore it always contains the most current list of currently mounted file systems and it is just a text file therefore you can view it using a standard text viewing utility like cat or less or more and by doing so you can see a list of mounted file systems.

Suggested Read: cat Command Examples in Linux

# cat /etc/mtab 
sysfs /sys sysfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
/dev/sda3 / xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
/dev/sda1 /boot ext4 rw,seclabel,relatime 0 0
/dev/sdb1 /data ext4 rw,seclabel,relatime 0 0

Here you can see that we have /dev/sdb1 mounted in the directory we’ve specified with the file system we specified.

In addition to the /etc/mtab file you can also view the contents of this file to view information about mounted file systems /proc/mounts and again it’s just a text file.

So you can use cat, less, or more whatever it is you want to use to view it and again we see the partition that we mounted in the this directory using the ext4 file system.

# cat /proc/mounts 
sysfs /sys sysfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
/dev/sda3 / xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
/dev/sda1 /boot ext4 rw,seclabel,relatime 0 0
/dev/sdb1 /data ext4 rw,seclabel,relatime 0 0

How to unmount mount point in Linux

Now in addition to mounting a file system you can also unmount a file system if you need to. this is done using the umount command.

Now as soon as I say this in every single class a student pipes up and says you spelled that wrong it’s not umount its unmount, no actually it is not unmount it is truly umount.

To unmount a filesystem, you enter the umount command followed by one of two different things.

Either the device that you want to unmount such as /dev/sdb1 or you can also specify the directory where that device is currently mounted.

In this case /data either one of these commands would accomplish the exact same thing.

You must follow the syntax given below.

# umount DEVICE_NAME
# umount DIRECTORY
## Unmount a file system by Device Name ##

# umount /dev/sdb1

## Unmount a file system by Mount Point ##

# umount /data

Now it’s important that you remember that when you’re trying to unmount a files ystem the device that you’re trying to unmount cannot be busy.

If it’s busy, then it’s not going to unmount.

For example, in this case I have used the cd command to change into the /data directory which is where this device is mounted and then I try to use the umount command to unmount this device and it says I can’t I’m busy somebody’s using it.

# cd /data/

[[email protected] data]# umount /data
umount: /data: target is busy.

Suggested Read: cd Command in Linux with examples

The same thing would hold true, if I had mounted say an Optical Disk or USB Drive somewhere in my file system and I were using the file system on that device.

When if I try to unmount it with the umount command it’ll give me a similar error saying hey somebody’s using it I can’t unmount it right now.

How do I forcefully unmount a Linux disk partition?

You can forcefully unmount a file system by passing the -f option to umount command.

# umount -f /data

Note: It is not recommended. Can be used in case of an unreachable NFS system.

Lazy unmount a busy disk in Linux

You want to unmount after the disk operations are done. It’s possible by passing the -l option to umount command.

# umount -l /data

How to permanently mount a drive/partition in Linux

Understand that just because you mount a file system with the mount command does not mean it’s going to stay mounted.

As long as the system stays running yeah, it’ll stay mounted but if you reboot the system the device that you specified with the mount command will not be remounted by default when the system starts back up.

But you can fix this what you do is go into your /etc/fstab file and then add the mounting information for that device to this file.

That way when the system boots up that you can be sure that, that device will be mounted in the directory you specify, and I did that right here I’ve added the /dev/sdb1 device to the /etc/fstab file.

I specified where I want it to mount it at, what file system I want to use, and then I specified my mount options here.

We’ll go into that in a little more detail here.

# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sun Jan 24 10:20:39 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=b119d862-07b7-4842-a6ea-cd2da64f8eae /                       xfs     defaults        0 0
UUID=e9f23f2d-118e-4a5e-94ab-b08afb78a3be /boot                   ext4    defaults        1 2
UUID=d3e3cc9a-6c24-4d58-9d33-4aeaa2eef5d1 swap                    swap    defaults        0 0
/dev/sdb1				  /data			  ext4	  defaults	  0 0	

Understanding the /etc/fstab file in Linux

# cat /etc/fstab 

UUID=b119d862-07b7-4842-a6ea-cd2da64f8eae /                       xfs     defaults        0 0
UUID=e9f23f2d-118e-4a5e-94ab-b08afb78a3be /boot                   ext4    defaults        1 2
UUID=d3e3cc9a-6c24-4d58-9d33-4aeaa2eef5d1 swap                    swap    defaults        0 0
/dev/sdb1				  /data			  ext4	  defaults	  0 0	

The important thing for you to remember is the fact that each line in the /etc/fstab file contains 6 different fields that you need to fill out for each file system to be mounted whenever the system boots.

Explaining fstab
Explaining fstab
  • The first field specifies the device to be mounted. In this case /dev/sdb1.
  • The second field specifies the mount point where you want that device to be mounted. In this case we want it mounted in /data.
  • The next field specifies the file system that’s been created on that device and remember that does have to match you can’t just use whatever file system you want.

    The file system you specify must be the file system that has been already been made on that particular device.
  • Next, we have our mount options which we’ll talk more about in just a second.

    I put defaults in here which we’ll just use the default mounting options. You customize this entry to make this device be mounted into the file system using a wide variety of different options.
  • The next field specifies whether or not this file system should be dumped. 0 means don’t dump it, 1 means go ahead and dump it.
  • And then the last number specifies the order in which the file system check utility should check the file system when the system starts up.

    Basically, there are only two different values that are used in this field.

    The file system that contains the root partition should always be checked first. so, on that one line in fstab file this field to be set to 1, everything else will be set to a value of 2 meaning it really doesn’t matter.

    Basically, we want the root file system always checked first and then after that it can be checked in whatever order we want.

Common Mount Options

Now in that fourth field we talked about the fact that you could use a variety of different mount options and I just use defaults just to make it easy but there are many different mount options you could include in this field.

  • rw – Specifies that we mount the file system read, write so we can read data from it and write data to it.
  • ro – Only allows us to read information, that does not allow us to write information.
  • sync – Enables synchronous input and output, which basically means that any changes you make to a file on that file system need to be written immediately and this is commonly used for devices that are removable in nature such as a USB Drive.

    Because there’s a potential lag between the time a write operation happens and the time when somebody pulls the drive out of the USB port you probably want your USB Drive information to be written right away and not held.
  • async – Enables asynchronous input/output.

    This option is used typically on devices that will not be disconnected from the system at all.

    Like a hard disk drive by enabling async all the changes that are being made to the files in the file system may be cached and then they’ll be written later on when the hard disk isn’t busy.

    By doing this we increase performance but the requirement here is that drives got to be in the system all the time otherwise you could potentially miss a write operation.
  • atime – This option specifies that whenever a file is accessed, the time when it’s accessed will be updated in the files inode.
  • noatime – It does not record the last access time in the files inode and sometimes that is used because it does provide a better overall file system performance, because we’re not constantly updating the inodes of files with their last access time.
  • nodev – Prevents the device files and /dev from being interpreted as special block devices.
  • dev – It causes the kernel to interpret device files in /dev as special block devices.
  • noexec – Prevents binaries on that file system from being executed. this can be useful from a security standpoint.
  • exec – It allows any executables on the file system to be executed.
  • nosuid – Blocks the use of SUID and SGID permissions.
  • suid – It enables the use of SUID and SGID permissions.
  • auto – Auto specifies that the file system be automatically mounted whenever the system boots.
  • noauto – It prevents the file system from being automatically mounted on system boot.
  • user – Allows users to mount the file system.
  • nouser – Specifies that only root is allowed to mount the file system.
  • defaults – As we said earlier you can just use default if you want to allow the kernel to mount the file system using the read, write, SUID, dev, exec, auto, nouser, and the async option.

You can use the mount options shown here and you can use many of them all at the same time if you want to. all you have to do is put a comma (,) between the different mount options that you want to use.

How to mount NFS share on Linux

First create a mount point. For that I am creating a new directory called data using the mkdir command.

# mkdir /data

Suggested Read: How to Create a Directory in Linux (mkdir command)

Suppose the IP Address of my NFS server is 192.168.1.45 and the directory exported via the NFS server is /data.

The following command will mount this NFS share in a directory called /mynfs.

# mount -t nfs 192.168.1.45:/data /mynfs

How to mount ISO file in Linux

Now before we end there’s one more thing, I want to show you and that’s how to mount an image file an ISO file in other words into the Linux file system.

By doing this we essentially mount the ISO image into a directory and when we switch to that directory, we’re actually able to access the contents of the ISO file.

The syntax are doing that is shown here:

# mount -o loop filename.iso mountpoint

Here is an example:

Here I have an ISO file called myfiles.iso that I want to mount to /mnt/iso

# mount -o loop myfiles.iso /mnt/iso

This can be really useful because it allows you to take an ISO file in your file system, mount it into a directory somewhere and then access the contents of the ISO file.

Basically, is if it were an optical disc in an optical drive such as a DVD in your optical drive.

You can visit at following websites to get more information on mount and umount command.

Conclusion

I hope that now you have a good understanding of how the mount and umount command works and you have some ideas for how you can use this within your workflow.

If anyone does have any questions about what we covered in this guide then feel free to ask in the comment section below and I will do my best to answer those.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment