Read in 5 minutes

last updated 

How to Mount and Unmount File Systems in Linux

On Linux and UNIX operating systems you can use the mount command to attach (mount) file systems and removable devices such as USB flash drives at a particular mount point in the directory tree.

The umount command detaches (unmounts) the mounted file system from the directory tree.

In this tutorial we will go over the basics of attaching and detaching various file systems using the mount and umount commands.

How to List Mounted File Systems

When used without any argument, the mount command will display all currently attached file systems:

mount

By default, the output will include all file systems including the virtual ones such as cgroup, sysfs, and others. Each line includes information about the device name, the directory in which the device is mounted, the type of the filesystem and the mount options in the following form:

device_name on directory type filesystem_type (options)

To display only a certain file systems use the -t option.

For example to display only the ext4 partitions you would use:

mount -t ext4

Mounting a File System

To attach a file system at a specified location (mount point), use the mount command in the following form:

mount [OPTION...] DEVICE_NAME DIRECTORY

Once the file system is attached, the mount point becomes the root directory of the mounted file system.

For example to mount the /dev/sdb1 file system to the /mnt/media directory you would use:

sudo mount /dev/sdb1 /mnt/media

Usually when mounting a device with a common file system such as ext4 or xfs the mount command will auto-detect the file system type. However, there are some file systems that are not recognized, and need to be explicitly specified.

Use the -t option to specify the file system type:

mount -t TYPE DEVICE_NAME DIRECTORY

To specify additional mount options, use the -o option:

mount -o OPTIONS DEVICE_NAME DIRECTORY

Multiple options can be provided as a comma separated list (do not insert a space after a comma).

You can get a list of all mount options by typing man mount in your terminal.

Mounting a File System using /etc/fstab

When providing just one parameter (either directory or device) to the mount command, it will read the content of the /etc/fstab configuration file to check if the specified file system is listed.

If the /etc/fstab contains information about the given file system, the mount command will fill in the value for the other parameter and use the mount options specified in the fstab file.

The /etc/fstab file contains a list of entries in the following form:

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

Use the mount command in one of the following forms to attach a file system specified in the /etc/fstab file:

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

Mounting USB Drive

On most modern Linux distribution like Ubuntu USB drive will auto mount when you insert it, but sometimes you may need to manually mount the drive.

To manually mount an USB device use the following steps:

  1. Create the mount point:

    sudo mkdir -p /media/usb
  2. Assuming that the USB drive uses the /dev/sdd1 device you can mount it to /media/usb directory by typing:

    sudo mount /dev/sdd1 /media/usb

    To find the device and filesystem type you can use any of the following commands:

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

Mounting ISO Files

To mount an ISO file use the loop device. The loop device is a special pseudo-device that makes a file accessible as a block device.

  1. Start by creating the mount point, it can be any location you want:

    sudo mkdir /media/iso
  2. Mount the ISO file to the mount point by typing the following command:

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

    Don’t forget to replace /path/to/image.iso with the path to your ISO file.

Mounting NFS

To mount a NFS system you’ll need to have the NFS client package installed on your system.

  • Install NFS client on Ubuntu and Debian:

    sudo apt install nfs-common
  • Install NFS client on CentOS and Fedora:

    sudo yum install nfs-utils

Use the steps below to mount a remote NFS directory on your system:

  1. Create a directory to serve as the mount point for the remote filesystem:

    sudo mkdir /media/nfs
  2. Generally, you will want to mount the remote NFS directory automatically at boot. To do so open the /etc/fstab file with your text editor:

    sudo nano /etc/fstab

    Add the following line to the file, replacing remote.server:/dir with the NFS server IP address or hostname and the exported directory:

    /etc/fstab
    # <file system>    <dir>       <type>   <options>   <dump>  <pass>
    remote.server:/dir /media/nfs  nfs      defaults    0       0
  3. Mount the NFS share by running the following command:

    sudo mount /mnt/nfs

Unmounting a File System

To detach a mounted file system, use the umount command followed by either the directory where it has been mounted (mount point) or the device name:

umount DIRECTORY
umount DEVICE_NAME

If the file system is in use the umount command will fail to detach the file system. In those situations, you can use the fuser command to find out which processes are accessing the file system:

fuser -m DIRECTORY

Once you determine the processes you can stop them and unmount the the file system.

Lazy unmount

Use the -l (--lazy) option to deatach a busy file system as soon as it is not busy anymore.

umount -l DIRECTORY

Force unmount

Use the -f (--force) option to force an unmount. This option is usually used to unmount an unreachable NFS system.

umount -f DIRECTORY

Generally not a good idea to force unmount as it may corrupt the data on the file system.

Conclusion

By now you should have a good understanding of how to use the mount command to attach various file systems to your directory tree and detaching the mounts with the umount command.

To learn more about the mount and umount command options see their respective man pages.