×

Search anything:

Mounting and unmounting in Linux

Free book on Graph Algorithms

Get this book -> Problems on Array: For Interviews and Competitive Programming

Mounting is simply making a file system accessible in a computer system. In this article we discuss mounting and unmounting in the Linux operating System.

Table of contents.

  1. Introduction.
  2. Mounting.
  3. Unmounting.
  4. Summary.
  5. References.

Introduction.

The Linux file system forms a hierarchical structure tree rooted at /. This root / is mounted during startup, other file systems remain unusable until they are mounted at a mount point. For access to a file system, it needs to be mounted. A case on point where we would need mounting is whereby assuming we are running a very popular web service on a 500 GB HDD but data grows very fast and we need to increase the storage space, mounting enables us to mount a new larger-capacity storage device at any point in a directory while maintaining the same file structure.

In this article we discuss mounting and unmounting file systems.
Mounting and unmounting file systems requires root privileges on the system and the existence of the directory in which we will mount the file system in the case of mounting

Mounting.

This is the process of associating a file system to a storage device. In Linux the mount command is used so as to attach a file system to the file system hierarchy. When mounting we provide information such as files system type, file system and the mount point.

By mounting a file system onto the file hierarchy, a file system becomes a subtree of the hierarchy and thus it is possible to navigate from the rest for the file hierarchy.

Mounting can also be done from one system to another by use of a networked file system e.g NFS or AFS. We can also opt to create a file in another existing file system and format it as a file system and mount it e.g downloading an ISO file and mounting it rather than copying it to another media.

A mount point is a directory created as part of the file system e.g the root file system is mounted in the / directory.

Mounting of file systems happens during startup and it is managed by the /etc/fstab (file system table) configuration file.

This file will have a list of all file systems, their selected mount points and other file system specific options.

To view the fstab file type cat /etc/fstab command.

From the output we can see columns such as,

  • Filesystem: we can either specifies the file system by UUID(universal unique identifier) or a disk label.
  • Mountpoint: is the directory on the file system we shall use to access stored data on the disk.
  • Type: This specifies the type of the file system.
  • Options: Here we can specify options for tuning a mount.
  • Dump: We can either enable or disable dumping on the system.
  • Pass num: Here we set the order user so that fsck can check the file system.

To use a directory as a mount point, we make sure that it is empty because if we mount a file system on an existing one, the original contents are hidden and the only contents that will be visible are those of the newly mounted file system.

We can view all mounted file systems by using the mount command.

mount

For mounting drives in Linux, the mount command is used, its syntax is as follows.

sudo mount [DEVICE] [DIR]

The command takes the device containing the file system to be mounted and the mount point and once we attached the file system, the mount point will be the root directory of the newly mounted file system.

An example

sudo mount /dev/sdb1 /mnt/media

We can also specify the file system type as follows,

sudo mount -t vfat /dev/sdb1 /mnt/media

To specify additional mount points we can use the -o option.

We can check partitions in the system that have not yet been mounted by listing them as follows,

lsblk -f

We can also view block devices by using the blkid command.

Mounting a USB.

Assuming you plug in a USB stick but it fails to mount automatically, we can mount it as follows,

We first create a mount point

sudo mkdir -p /mnt/media/usb

Then mount it as follows,

sudo mount /dev/sda1 /mnt/media/usb

From the above command we have assumed the USB uses /dev/sda1 device.

Mounting NFS.

For this the NFS client package must be installed in a system.
First we create a directory that will serve as a mount point for the remote file system.

sudo mkdir /mnt/media/nfs

Secondly we edit the fstab file so that we can mount it automatically during startup.

# <file system>    <dir>       <type>   <options>   <dump>	<pass>
nfs-svr-ip-addr:/dir /mnt/media/nfs  nfs      defaults    0       0

In the file system column we can either specify a remote ip address or a hostname.

Finally we mount it as follows,

sudo mount /mnt/media/nfs

Unmounting.

All file systems are normally unmounted when the system powers down and any cached data stored in memory is flushed to the device.

Unmounting can be done manually e.g when removing writable media such as USBs.
The unmount command is used for this operation as follows,

unmount [DIRECTORY]
#or
unmount [DEVICE]

After unmounting files in the directory used as mount points, the hidden files if they existed will become visible again.

Errors can occur when we try to unmount a file system while there are processes with its files open. The lsof or fuser command is useful for checking what processes have open files.

We can check for the following condition as follows,

fuser -m DIRECTORY

Sometimes a file system can be busy, in such a case we can unmount it by use of -l (lazy unmounting) option as follows,

umount -l DIRECTORY

Although not recommended, the -f option is used to force and unmounting e.g in the case a file system is unreachable such as int the case of NFS.

umount -f DIRECTORY

Note that forcing an unmount may corrupt data in the file system.

Summary.

In order for one to be able to access a file system in Linux OS, it needs to be mounted first.

Mounting is the process of making a file system accessible in a Linux directory.
All partitions are attached to the system via a mount point.
During startup all partitions are mounted as described by the /etc/fstab file.
The df(disk full/free) command is used to display information about partitions and their mount points.

With this article at OpenGenus, you must have a strong idea of Mounting and unmounting in Linux.

References.

  1. man mount, man unmount commands for the commands' manual pages.
  2. Linux file system
  3. Linux file hierarchy
Mounting and unmounting in Linux
Share this