Understanding the /etc/fstab file in Linux

The file systems and their mount points in the directory tree are configured in the file /etc/fstab. This file contains 1 line with 6 fields for each mounted file system. The lines look similar to the following:

Each field provides the following information for mounting the file system:
Field 1. Lists the name of the device file, or the file system label, or the UUID (Universally Unique Identifier). Use of LABEL=label or UUID=uuid has the advantage that the partition is mounted correctly even if the device file used changes, for instance, because you swapped hard disks on the IDE controller.
Field 2. Lists the mount point—the directory to which the file system should be mounted. The directory specified here must already exist. You can access the content on the media by changing to the respective directory.
Field 3. Lists the file system type (such as ext2, ext4).
Field 4. Shows the mount options. Multiple mount options are separated by commas (such as noauto,user,sync).
Field 5. Indicates whether to use the backup utility dump for the file system. 0 means no backup.
Field 6. Indicates the sequence of the file system checks (with the fsck utility) when the system is booted:

  • 0: file systems that are not to be checked
  • 1: the root directory
  • 2: all other modifiable file systems; file systems on different drives are checked in parallel

While /etc/fstab lists the file systems and where they should be mounted in the directory tree during startup, it does not contain information on the actual current mounts.

The /etc/mtab file lists the file systems currently mounted and their mount points. The mount and umount commands affect the state of mounted file systems and modify the /etc/mtab file.

The kernel also keeps information for /proc/mounts, which lists all currently mounted partitions. For troubleshooting purposes, if there is a conflict between /proc/mounts and /etc/mtab information, the /proc/mounts data is always more current and reliable than /etc/mtab.

View Currently Mounted File Systems

You can view the file systems currently mounted by entering the command mount. Information similar to the following appears:

# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=487424k,nr_inodes=121856,mode=755)
/dev/vda1 on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
...

You can also view this information in the file /proc/mounts.

# cat /proc/mounts

Mounting File Systems

A file system residing on a SATA/PATA or SCSI device needs to be mounted manually to access it. The mount command allows the root user to manually mount a file system. The first argument of the mount command specifies the file system to mount. The second argument specifies the target directory where the file system is made available after mounting it. The target directory is referred to as a mount point.

The mount command expects the file system argument in one of two different ways:

  • The device file of the partition holding the file system, residing in /dev.
  • The UU/D, a universal unique identifier of the file system.
Note: As long as a file system is not recreated, the UUID stays the same. The device file can change; for example, if the order of the devices is changed or if additional devices are added to the system.

The blkid command gives an overview of existing partitions with a file system on them and the UUID of the file system, as well as the file system used to format the partition.

# blkid
/dev/vda1: UUID="74309cb6-4564-422f-bd07-a13e35acbb7a" TYPE="xfs" 
/dev/vdb: UUID="2018-04-26-04-03-48-00" LABEL="config-2" TYPE="iso9660"

To mount a device file pr partition which holds the filesystem:

# mount /dev/vda1 /mount_point

To mount the file system by universal unique id, or the UUID, of the file system.

# mount UUID="46f543fd-7Bc9-4526-a857·244811be2dBB" /mount_point
Note: If the directory acting as mount point is not empty, the files that exists in that directory are not accessible as long as a file system is mounted there. All files written to the mount point directory end up on the file system mounted there.

Unmounting file systems

To unmount a file system, the umount command expects the mount point as an argument. Change to the /mount_point directory. Try to umount the device mounted on the /mnount_point mount point. It will fail.

# umount /mount_point
umount: /mount_point: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

Unmounting is not possible if the mount point is accessed by a process. For umount to be successful, the process needs to stop accessing the mount point. You can use lsof or fuser commands to identify the processes running on a particular mount point. Once you confirm that no processes are running on the mount point, you can umount the mount point without any error.

# umount /mount_point
Related Post