Mount Command in Linux

Channel: Linux
Abstract: blocks available and the path of the directory where the filesystem is mounted. mount -t ext4 This command returns all the mount filesystems of type e

All files in Linux are present as a node of a tree rooted at /. Initially, the parent directory (ext4 in default mode with Linux) is mounted to this root directory and all files added by Linux during installation are saved under this parent directory. 

Any removal/non-removal storage devices can then be mounted to the root node. For example, whenever external USB flash drives are mounted to the root it becomes discoverable by the Linux file system, and files on the drive can then be accessed as it becomes part of the root.

The Linux mount command works to facilitate the attachment of the device’s file system to the root of the tree. A mount point in Linux is the directory where the mounted file system gets attached.

In this tutorial, we learn about mount command in Linux.

Prerequisites
  • A terminal to run commands.
  • Requires root or sudo privileges to mount drives and filesystems.
  • Good understanding of Linux filesystem.
Linux file system

Filesystem on Linux defines the systematic storage of data on the disk. It defines how and what data will be stored while saving a file including where the data will be stored and how the data will be retrieved from the disk. A few examples of Linux file systems include ext, ext4, XFS, swap, and ReiserFS. 

Filesystems are responsible for defining how the inodes will store the data. This characterization forms the base construct for the maximum size of file which the filesystems support and the maximum size of volumes that the filesystem can mount with. 

mount command

The mount command is used to attach a filesystem on an external device to the root node (/) of the tree (Linux file system) using the terminal. The entire file hierarchy in Linux filesystems is mounted to this root. We can use umount command to detach the device from the root.

The standard syntax for linux mount command is:

mount -t type device dir

This communicates to the kernel to detect and attach the file system present on the device to the directory dir. 

For a number of file systems types all the mount program does is to make a kernel call and it need not share details about the filesystem types using -t mount flags. Whenever this happens, mount command independently figures out the filesystem type required for mounting the device

If only the directory or the device is given as input then the mount command looks for the mount point in the file /etc/fstab. For example, mount /dir

mount command has the option to specify the source and target device paths which helps in avoiding ambiguity while mounting the device to a target location.

mount --source <source> --target <target>

There are multiple options that mount command support to help with additional flexibility while mounting.

OptionsDescription-aMounts all filesystems available in fstab-hDisplays the available options for mount command-lDisplays all mounted file systems with label in the mount output-LMount the partition with label-MMove a subtree to other path-oDescribes the mount option, eg, loop-tIndicates the file system type used by the device. The supported file system are dependent on the kernel running. Mount command auto detects the file type if no args are provide for this parameter or if the value of the parameter has been set as auto-VDisplays the version information for mount command. Note: This might be different from the Linux kernel versions How to use mount Command in Linux

The mount command in Linux is used to attach external file systems to Linux filesystems. Mounting an external file system makes its directories and files available for use and accessible by the Linux file system and allows the users to access the data stored on the device. 

Mount command instructs the operating system that the files on external devices are ready to be associated at a specified mount point in the Linux tree hierarchy rooted at /.

Mount a Linux filesystem

Mounting filesystems of a device in Linux filesystem consists of 2 steps - creating the mount point and mounting the device.

Let’s take an example of mounting ISO image:

Create a mount point:

mkdir /media/iso

Mount the ISO file in the Linux file system:

mount -o loop /source/path_to_iso_image.iso /media/iso

Note: loop signifies the transfer function using loop device which is a block device used to map the data blocks to the blocks of a regular file.

Mount a device 

mount command provides the liberty to attach external storage devices through the command line. For example, you can mount or unmount a USB flash drive, CD-ROM, hard disk device, etc. 

Each of these external storage devices gets mounted as a node in the Linux file system tree which is rooted at /. 

Once mounted, the files on these external devices are made accessible for read/write on the Linux system as the Linux filesystem can now read through the file paths and interrupt the data stored on the file.

One of the necessary command while mounting a device in Linux is fdisk

fdisk -l

fdisk is used to manipulate the partition table of the disk. When the parameter -l is passed in, it lists all the partition tables for the device specified in the arguments. 

If there are no devices passed in as arguments for this command it gets the devices mentioned in /proc/partitions

Let’s take an example to understand how to mount an external usb flash drive:

Step 1: Once you connect the usb drive, Linux adds a new block device at /dev. Run fdisk -l to find the name of this block device file.

Check for USB drive based on the filesystem and the device size. In the above example, it's /dev/sdb1 with GPT filesystem.

Instead of using /dev/sda2, you may also use by uuid, id or path.

ls -l /dev/disk/by-uuid/
11169433-051f-4b3b-bd05-7c31c85f0d47 -> ../../sda2

ls -l /dev/disk/by-id/
ata-Samsung_SSD_870_QVO_1TB_S5SVNG0N955653H-part2 -> ../../sda2

ls -l /dev/disk/by-path/
pci-0000:02:00.1-ata-1-part2 -> ../../sda2

Step 2: Create a mount point for the usb drive.

This can be any existing or new directory within the Linux filesystem. We are using mkdir to create the new mount point where we want the usb flash drive to mount.

sudo mkdir /media/usb-dsk

Step 3: Mount the usb partition /dev/sdb1 to the mount point created in step 2.

sudo mount /dev/sdb1 /media/usb-dsk

Step 4: Run mount command to ensure that the mount was successful.

sudo mount | grep sdb1
/etc/fstab File

fstab file contains information about different file systems that the Linux system can mount. It stores the data in a tabular format where each column specifies the rules to control different file systems while mounting/unmounting them. 

The order of the entries in the table are important as the filesystems on top of the list are mounded first. Each line represents a new filesystem for mounting. 

The content of this file is read by the mount command for mounting different available filesystems. The command is executed automatically at boot where again mount reads the fstab file to get the list of available filesystems for mount.

The table in the file comprises of 6 columns specifying:

  1. device - name or the UUID of the currently mounted filesystems
  2. mount-point - path to the directory where the device is currently mounted
  3. fs-type - the filesystem type of the device, eg, ext4, swap, etc.
  4. options - describes the mount options available with the filesystem
  5. dump - used by dump to understand if the file system needs to dumped
  6. pass -  defines the order in which the file systems check needs to be done at boot time by fsck

/proc/mounts file stores the default system mount options for the Linux file systems and it’s used by Kernel to display the statuses of all currently mounted file systems with Linux.

Remounting All File Systems

mount allows linux users to re-mount all the file systems with the help of -a parameter. It communicates to the kernel to re-mount all the mounts with Linux listed in /etc/fstab. 

The filesystems are mounted in the order of their presence in fstab. 

The command once executed compares the source and target for file systems which are already mounted and the kernel tables with already mounted filesystems are cached.

mount -a

Remounts all the filesystems in fstab. In other words, it attempts to remount the already mounted filesystem.

List Mount Point

mount command in Linux provides the flexibility to check the mounted devices in the Linux filesystem. 

Often these are useful when you want to confirm and diagnose if the devices or the directory or the file on the device is mounted to the Linux filesystem. 

Additionally, Linux also provides alternative commands to get the list of all mounted file systems which are discussed below. 

There are different methods to get the mounted devices on Linux:

  • mount 

When the mount command is executed without any arguments, it returns the list of currently mounted file systems. 

For example the following command lists all the mounted filesystems available on the Linux filesystem

  • mount -l

The option -l displays all the mounted filesystems along with their corresponding labels.

  • df

df stands for disk filesystem and it is often used to get the space available on all the mounted filesystems with each file name argument. 

If there are no file name passed as argument, then the command returns the space available on all the mounted filesystems. 

The command by-default shows the disk space in blocks of 1K. 

The output of the command contains the number of blocks used, blocks available and the path of the directory where the filesystem is mounted.

  • mount -t ext4

This command returns all the mount filesystems of type ext4. The supported file systems types depend on the underlying kernel. 

We can list more than 1 filesystem type as a comma-separated list of parameters.

  • lsmod

Returns the status of modules loaded in the Linux file system along with the size of these modules in bytes and the number of times they are being used by the running programs. 

The command returns currently loaded kernel modules by reading and formatting content of the file /proc/modules

Conclusion

mount command facilitates the user to attach external file systems for accessing the files and directories stored on the external device. In this tutorial, we learned about the mount command in Linux with mounting devices in Linux along with an example for mounting a USB disk drive.

We discussed the different command line options that can help to find mount points. Towards the end of this tutorial we discussed how the different filesystem remains mounted and the different filesystem mount options available in the Linux filesystem.

Ref From: linuxopsys

Related articles