7. /etc/fstab

In Linux, when you want to automatically mount filesystems at startup, you configure them in a special configuration file located at /etc/fstab. The name fstab is short for "filesystem table," and this file contains a permanent list of filesystems that the system should mount during the boot process. Understanding the fstab linux configuration is a key skill for any system administrator.

What is /etc/fstab

The /etc/fstab file is a system configuration file that defines all available disk partitions and other types of filesystems and data sources that are not necessarily disk-based. The system consults this file during startup to determine which filesystems to mount automatically.

Here is an example of a typical fstab file:

pete@icebox:~$ cat /etc/fstab
UUID=130b882f-7d79-436d-a096-1e594c92bb76 /               ext4    relatime,errors=remount-ro 0       1
UUID=78d203a0-7c18-49bd-9e07-54f44cdb5726 /home           xfs     relatime        0       2
UUID=22c3d34b-467e-467c-b44d-f03803c2c526 none            swap    sw              0       0

The fstab File Structure

Each line in the etc fstab file represents one filesystem and contains six fields separated by spaces or tabs. Let's break down what each field means:

  • Device Identifier: This specifies the device to mount. Modern systems use a UUID (Universally Unique Identifier) to avoid issues if the device name (e.g., /dev/sda1) changes.
  • Mount Point: The directory in the filesystem where the device will be mounted (e.g., / or /home).
  • Filesystem Type: The type of filesystem on the device, such as ext4, xfs, btrfs, or swap.
  • Options: Mount options that control how the filesystem is mounted. Common options include defaults, relatime, and errors=remount-ro. For a full list, consult the mount manpage.
  • Dump: This field is used by the dump utility to determine if a filesystem needs to be backed up. A value of 0 means it will be ignored, which is a safe default.
  • Pass: This field is used by fsck to determine the order for checking filesystems at boot time. The root filesystem (/) should be 1, other filesystems should be 2, and a value of 0 means the filesystem will not be checked.

How to Edit /etc/fstab

You can add an entry by directly modifying the /etc/fstab file using a text editor with root privileges. Be extremely careful when editing this file; an incorrect entry in the fstab can prevent your system from booting correctly. It is always a good practice to back up the file before making changes. After saving your changes, you can test them without rebooting by running the command sudo mount -a, which mounts all filesystems listed in /etc/fstab.

Sign in to save your learning progress

Sign in

Exercises

Practice makes perfect! Hands-on experience is crucial for understanding how to manage filesystems and ensure they are correctly mounted at system startup. Here are some hands-on labs to reinforce your understanding of Linux filesystem management and the /etc/fstab file:

  1. Manage Linux Partitions and Filesystems - Practice creating partitions, formatting them, mounting them, and configuring persistent mounting using /etc/fstab.
  2. Create and Activate a Swap File in Linux - Learn the essential administrative task of creating and activating a swap file, which often involves entries in /etc/fstab.

These labs will help you apply the concepts of filesystem mounting and configuration in real scenarios and build confidence with managing disk resources in Linux.

Quiz

What file is used to define how filesystems should be mounted? (Please provide the full path. The answer is case-sensitive.)