Linux /etc/fstab File Structure Explained – Beginners Guide

By | February 23, 2023

The fstab (File system table) file is a configuration file used by the Linux kernel to mount declared file systems during booting. It contains a set of rules describing where a file system should be mounted, access control for file systems, and additional configuration options for each file system.

In this article, we'll cover the fstab file in detail with its various properties and how to safely add a new file system entry.

Role of fstab File

The fstab file is a crucial Linux file. It serves different purposes including the following:

  • Centralized File System Management: It is a central location for managing file system mounts, ensuring consistent behavior across different environments.
  • Security: It contains rules for restricting access to specific file systems, preventing unauthorized access, and offering data protection.
  • Automatic Mounting: It allows the automatic mounting of file systems, making them readily available for use by the Operating system and other programs.

fstab File Structure

The fstab file is located in the /etc directory. Root user or "sudo" privileges are required to make changes to it. This file is present on most linux distros. You can open the fstab command in a text editor using the following command:

sudo nano /etc/fstab

In this example, we're using nano but you can use your preferred editor.

The content of a sample fstab file is shown below:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#                
# / was on /dev/sda3 during installation

UUID=b43f950a-fcc3-4ae3-abfc-c3149da92b4e /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/sda2 during installation
UUID=D856-2CC6  /boot/efi       vfat    umask=0077      0       1
/swapfile                                 none            swap    sw              0       0

The fstab file contains lines for file-system and paritions and each line actually has 6 fields separated by whitespace. These fields also called columns contain special values that indicate how the filesystem is to be managed by the linux kernel.

The columns are listed from left to right as follows:

  1. File system
  2. Mount point
  3. File system type
  4. Options
  5. Dump
  6. Pass

Each uncommented line below the column headings represents a file system entry in the table. We'll begin by examining the purpose of each column.

1. File System

The first column of the fstab table points to the file system to be mounted. This could either be a local block device or a remote file system. A block device can be referenced here, using its name or assigned Universal Unique Identifier (UUID).

Assuming we want to mount the first partition of a certain block device, we could specify its name such as "/dev/sda1" under the file system column. However, this method can potentially lead to problems, as device names can change when the system is rebooted or when disks are added and removed.

The recommended way of referencing a block device is with its UUID, which is assigned when the file system is created. Since the UUID remains unchanged, the system is guaranteed to identify the right block device, ensuring that file systems are mounted correctly.

To obtain the UUID of a given block device, we can use the "blkid" command and specify the block device name as an argument, as shown in the example below:

blkid /dev/sda3

Sample output:

/dev/sda3: UUID="b43f950a-fcc3-4ae3-abfc-c3149da92b4e" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="e66f0bc9-3097-4862-8789-a63ea4ec5233"

This output shows the UUID of the "/dev/sda3" block device.

2. Mount Point

This column indicates the directory where the file system should be mounted. When mounted, the content of the file system becomes available in this directory.

For instance, we could specify a directory, /mnt/example-data as the mount point of our file system as shown below:

UUID=b43f950a-fcc3-4ae3-abfc-c3149da92b4e  /mnt/example-data  ext4  defaults  0  0

It's worth noting that the directory must first be created otherwise the mount operation will fail. The specified directory should also be empty if you're using an existing directory to avoid overwriting important data.

3. File system Type

This column indicates the file system type of the block device or partition being mounted. Some of the supported types include:

  • ext2, ext3, ext4: These are commonly used file systems in Linux.
  • NTFS: This is a file system used by Windows OS.
  • FAT32: This is a simple file system commonly used in external storage devices such as SD cards and USB drives.
  • XFS: This is used in servers and data centers.
  • swap: This is used for swap files.

The Linux kernel uses this information to access the specified file systems, using the appropriate drivers.

To view the file system type, we can use the "blkid" or "lsblk" commands with the block device name as an argument as illustrated below:

blkid /dev/sda3

Sample output:

/dev/sda3: UUID="b43f950a-fcc3-4ae3-abfc-c3149da92b4e" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="e66f0bc9-3097-4862-8789-a63ea4ec5233"
lsblk -f /dev/sda3

Sample output:

NAME FSTYPE FSVER LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINTS

sda3 ext4   1.0         b43f950a-fcc3-4ae3-abfc-c3149da92b4e   11.8G    45% /var/snap/firefox/common/host-hunspell

4. Options

This column specifies the mount options for individual file systems. The defined options control how the file system is mounted and accessed.

The mount option can take multiple values. When multiple values are used, each value is separated by a comma as shown below:

UUID=9d437cf6-5916-448c-89d8-7d97c957f18d /mnt/mydata ext4 defaults,user,rw 0 0

While there are a lot of mount options that can be used, the "defaults" option is the most common and sufficient for ordinary cases.

The "defaults" option is a set of seven mount options that include the following:

  • rw: This option ensures that the file system is mounted with read-write access.
  • suid: This option specifies that the set user ID (SUID) bits should be enabled for files on the file system. Programs executed in a file system with this option will use the privileges of the file owner instead of that of the user executing it.
  • dev: This option specifies that character and block devices on the file system should be interpreted.
  • exec: This allows binaries on the file system to be executed. Its reverse option is "noexec", which disables binaries execution.
  • auto: This option enables the auto-mounting of file systems during boot time and whenever the "mount -a" command is executed in the terminal. Its reverse option is "noauto".
  • nouser: This option specifies that only a superuser can mount a file system. Other alternative options here are "user", "users", "owner", and "group".
  • async: This specifies that write operations should be performed asynchronously. Its reverse option is "sync", in which data writes are performed synchronously.

The man page of the mount command contains a full list of the available mount options.

5. Dump

This column indicates whether a file system should be included in backups when the "dump" utility is executed. It takes a value of either "0" or "1". When 0 is used, the file system is omitted from backups. A value of 1 will include the file system in backups.

However, this backup method is outdated and only included in the fstab file for backward compatibility with older Linux systems. It's recommended to leave the value at 0.

6. Pass

This column specifies the order in which the "fsck" (file system check) utility performs integrity checks on individual file systems during boot-up.

The pass column can have either of the following values:

  • 0: When this value is set, the file system is ignored during the check. The pass column defaults to this value if a pass value is not specified.
  • 1: A file system entry with this value is checked before others. This value should be set for the root file system.
  • 2: File systems with this value are checked after those with a pass value of "1".

It is important to note that this column does not affect the order in which file systems are mounted.

Adding a new Entry

Having covered the different columns in fstab, we can now use this information to add additional file system entries in the fstab file in the following steps:

  1. Open /etc/fstab in a text editor using the "sudo nano /etc/fstab" command
  2. Add a new file system entry below the existing entries by substituting the values shown in the example below with yours:
  3. UUID=1f58c7bf-d780-4f1b-ba00-cade94d76c39 /mnt/disk-one ext4 defaults 0 2
  4. Press Ctrl + O to write the changes
  5. Press Enter to save changes
  6. Press Ctrl + X to exit the editor

You can run "sudo mount -a" to immediately mount the new file system entry.

Conclusion

The fstab file plays a key role in Linux. It is a convenient medium for managing file system mounting and controlling access to file systems in Linux. We've also looked at how the fstab file is structured and the purpose of all 6 table columns.

In previous article, I had show how to automount partitions on system boot. You can check it here:

How to Automatically Mount Partitions at Startup on Kubuntu
Ubuntu - automatically mount partition at startup with fstab

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

Leave a Reply

Your email address will not be published. Required fields are marked *