fbpx mount Command in Linux: Mount Disk and USB Drive | Certificação Linux

mount Command in Linux: Mount Disk, USB Drive, and File Systems

📅 01/01/2019 ⏱ 9 min ✍️ Uira Ribeiro
Linuxcommands
mount Command in Linux: Mount Disk, USB Drive, and File Systems
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

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

The Linux filesystem is hierarchical and allows several devices to be mapped and used from the system root / (root).

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

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

  • /mnt        Mount point for temporarily mounted filesystems, such as hard disks;
  • /media    Mount points for removable media, such as CD-ROMs, flash drives, etc (appeared in FHS version 2.3).

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 filesystem, as needed.

Generally these directories are empty when they are not mounted with a filesystem.

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

If the directories chosen as mount points 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 filesystems.

Using the mount command

Generally, a device to be mounted and the mount point must be specified as parameters.

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

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

/etc/fstab file

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

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=<label>Specifies the device by the filesystem label;
UUID=<uuid>Specifies the device by the filesystem UUID;
PARTLABEL=<label>Specifies the device by the partition label;
PARTUUID=<uuid>Specifies the device by the partition UUID;

The most common options are:

  • -a            Mounts all devices specified in /etc/fstab that do not have the noauto option selected;
  • -r             Mounts the device filesystem as read-only;
  • -w           Mounts the device filesystem for read and write;
  • -o           Specifies the mount options;
  • -t type     Specifies the filesystem type of the device. Example: ext2, ext3, ext4, iso9660, msdos, xfs, btrfs, nfs, etc.

To mount the various filesystems, Linux must have support for these filesystems enabled in the Kernel or loaded as a module.

/etc/filesystems file

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

$ cat /etc/filesystems xfsext4ext3ext2nodev prociso9660vfat

Examples of mount usage:

To mount a USB card:

# mount /dev/sdb1 /mnt

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

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

To remount the root partition as read-only:

# mount -o remount,r / 

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

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

So that mount remounts all mount points defined in /etc/fstab:

# mount -a

If no parameter or option is passed, mount shows all mounted devices, including the pseudo-filesystems /proc, /dev, /run and /sys. It also shows mount options, such as read and write, as well as the filesystem:

$ mountsysfs 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 mount a device using its UUID, first you must 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 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 moved in the IDE cable. This way, a disk named as /dev/sda, can become /dev/sdb. If referenced by 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 devices that were mounted manually. 

The /etc/mtab file is maintained by the system, through the mount command. The contents of this file are 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 user space via the /proc/mounts file. The other mechanism keeps track of mount points through the mount utility using the /etc/mtab file.

/proc/partitions File

Every disk detected in the system will appear in the /proc/partitions file. This is the file where the Kernel maintains a list of detected disks, their partitions, and sizes.

$ 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:

$ lsblkNAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTsda      8:0    0 465,8G  0 disk ├─sda1   8:1    0   512M  0 part /boot/efi├─sda2   8:2    0     2G  0 part [SWAP]├─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      8:48   1  30,5G  0 disk ├─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 disks: sda with 4 partitions, and sdb with a single partition. There is also a 30 GB flash drive, mounted at the directory /media/uira/UNTITLED. 

Using the umount Command

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

Umount synchronizes the disk contents with the buffer contents (memory specifically for data transfer) and releases the mount directory.

It can receive either the mounted device or the mount point directory as a parameter:

$ umount [opções] dispositivo

Or

$ umount [opções] diretório

The available options are:

  • -a         Unmounts 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 unmounted;
  • -t type  Unmounts only devices that contain the filesystem type specified.

No file can be in use in the filesystem that will be unmounted.

Examples:

$ umount /dev/cdrom

Or

$ umount /media/cdrom

Never remove removable media such as flash drives, USB sticks, and external HDs without first unmounting the filesystem, as there is a high likelihood of data loss.

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

$ sudo umount /media/uira/UNTITLED/umount: /media/uira/UNTITLED: o alvo está ocupado.

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 NAMEevince    81294 uira   16r   REG   8,50   190985 2217 /media/uira/UNTITLED/CompTIA Linux+ XK0-004 Exam Objectives_ESN.pdfsoffice.b 81592 uira    5uW  REG   8,50    17095 2221 /media/uira/UNTITLED/melhorias campus bh.docx

To unmount 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 unmount the device. Check out our content with all the main commands in Linux.

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp