How to Mount and Unmount File Systems in Linux

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Introduction

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.

mount command in Linux with Examples

All files in a Linux filesystem are arranged in form of a big tree rooted at ‘/‘.These files can be spread out on various devices based on your partition table, initially your parent directory is mounted(i.e attached) to this tree at ‘/‘, others can be mounted manually using GUI interface(if available) or using mount command.
mount command is used to mount the filesystem found on a device to big tree structure(Linux filesystem) rooted at ‘/‘. Conversely, another command umount can be used to detach these devices from the Tree.

Syntax:

mount -t type device dir

Other forms:

mount [-l|-h|-V]
mount -a [-fFnrsvw] [-t fstype] [-O optlist]
mount [-fnrsvw] [-o options] device|dir
mount [-fnrsvw] [-t fstype] [-o options] device dir

These commands tells the Kernel to attach the filesystem found at device to the dir.

Notes:

  • If you leave the dir part of syntax it looks for a mount point in /etc/fstab.
  • You can use â€“source or â€“target to avoid ambivalent interpretation.
mount --target /mountpoint
  • /etc/fstab usually contains information about which device is need to be mounted where.
  • Most of the devices are indicated by files like /dev/sda4, etc. But it can be different for certain filesystems. Please refer below for more information.
man mount

Note: It is important to note that we are only discussing the standard form of mount command given as syntax. Different forms are somewhat discussed because it has certain limitations on different kernels.

Some Important Options:

  • l : Lists all the file systems mounted yet.
  • h : Displays options for command.
  • V : Displays the version information.
  • a : Mounts all devices described at /etc/fstab.
  • t : Type of filesystem device uses.
  • T : Describes an alternative fstab file.
  • r : Read-only mode mounted.

Examples:

  • Displays information about file systems mounted:
  • Mounts file systems:
  • Displays version information:
  • Unmounts file systems:

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 contains information about the device name, the directory to which the device is mounted to, the type of the filesystem and the mount options in the following form:

device_name on directory type filesystem_type (options)

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

For example, to print only the ext4 partitions you would use:

mount -t ext4

Mounting a File System

To mount a file system in a given 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, some file systems 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 whether the specified file system is listed or not.

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

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

/etc/fstab

[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 drives will auto mount when you insert it, but sometimes you may need to manually mount the drive.

To manually mount a USB device, perform 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*
# dmesglsblk

To mount exFAT formatted USB drives, install the free FUSE exFAT module and tools.

Mounting ISO Files

You can mount an ISO file using the loop device which 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 /media/iso -o loop

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

Mounting NFS

To mount an NFS 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 share 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 /media/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 file system.

Lazy unmount

Use the -l (--lazy) option to unmount 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.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
MQ-Jr
MQ-Jr
unixcop Admin

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook