How to create fstab entry in Linux

March 19, 2023

The fstab is used to mount drives during the boot. Basically, it is represented by the file /etc/fstab. The mount and unmount commands also read this to mount/unmount drives with specific options. In this article, I will explain how it works, and its fields. I will also help you to create a mount entry for your new disk drive, NTFS drives, virtual hard disk, swapfile, etc.

Each Linux distribution creates /etc/fstab file during its installation. It might look like this:

# <file system> <dir> <type> <options> <dump> <pass>

# /dev/nvme0n1p5
UUID=fc986af0-b252-4c73-a921-a7cb75eb4c5f    /           ext4        rw,lazytime,strictatime 0 1

# /dev/nvme0n1p1
UUID=B2E8-9A01          /boot/EFI   vfat        rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro   0 2

A word of advice

Before editing the /etc/fstab file, make a copy of the file. And also create a live boot media. You might modify the file in such a way that your computer might not boot up. In that case, boot into this live environment and replace the fstab file using this backup copy.

Explanation of the fields in fstab in Linux

Comments start with #. Each entry in fstab consists of 6 fields.

The first field in the fstab

The first field (in the above example, UUID=B2E8-9A01) corresponds to the file system - the partition of your disk. You can use UUID, LABEL, or PARTUUID (for GPT). To find your device, use the command lsblk and look into the directory /dev/disk/by-uuid/:

 lsblk --paths
NAME             MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
/dev/loop0         7:0    0    56G  0 loop /mnt/VHD
/dev/nvme1n1     259:0    0 931.5G  0 disk 
├─/dev/nvme1n1p1 259:1    0    16M  0 part 
└─/dev/nvme1n1p2 259:2    0 931.5G  0 part
$ ls -al '/dev/disk/by-uuid' 
total 0
drwxr-xr-x 2 root root 160 Mar 16 18:42 .
drwxr-xr-x 9 root root 180 Mar 16 18:41 ..
lrwxrwxrwx 1 root root  15 Mar 16 18:42 00E8EAD5E8EAC7CC -> ../../nvme0n1p3
lrwxrwxrwx 1 root root  11 Mar 16 18:42 5627befe-b7a4-48c7-ae19-a71689a67d7e -> ../../loop0
lrwxrwxrwx 1 root root  15 Mar 16 18:42 B2E8-9A01 -> ../../nvme0n1p1
lrwxrwxrwx 1 root root  15 Mar 16 18:42 E4CA6C29CA6BF5E8 -> ../../nvme1n1p2
lrwxrwxrwx 1 root root  15 Mar 16 18:42 EAD2EB39D2EB08A1 -> ../../nvme0n1p4
lrwxrwxrwx 1 root root  15 Mar 16 18:42 fc986af0-b252-4c73-a921-a7cb75eb4c5f -> ../../nvme0n1p5

Now, use UUID=<your_uuid> as your first field.

Similarly, you can use PARTUUID=<your_partition_uuid> to use the partition UUID. To find the partition UUID, look into the directory /dev/disk/by-partuuid. Please note that PARTUUID is supported only for the system with GPT (GUID Partition Table). But you don't have to worry about it if your computer is new because most modern systems use it. The old systems rely on MBR instead of GPT.

The second field in the fstab

The second field decides the mount point of the drive. It is the directory where your partition will be mounted. Use full folder name instead
of $HOME or ~.

The third field in the fstab

You use this field to tell about the file system type. It can be ext4, vfat, ntfs, etc. which you used during formatting your partition. Use auto in case you don't know about it. In that case, fstab will try to find the file system type from the drive.

The fourth field in the fstab in linux (fstab 'options')

This is a very important field since it decides all the options used to mount the drive. These options are those options you use with the --option in the mount command. Put all the options separated by a comma. Here, I will describe a few important options. To know more, look at the man pages.

The option auto in the fourth field means the device will be mounted automatically during boot time.

The option exec will give the execution permission on the binaries while noexec will prevent such execution.

umask, fmask and dmask define permissions on files and directories. The option dmask=0000 means all directories will have r,w, and x permissions. Not mentioning them means fmask and dmask take the value of umask and if you do not mention umask, it becomes 000 by default. But please note that these permissions don't work as intended in the NTFS file system. The noexec option in fstab does not work with NTFS; hence you have to use dmask=0000,fmask=0111 to have exec permission on directories but not on files.

The option rw means that the files in the drive will have read and write permissions. Use ro for read-only.

Use user to permit any user to mount the drive. Use nouser to permit only the root user to mount the drive.

The option defaults is the most widely used option. It is basically rw,suid,dev,exec,auto,nouser,async.

The option nofail is very important. If you use this option, the systemd will start the computer even if the drive has been plugged out. Otherwise, your computer will not boot up if the drive is not found. You can supply the option x-systemd.device-timeout=1ms with this option to tell the systemd to check for this amount of time. When nofail option is set, the systemd checks for a drive for 90 seconds (by default). And if the drive is not found for this much time, the computer ignores the drive and boots up. The given option makes it 1ms → faster bootup.

You can also use atime related options (ex - relatime, lazytime, noatime, strictatime, etc.) If you don't set it, the system assumes relatime by default. To know about them look at my article on *atime.

The fifth field in the fstab in linux

Use it to decide whether the backup utility dump will backup the file system. Use 0 to ignore it and 1 to allow it.

The sixth field in the fstab

The last field can have a value of 1 or 2. Generally, 1 is given to the root device and all other devices are given 2 (1 means top priority while checking the device).

To know more about fields look into the man page man 5 fstab and Ubuntu's article on fstab.

Examples of the fstab entry

Example 1: Virtual Hard Disk (VHD)

/mnt/crucial/linux_backup.img    /mnt/VHD    ext4    defaults,nofail 0   0

Example 2: swapfile

/mnt/crucial/swapfile none swap defaults,nofail 0 0

Example 3: NTFS drive

UUID=E4CA6C29CA6BF5E8    /mnt/crucial    ntfs    users,rw,async,auto,dmask=0000,fmask=0111,nofail    0   2

Example 4: my Arch Linux installation partition created the following entry for the root /

UUID=fc986af0-b252-4c73-a921-a7cb75eb4c5f    /           ext4        defaults    0 1

Example 5: I modified the above entry for the / to include strictatime and lazytime options as well:

UUID=fc986af0-b252-4c73-a921-a7cb75eb4c5f    /           ext4        rw,lazytime,strictatime 0 1

Conclusion

That's all folks. Thanks. If you want to know more, read the man pages (man 8 mount, and man 5 fstab). If you have any queries/suggestions, use the comment section given below.