Linux mount command (mount file systems) [Basic Guide]

Linux mount command

The mount command in Linux is used to mount a device in the Linux file system hierarchy manually. To unmount a device, the umount command can be used.

The Linux file system is hierarchical and allows various devices to be mapped and used from the root of the/(root) system.

In this way, DVDs, other discs, pendrivers, etc., can be used quite simply, and the user will see these devices as a directory in the structure of Linux.

The Filesystem Hierarchy Standard (standard for hierarchical file system) separated two directories that will serve as hosts for devices called mount points:

  • /mnt Mount Point for momentarily mounted file systems, such as hard drives;
  • /media “mount” points for removable media, such as CD-ROMs, pendrives, etc. (they appeared in version 2.3 of the FHS).

In some distributions, the /media directory contains some other optional subdirectories for mounting removable media:

  • cdrom CD-ROM
  • cdrecorder CD writer
  • zip zip drive

The /mnt directory, on the other hand, is provided so that the system administrator can temporarily mount a file system as needed.

Generally, these directories are empty when there is no system installed.

Although the FHS defines these directories as mount points for devices, any directory can serve as a mount point.

If the directories chosen as the mount point are not empty when mounting a device, the content will not be available until the device is unmounted.

Linux FHS

The mount and umount commands are used to mount and unmount file systems.

Using the mount command

Generally, a device to be assembled and the mounting point must be entered as a parameter.

There is another possibility of manual assembly informing only the device, or only the mounting point. When this happens, the device or mount point must be configured in the /etc/fstab file:

$ mount [options] device $ mount [options] directory

/etc/fstab file

The /etc/fstab file contains the assembly information for the devices that are mounted during the system loading process.

The mount also allows the following device notations to be used to refer to a device:

NotationDescription
/dev/SDAXSpecifies the device by its name on the system;
LABEL=Specifies the device by the file system label;
UUID=Specifies the device by the file system UUID;
PARTLABEL=Specifies the device by the partition label;
PARTUUID=Specifies the device by the partition UUID;

The most common options are:

    • -a Mount all devices specified in the /etc/fstab file that do not have the noauto option selected;
    • -r Mounts the device’s file system as read-only

;

  • -w Mounts the device’s file system for reading and writing;
  • -o Specifies mounting options;
  • -t type Specifies the device’s file system type. Example: ext2, ext3, ext4, iso9660, msdos, xfs, btrfs, nfs, etc.

To assemble the various file systems, Linux must support these file systems enabled in the Kernel or loaded as a module.

/etc/filesystems file

The /etc/filesystems file provides a list of the file systems that are enabled and accepted by mount:

$ cat /etc/filesystems xfs ext4 ext3 ext2 nodev proc iso9660 vfat

Examples of using the mount:

To mount a USB card:

# mount /dev/sdb1 /mnt

To mount a Windows partition (vfat) on /dev/sda1 at the mount point /mnt for read-only:

# mount /dev/sda1 /mnt -r -t vfat

To reassemble the root partition as read-only:

# mount -or remount,/

To mount an ISO image, the “-o loop” option must be used:

# mount /tmp/imagem.iso /media/iso -o loop

To have the mount reassemble all mount points defined in /etc/fstab:

# mount -a

If no parameters or options are passed, the mount shows all mounted devices, including the pseudo-file-system /proc, /dev, /run, and /sys. It also shows the mounting options, such as reading and writing, as well as the file system:

$ mount 
sysfs on /sys type sysfs (rw, nosuid, nodev, noexec, relatime)
proc on /proc type proc (rw, nosuid, nodev, noexec, relatime)
devtmpfs on /dev type devtmpfs (rw, nosuid, size=1001736k, nr_inodes=250434, mode=755)
tmpfs on /run type tmpfs (rw, nosuid, nodev, mode=755)
/dev/xvda1 on/type xfs (rw, noatime, attr2, inode64, noquota)

/dev/xvdf on /home type ext4 (rw, noatime, data=ordered)

To assemble a device using its UUID, you must first see what the device’s UUID is with the blkid command:

# blkid /dev/sda1: uuid="1C3B15B1-cd13-4a73-b2a8-449fa3aa039f” Type="XFs” # mount UUID="1C3B15B1-cd13-4a73-b2a8-449fa3aa039f” /mnt

The advantage of using the UUID is that this disk ID never changes. The disk naming, on the other hand, can be changed when a new disk is inserted or changed in place in the IDE cable. In this way, a disk named /dev/sda can become /dev/sdb. If they are referenced by the UUID, this number does not change.

/etc/mtab file

The system maintains a file called /etc/mtab that contains information about the devices that are mounted on the system. It may contain the mount points indicated in the /etc/fstab file and also the manually mounted devices.

The /etc/mtab file is maintained by the system using the mount command. The content of this file is very similar to the /proc/mounts file.

Linux uses two different mechanisms to track mounted devices. One is implemented (read about deploy) in the kernel and exposes this information to the user space via file /proc/mounts. The other mechanism keeps track of mount points by the mount utility using the /etc/mtab file.

/proc/partitions file

Each disk detected in the system will appear in the /proc/partitions file. It is in this file that the Kernel maintains a list of the detected disks, their partitions and size.

$ cat /proc/partitions 
major minor #blocks name
8 0 488386584 sda
8 1 524288 sda1
8 2 2097152 sda2
8 3 2097152 sda3
8 4 483666951 sda4
8 16 117220824 sdb
8 17 117219328 sdb1
8 48 31950720 sdd

8 49 204800 sdd1
8 50 31744000 sdd2

The lsblk tool can provide the same information in a much clearer way:

$ lsblk 
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
da 8:0 0 465.8G 0 disk
␞ ─sda1 8:1 0 512M 0 part /boot/efi
2006/─sda2 8:2 0 ; 2G 0 part [SWAP]
suo sda3 8:3 0 2G 0 part
─sda4 8:4 0 461.3G 0 part
sdb 8:16 0 111.8G 0 disk
─sdb1 8:17 0 111.8G 0 part
sdd& nbsp; 8:48 1 30.5 G 0

disk 2006/─sdd1 8:49 1 200M 0
part ─sdd2 8:50 1 30.3G 0 part /media/UIRA/untitled

On this computer there are two hard drives: SDA with 4 partitions, and SDB with a single partition. And there is also a 30 GB pendrive, mounted in the /Media/UIRA/Untitled directory.

Using the umount command

The umount command is used to unmount devices mounted by the mount command.

Umount synchronizes the contents of the disk with the contents of the buffers (own memory for transferring data) and frees up the mounting directory.

It can either take the device that is mounted as a parameter or the mount point directory as a parameter:

$ umount [options] device

Ou

$ umount [options] directory

The options available are:

  • -a Unmount all devices listed in the /etc/mtab file, which is maintained by the mount command as a reference for all mounted devices;
  • -f Forces a device to be disassembled;
  • -t type Unmount only devices that contain the file system specified in the type.

No file may be in use on the file system that will be unmounted.

Examples:

$ umount /dev/cdrom

Ou

$ umount /media/cdrom

Never remove a removable disk such as flash memories, pendrivers and external hard drives without first disassembling the file system, as there is a high chance of data loss.

Sometimes when we try to remove a device, we receive the message that it is in use:

$ sudo umount /media/uira/untitled/ 
umount: /media/uira/untitled: the target is busy.

In this case, you can use the lsof command to find out which files and users have files in use on the device:

$ lsof /media/uira/untitled/ 
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
evince 81294 uira 16r REG 8.50 190985 2217 /media/uira/untitled/Comptia Linux+ XK0-004 Exam Objectives_ESN.pdf

soffice.b 81592 uira 5uW REG 8.50 17095 2221 /media/uira/untitled/Campus improvements bh.docx

To disassemble this device, there are two options: kindly ask the user to close what they are doing, or abruptly terminate processes 81294 and 81592 with the command “kill -9 81294 and 81592” and then disassemble the device. Check out our content with all the main Linux commands

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need help?