{"id":2549,"date":"2023-03-19T15:51:30","date_gmt":"2023-03-19T10:21:30","guid":{"rendered":"https:\/\/smarttech101.com\/?p=2549"},"modified":"2023-03-22T17:14:23","modified_gmt":"2023-03-22T11:44:23","slug":"how-to-create-fstab-entry-in-linux","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/how-to-create-fstab-entry-in-linux\/","title":{"rendered":"How to create fstab entry in Linux"},"content":{"rendered":"\n
The fstab is used to mount drives during the boot. Basically, it is represented by the file Each Linux distribution creates Before editing the Comments start with The first field (in the above example, Now, use Similarly, you can use 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 You use this field to tell about the file system type. It can be 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 The option The option The option Use The option The option You can also use Use it to decide whether the backup utility dump will backup the file system. Use 0 to ignore it and 1 to allow it.<\/p>\n\n\n\n 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).<\/p>\n\n\n\n To know more about fields look into the man page Example 1: Virtual Hard Disk (VHD)<\/a><\/p>\n\n\n\n\/etc\/fstab<\/code>. The
mount<\/code> and
unmount<\/code> 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.<\/p>\n\n\n\n
\/etc\/fstab<\/code> file during its installation. It might look like this:<\/p>\n\n\n\n
# <file system> <dir> <type> <options> <dump> <pass>\n\n# \/dev\/nvme0n1p5\nUUID=fc986af0-b252-4c73-a921-a7cb75eb4c5f \/ ext4 rw,lazytime,strictatime 0 1\n\n# \/dev\/nvme0n1p1\nUUID=B2E8-9A01 \/boot\/EFI vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 2<\/code><\/pre>\n\n\n\n
Table of Contents<\/h2>\n\n\n\n
\n
\n
A word of advice<\/h2>\n\n\n\n
\/etc\/fstab<\/code> file, make a copy of the file. And also create a live boot media<\/a>. 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.<\/p>\n\n\n\n
Explanation of the fields in fstab in Linux<\/h2>\n\n\n\n
#<\/code>. Each entry in fstab consists of 6 fields.<\/p>\n\n\n\n
The first field in the fstab<\/h3>\n\n\n\n
UUID=B2E8-9A01<\/code>) 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<\/code> and look into the directory
\/dev\/disk\/by-uuid\/<\/code>:<\/p>\n\n\n\n
\u276f lsblk --paths\nNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS\n\/dev\/loop0 7:0 0 56G 0 loop \/mnt\/VHD\n\/dev\/nvme1n1 259:0 0 931.5G 0 disk \n\u251c\u2500\/dev\/nvme1n1p1 259:1 0 16M 0 part \n\u2514\u2500\/dev\/nvme1n1p2 259:2 0 931.5G 0 part<\/code><\/pre>\n\n\n\n
$ ls -al '\/dev\/disk\/by-uuid' \ntotal 0\ndrwxr-xr-x 2 root root 160 Mar 16 18:42 .\ndrwxr-xr-x 9 root root 180 Mar 16 18:41 ..\nlrwxrwxrwx 1 root root 15 Mar 16 18:42 00E8EAD5E8EAC7CC -> ..\/..\/nvme0n1p3\nlrwxrwxrwx 1 root root 11 Mar 16 18:42 5627befe-b7a4-48c7-ae19-a71689a67d7e -> ..\/..\/loop0\nlrwxrwxrwx 1 root root 15 Mar 16 18:42 B2E8-9A01 -> ..\/..\/nvme0n1p1\nlrwxrwxrwx 1 root root 15 Mar 16 18:42 E4CA6C29CA6BF5E8 -> ..\/..\/nvme1n1p2\nlrwxrwxrwx 1 root root 15 Mar 16 18:42 EAD2EB39D2EB08A1 -> ..\/..\/nvme0n1p4\nlrwxrwxrwx 1 root root 15 Mar 16 18:42 fc986af0-b252-4c73-a921-a7cb75eb4c5f -> ..\/..\/nvme0n1p5<\/code><\/pre>\n\n\n\n
UUID=<your_uuid><\/code> as your first field.<\/p>\n\n\n\n
PARTUUID=<your_partition_uuid><\/code> to use the partition UUID. To find the partition UUID, look into the directory
\/dev\/disk\/by-partuuid<\/code>. 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.<\/p>\n\n\n\n
The second field in the fstab<\/h3>\n\n\n\n
of $HOME<\/code> or
~<\/code>.<\/p>\n\n\n\n
The third field in the fstab<\/h3>\n\n\n\n
ext4<\/code>,
vfat<\/code>,
ntfs<\/code>, etc. which you used during formatting your partition. Use
auto<\/code> in case you don’t know about it. In that case, fstab will try to find the file system type from the drive.<\/p>\n\n\n\n
The fourth field in the fstab in linux (fstab ‘options’)<\/h3>\n\n\n\n
--option<\/code> in the mount command<\/a>. Put all the options separated by a comma. Here, I will describe a few important options. To know more, look at the man pages.<\/p>\n\n\n\n
auto<\/code> in the fourth field means the device will be mounted automatically during boot time.<\/p>\n\n\n\n
exec<\/code> will give the execution permission on the binaries while
noexec<\/code> will prevent such execution.<\/p>\n\n\n\n
umask<\/code>,
fmask<\/code> and
dmask<\/code> define permissions on files and directories. The option
dmask=0000<\/code> means all directories will have r,w, and x permissions. Not mentioning them means
fmask<\/code> and
dmask<\/code> take the value of
umask<\/code> and if you do not mention
umask<\/code>, it becomes
000<\/code> by default. But please note that these permissions don’t work as intended in the NTFS file system. The
noexec<\/code> option in fstab does not work with NTFS; hence you have to use
dmask=0000,fmask=0111<\/code> to have
exec<\/code> permission on directories but not on files.<\/p>\n\n\n\n
rw<\/code> means that the files in the drive will have read and write permissions. Use
ro<\/code> for read-only.<\/p>\n\n\n\n
user<\/code> to permit any user to mount the drive. Use
nouser<\/code> to permit only the root user to mount the drive.<\/p>\n\n\n\n
defaults<\/code> is the most widely used option<\/strong>. It is basically
rw,suid,dev,exec,auto,nouser,async<\/code>.<\/p>\n\n\n\n
nofail<\/code> is very important<\/strong>. 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<\/code> with this option to tell the systemd to check for this amount of time. When
nofail<\/code> 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 \u2192 faster bootup.<\/p>\n\n\n\n
atime<\/code> related options (ex –
relatime<\/code>,
lazytime<\/code>,
noatime<\/code>,
strictatime<\/code>, etc.) If you don’t set it, the system assumes
relatime<\/code> by default. To know about them look at my article on *atime<\/a>.<\/p>\n\n\n\n
The fifth field in the fstab in linux<\/h3>\n\n\n\n
The sixth field in the fstab<\/h3>\n\n\n\n
man 5 fstab<\/code> and Ubuntu’s article on fstab<\/a>.<\/p>\n\n\n\n
Examples of the fstab entry<\/h2>\n\n\n\n
\/mnt\/crucial\/linux_backup.img \/mnt\/VHD ext4 defaults,nofail 0 0<\/code><\/pre>\n\n\n\n