# The /etc/fstab file in Linux

[The `fstab` file](https://man7.org/linux/man-pages/man5/fstab.5.html) 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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1713380783208/3074ceca-b84b-497e-acb5-8f1c4adc6f26.png align="center")

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[;](https://linuxconfig.org/how-to-use-special-permissions-the-setuid-setgid-and-sticky-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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1714214333121/55d465cb-3a6d-4cfe-81d9-5b13c887bb6d.png align="center")

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:
    
    ```bash
    lsblk
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1714215121280/64a8af7b-3283-404d-a494-5fca7964afc3.png align="center")
    
2. To get a listing of mounted devices and find out whether our disk device is mounted or not, run:
    
    ```bash
    mount | grep sdc
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1714215431838/18ed3963-335b-4a36-94fe-eac1ed3a6d1a.png align="center")
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">Choose <code>/mnt</code> directory for permanent storage to mount the disk.</div>
    </div>
    
3. To add a new entry to the end of the `/etc/fstab` file:
    
    ```bash
    sudo vim /etc/fstab
    /dev/sdc /mnt/mydisk ext4 defaults 0 0
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1714215652182/33abc3b5-8582-4002-831a-45ad63aab8da.png align="center")
    
4. To create the directory for the storage volume to be mounted:
    
    ```bash
    sudo mkdir /mnt/mydisk
    ```
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">Without this directory exists, the <code>fstab</code> file would not be able to find it and it will cause the problem.</div>
    </div>
    
5. To mount the device *manually*, run:
    
    ```bash
    sudo mount /mnt/mydisk
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1714217170807/8afb03b6-7095-4bb6-9c3e-cc9cac543777.png align="center")
    
    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:
    
    ```bash
    sudo unmount /mnt/mydisk
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1714217040023/04351818-9295-47ac-a19a-b8303dd03f62.png align="center")
    
7. To ensure the new entry mounts correctly by simulating the boot-time mounting process, run:
    
    ```bash
    sudo mount -a
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1714216100289/39b60397-8e86-42d0-b283-ca81eb103a14.png align="center")
    
    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.
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">The option <code>noauto</code> in the mount options field of the <code>fstab</code> file does not mount when <code>mount -a</code> 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.</div>
    </div>
    
8. To find out the UUID of the storage volume, run:
    
    ```bash
    sudo blkid
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1714218549826/c4ecda5b-3cc1-4630-82b7-4c3b10c45481.png align="center")
    
    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.
    

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">It is highly recommended to use the option <code>ro</code> since it prohibits writing or deleting the storage volume.</div>
</div>

## References:

1. [Linux Crash Course - The /etc/fstab file](https://www.youtube.com/watch?v=A7xH74o6kY0)
    
2. [fstab](https://en.wikipedia.org/wiki/Fstab)
    
3. [fstab(5) — Linux manual page](https://man7.org/linux/man-pages/man5/fstab.5.html)
    
4. [An introduction to the Linux /etc/fstab file](https://www.redhat.com/sysadmin/etc-fstab)
    
5. [How fstab works – introduction to the /etc/fstab file on Linux](https://linuxconfig.org/how-fstab-works-introduction-to-the-etc-fstab-file-on-linux)
