The /etc/fstab file in Linux

The fstab file in Linux stands for "filesystem table" and is commonly found in the /etc directory. It typically lists all available disk partitions and other types of file systems, their mountpoints and mount options. fstab is meant to be only read by programs and never written except by the system administrator. It is read by the mount command, which happens automatically at boot time to determine the overall file system structure, and thereafter when a user executes the mount command to modify that structure. The system administrator has to properly create and maintain this file.

How fstab is Structured

The table itself is a 6-column structure, where each column designates a specific parameter and must be set up in the correct order. The fields of the table are as follows from left to right:

  1. Block device - the device that should be mounted. The most typical way to reference a block device is by using its location /dev/sda1 or by using its LABEL or UUID (Universal Unique IDentifier). The latter is the preferred method since it guarantees to univocally reference a filesystem. On GPT partitioned disks it’s also possible to reference a filesystem by using PARTUUID or PARTLABEL.

  2. Mount point - specifies the mountpoint for the filesystem. It is a directory in the system that should be used to access its content. This should always be provided except if the block device is used as a swap. In that case "none" should be used.

  3. Filesystem type - the type of filesystem in use on the raw block device or partition. Linux supports many filesystem types: ext4, xfs, btrfs, f2fs, vfat, ntfs, hfsplus, tmpfs, sysfs, proc, iso9660, udf, squashfs, nfs, cifs, and many more.

  4. Mount options - a list of options when mounting the filesystem. To use the default set of mount options we specify defaults as a value. The kernel default is usually rw, suid, dev, exec, auto, nouser, async.

    • suid - respect SETUID and SETGID bits;

    • exec - allow executing binaries and scripts;

    • noauto - do not mount when mount -a is given (e.g., at boot time);

    • user - allow a user to mount;

    • nouser - make the filesystem not mountable by a standard user;

    • async - perform I/O operations on the filesystem asynchronously;

    • owner - allow a device owner to mount;

    • nofail - do not report errors for this device if it does not exist.

  5. Dump - enable (1) or disable (0) the backing up of the device/partition. The value is used by the dump backup program (if installed) to know what filesystem should be dumped. Usually, this field is set to 0, which disables it.

  6. Pass - establishes the order by which another utility, fsck should check filesystems on boot. 0 means that fsck will not check the filesystem. Numbers higher than this represent the check order. The root filesystem should be set to 1 and other partitions set to 2.

An example of fstab table on a Vagrant Ubuntu virtual machine.

Each filesystem is described on a separate line. Lines starting with '#' are comments. Blank lines are ignored. Fields on each line are separated by tabs or spaces.

Hands-on Exercise Overview

The main purpose of this hands-on exercise is to gain practical experience in configuring filesystem mounts using the /etc/fstab file. This hands-on shows how to add a new entry to the /etc/fstab file to mount a filesystem and tests the configuration by mounting and unmounting the filesystem.

Hands-on Exercise

  1. Run the command that allows us to find out whether there are storage volumes that are not mounted:

     lsblk
    

  2. To get a listing of mounted devices and find out whether our disk device is mounted or not, run:

     mount | grep sdc
    

    💡
    Choose /mnt directory for permanent storage to mount the disk.
  3. To add a new entry to the end of the /etc/fstab file:

     sudo vim /etc/fstab
     /dev/sdc /mnt/mydisk ext4 defaults 0 0
    

  4. To create the directory for the storage volume to be mounted:

     sudo mkdir /mnt/mydisk
    
    💡
    Without this directory exists, the fstab file would not be able to find it and it will cause the problem.
  5. To mount the device manually, run:

     sudo mount /mnt/mydisk
    

    This command assumes that the details of what to mount (like the device or remote filesystem) are already specified in the /etc/fstab file. The target directory /mnt/mydisk was found in the fstab file and the system knows that this directory is associated with the device /dev/sdc. Thus, the mount command was simplified.

  6. To unmount the device, run:

     sudo unmount /mnt/mydisk
    

  7. To ensure the new entry mounts correctly by simulating the boot-time mounting process, run:

     sudo mount -a
    

    This command automatically mounts all filesystems specified in the /etc/fstab file without requiring further human interaction. By running this command, you instruct the system to read /etc/fstab and mount all listed filesystems according to the defined parameters, making it useful for ensuring all necessary filesystems are mounted, especially after a system reboot.

    💡
    The option noauto in the mount options field of the fstab file does not mount when mount -a is given (e.g., at boot time). There are several reasons to use this option. 1. It allows you to control when these filesystems are mounted. 2. It simplifies the mount command since there is no need for the device name, and only the target directory can be referred to.
  8. To find out the UUID of the storage volume, run:

     sudo blkid
    

    It is recommended to use the UUID (Universally Unique Identifier) of a storage volume instead of the device path (like /dev/sdc) in the /etc/fstab file, because UUIDs are unique and remain constant while device paths can change depending on the order in which devices are detected by the system.

💡
It is highly recommended to use the option ro since it prohibits writing or deleting the storage volume.

References:

  1. Linux Crash Course - The /etc/fstab file

  2. fstab

  3. fstab(5) — Linux manual page

  4. An introduction to the Linux /etc/fstab file

  5. How fstab works – introduction to the /etc/fstab file on Linux