The /etc/fstab file on Linux Explained

To use a hard disk partition or external file system, we need to mount it on the Linux file system. There are two ways to mount a disk partition or external file system: temporary and permanent. We use the mount command to mount it temporarily. We use the /etc/fstab file to mount it permanently.

The /etc/fstab file stores mount points' information. The kernel reads this file at the boot time to mount partitions to the Linux file system. If we want to mount a partition permanently, we must create an entry for that partition in this file.

A fstab file entry has six fields. The following table explains their meanings.

Field Description
First It describes the device or partition's name we want to mount.
Second It describes the mount point's name.
Third It describes the file system of the partition.
Fourth It describes the mount options.
Fifth The dump command uses it to determine whether it should dump the mounted partition at the boot time.
Sixth The fsck command uses it to determine whether it should check and repair the mounted partition at the boot time.

Using the /etc/fstab

Let us take an example to understand how to use the /etc/fstab file to mount partitions permanently.

Suppose you attached a new hard disk to the system and created partitions. You can use the fdisk -l command to list partitions on a disk. The following image shows a new hard disk with partitions.

the fdisk l command

To learn how to attach a new hard disk to the system and create partitions and a file system on them, you can check the following tutorials.

Now suppose, you want to mount these partitions permanently. To mount a partition permanently, you need to create an entry in the /etc/fstab file. The kernel reads and uses this file at the boot time to mount partitions.

the fstab file

Before we create entries for our partitions, let us take the backup of the current file. A backup copy allows us to restore the system to a working state if we make any mistakes.

backup

To mount a partition, we need a directory. To mount four partitions, we need four directories. Create four directories.

make directory

Open the /etc/fstab file and create entries for the partitions at the end of existing entries.

default fstab

In the first field, specify the absolute path of the partition you want to mount.

fstab entry first field

In the second field, specify the directory on which you want to mount the partition specified in the first field.

fstab second field

In the third field, you need to specify the file system the partition has.

fstab thrid field

The fourth field describes the mount options associated with the filesystem. For practice, you can use the default option here.

fstab file fourth field

In the fifth and sixth fields, use a value of zero.

The dump command uses the fifth field to determine whether the mounted device or partition needs to be dumped during the boot time.

the fstab file fifth field

The fsck command uses the sixth field to determine whether it should check the file system on the mounted partition or device.

sixth field

To enable these options, we need to use a value of one in these fields. If we enable these options, it will improve system performance and increase the boot time. On a production system, based on your requirements, you can enable or disable these options. Since we created these partitions only for practice, we do not need to enable these options.

Save the file.

save the fstab file

To test these partitions, restart the system.

restart the system

The kernel reads the /etc/fstab file to mount partitions at boot time. If it fails to mount any partition, it halts the boot process. If the system boots normally, it verifies all partitions specified in the fstab file have been mounted successfully.

After restart, we can use the lsblk command to list partitions.

lsblk command

As we can see in the above output, all partitions are mounted successfully. It verifies the /etc/fstab file entries.

ComputerNetworkingNotes Linux Tutorials The /etc/fstab file on Linux Explained