Detailed Explanation of the fstab File in Linux

/etc/fstab is a file that stores static information about file systems. It is located in the /etc/ directory and can be viewed using the command less /etc/fstab. To modify it, use the command vi /etc/fstab.

When the system starts, it automatically reads information from this file and mounts the specified file systems to the designated directories. Below, we will introduce how to fill in information in this file.

File Example

A simple <span>/etc/fstab</span> using kernel names to identify disks:

Code language:javascript

/etc/fstab
# <file system><dir><type><options><dump><pass>
tmpfs                  /tmp          tmpfs     nodev,nosuid          00
/dev/sda1              /             ext4      defaults,noatime      01
/dev/sda2              none          swap      defaults              00
/dev/sda3              /home         ext4      defaults,noatime      02

Field Definitions

<span>/etc/fstab</span> file contains the following fields, separated by spaces or tabs:

<file system><dir><type><options><dump><pass>
  • <file systems> – The partition or storage device to be mounted.
  • <dir> – The mount point for the <file systems>.
  • <type> – The file system type of the device or partition to be mounted, supporting many different file systems:<span>ext2</span>, <span>ext3</span>, <span>ext4</span>, <span>reiserfs</span>, <span>xfs</span>, <span>jfs</span>, <span>smbfs</span>, <span>iso9660</span>, <span>vfat</span>, <span>ntfs</span>, <span>swap</span>, and <span>auto</span>. Setting it to <span>auto</span> allows the mount command to guess the file system type, which is very useful for removable devices like CDROM and DVD.
  • <options> – Parameters used during mounting; note that some mount parameters are specific to certain file systems. Some commonly used parameters include:
  • <span>auto</span> – Automatically mount at startup or when the <span>mount -a</span> command is typed.
  • <span>noauto</span> – Only mounted under your command.
  • <span>exec</span> – Allows execution of binaries on this partition.
  • <span>noexec</span> – Disallows execution of binaries on this file system.
  • <span>ro</span> – Mount the file system in read-only mode.
  • <span>rw</span> – Mount the file system in read-write mode.
  • <span>user</span> – Allows any user to mount this file system; if not explicitly defined, it implicitly enables <span>noexec</span>, <span>nosuid</span>, and <span>nodev</span> parameters.
  • <span>users</span> – Allows all users in the users group to mount the file system.
  • <span>nouser</span> – Can only be mounted by root.
  • <span>owner</span> – Allows the device owner to mount.
  • <span>sync</span> – I/O is synchronous.
  • <span>async</span> – I/O is asynchronous.
  • <span>dev</span> – Resolves block special devices on the file system.
  • <span>nodev</span> – Does not resolve block special devices on the file system.
  • <span>suid</span> – Allows suid operations and setting sgid bits. This parameter is typically used for special tasks to temporarily elevate permissions when a general user runs a program.
  • <span>nosuid</span> – Disallows suid operations and setting sgid bits.
  • <span>noatime</span> – Does not update inode access records on the file system, which can improve performance (see atime parameter).
  • <span>nodiratime</span> – Does not update directory inode access records on the file system, which can improve performance (see atime parameter).
  • <span>relatime</span> – Updates inode access records in real-time. Only updates if the access time recorded is earlier than the current access time. (Similar to noatime but does not interrupt processes like mutt or other programs that probe files to see if they have been modified since last accessed.), can improve performance (see atime parameter).
  • <span>flush</span> – An option for <span>vfat</span>, refreshes data more frequently, and the copy dialog or progress bar only disappears after all data has been written.
  • <span>defaults</span> – Uses the default mount parameters for the file system, for example, the default parameters for <span>ext4</span> are:<span>rw</span>, <span>suid</span>, <span>dev</span>, <span>exec</span>, <span>auto</span>, <span>nouser</span>, <span>async</span>.
  • <dump> – The dump tool uses this to determine when to back up. Dump checks its contents and uses a number to decide whether to back up this file system. Allowed numbers are 0 and 1. 0 means ignore, and 1 means back up. Most users do not have dump installed, for them <dump> should be set to 0.
  • <pass> – fsck reads the value of <pass> to determine the order in which file systems need to be checked. Allowed numbers are 0, 1, and 2. The root directory should have the highest priority of 1, and all other devices that need to be checked should be set to 2. 0 means the device will not be checked by fsck.

File System Identification

In the <span>/etc/fstab</span> configuration file, you can represent file systems in three different ways: kernel name, UUID, or label. The advantage of using UUID or label is that they are independent of disk order. If you change the order of your storage devices in the BIOS, or reinsert storage devices, or due to some BIOS that may randomly change the order of storage devices, using UUID or label will be more effective. See Persistent Block Device Names.

To display basic information about partitions, run:

$ lsblk -f
NAMEFSTYPELABELUUIDMOUNTPOINT
sda                                                         
├─sda1 ext4   Arch_Linux 978e3e81-8048-4ae1-8a06-aa727458e8ff /
├─sda2 ntfs   Windows    6C1093E61093B594                     
└─sda3 ext4   Storage    f838b24e-3a66-4d02-86f4-a2e73e454336 /media/Storage
sdb                                                            
├─sdb1 ntfs   Games      9E68F00568EFD9D3                     
└─sdb2 ext4   Backup     14d50a6c-e083-42f2-b9c4-bc8bae38d274 /media/Backup
sdc                                                            
└─sdc1 vfat   Camera     47FA-4071/media/Camera

Kernel Name

You can use <span>fdisk -l</span> to obtain the kernel name, prefixed with <span>dev</span>.

Label

Note: When using this method, each label must be unique.

To display the labels of all devices, you can use the <span>lsblk -f</span> command. In <span>/etc/fstab</span>, use <span>LABEL=</span> as the prefix for the device name:

/etc/fstab
# <file system><dir><type><options><dump><pass>
tmpfs                  /tmp          tmpfs     nodev,nosuid   00
LABEL=Arch_Linux       /             ext4      defaults,noatime      01
LABEL=Arch_Swap        none          swap      defaults              00

UUID

All partitions and devices have a unique UUID. They are generated by the file system creation tools (<span>mkfs.*</span>) when the file system is created.

<span>lsblk -f</span> command will display the UUID values of all devices.<span>/etc/fstab</span> uses the <span>UUID=</span> prefix:

/etc/fstab
# <file system><dir><type><options><dump><pass>
tmpfs                                     /tmp          tmpfs     nodev,nosuid          00
UUID=24f28fc6-717e-4bcd-a5f7-32b959024e26 /     ext4              defaults,noatime      01
UUID=03ec5dd3-45c0-4f95-a363-61ff321a09ff /home ext4              defaults,noatime      02
UUID=4209c845-f495-4c43-8a03-5363dd433153 none  swap              defaults              00

Tips and Tricks

Automatic Mounting

  • If the <span>/home</span> partition is large, you can allow services that do not depend on the <span>/home</span> partition to start first. Add the following parameters to the <span>/etc/fstab</span> file in the parameters section of the <span>/home</span> entry:
noauto,x-systemd.automount

This way, the <span>/home</span> partition will only be mounted when accessed. The kernel will cache all file operations until the <span>/home</span> partition is ready.

Note: Doing this will cause the file system type of <span>/home</span> to be recognized as <span>autofs</span>, causing mlocate queries to ignore that directory. The actual speedup effect varies by configuration, so weigh whether you need it.

  • Mounting remote file systems works similarly. If you only want to mount when needed, you can also add <span>noauto,x-systemd.automount</span> parameters. Additionally, you can set the <span>x-systemd.device-timeout=#</span> parameter to set a timeout to prevent wasting time when network resources are inaccessible.
  • If your encrypted file system requires a key, you need to add the <span>noauto</span> parameter to the corresponding location in the <span>/etc/crypttab</span> file. The systemd will not open this encrypted device at boot time and will wait until the device is accessed to use the key file for mounting. For example, when using encrypted RAID devices, this can save some time because systemd does not have to wait for the device to be available before accessing it. For example:
/etc/crypttab
data /dev/md0 /root/key noauto

Swap Partition UUID

If the swap partition does not have a UUID, you can manually add it. If the <span>lsblk -f</span> command does not list the UUID of the swap partition, it indicates this situation. Here are the steps to specify a UUID for the swap partition:

Identify the swap partition:

# swapon -s

Disable the swap partition:

# swapoff /dev/sda7

Recreate the swap partition with a new UUID:

# mkswap -U random /dev/sda7

Activate the swap partition:

# swapon /dev/sda7

Path Names with Spaces

If the mount path contains spaces, you can use the “\040” escape character to represent spaces (using three-digit octal representation).

/etc/fstab
UUID=47FA-4071/home/username/Camera0Pictures   vfat  defaults,noatime      02
/dev/sda7          /media/1000GB0(Storage)       ext4  defaults,noatime,user  00

…..</nowiki>}}

External Devices

External devices are mounted when inserted and ignored when not inserted. This requires the <span>nofail</span> option, which allows it to be ignored at startup if the device does not exist without reporting an error.

/etc/fstab
/dev/sdg1    /media/backup    jfs    defaults,nofail    02

atime Parameter

Using <span>noatime</span>, <span>nodiratime</span>, or <span>relatime</span> can improve the performance of ext2, ext3, and ext4 formatted disks. Linux uses the <span>atime</span> option by default, which generates a record every time data is read (or written) on the disk. This is designed for servers and is not very meaningful in desktop use. The main problem with the default <span>atime</span> option is that it generates disk write operations even when reading files from page cache (reading from memory instead of disk)!

Using the <span>noatime</span> option prevents write operations when reading files. Most applications work well with this. Only a few programs like Mutt need this information. Mutt users should use the <span>relatime</span> option. With the <span>relatime</span> option, write operations for file access time are only generated when the file is modified.<span>nodiratime</span> option disables file access time only for directories.<span>relatime</span> is a good compromise; programs like Mutt can still work, but it can still improve system performance by reducing access time updates.

Note: The <span>noatime</span> option already includes <span>nodiratime</span>. It is not necessary to specify both.

tmpfs

tmpfs is a temporary file system that resides in your swap partition or memory (depending on your usage). Using it can improve file access speed and ensure that these files are automatically cleared upon reboot.

Commonly used tmpfs directories include /tmp, /var/lock, and /var/run. Do not use it for /var/tmp, as temporary files in this directory need to be preserved during reboot. Using tmpfs for <span>/run</span>, <span>/var/run</span>, and <span>/var/lock</span> is for compatibility with older versions. By default, the <span>/etc/fstab</span> entry for <span>/tmp</span> is also tmpfs.

By default, the tmpfs partition is set to half of your total memory, but you can freely set this value. Note that actual memory and swap usage depends on your usage, and the tmpfs partition will not occupy storage space until it is actually used.

To place <span>/tmp</span> on tmpfs, add the following line to <span>/etc/fstab</span>:

/etc/fstab
.....
tmpfs /tmp      tmpfs nodev,nosuid                 00
.....

You can specify a size, but do not modify the <span>mode</span> option to ensure files have the correct access permissions (1777). In the example above, <span>/tmp</span> will use at most half of the memory. To specify a maximum space, use the <span>size</span> mount option:

/etc/fstab
.....
tmpfs /tmp      tmpfs nodev,nosuid,size=2G          00
.....

Here is a more advanced example showing how to add a tmpfs mount for users. This is useful for websites, MySQL temporary files, <span>~/.vim/</span>, and other situations. It is important to try and obtain the ideal mount options to achieve the goal. The goal is to adopt a secure strategy to prevent abuse. Limiting size while specifying uid and gid along with mode is very secure. More information.

/etc/fstab
tmpfs /www/cache tmpfs rw,size=1G,nr_inodes=5k,noexec,nodev,nosuid,uid=648,gid=648,mode=170000

Refer to the mount command man page for more content.

Changes will take effect after a reboot. Note not to directly execute the <span>mount -a</span> command, as it may cause files in the current directory to become inaccessible (for example, you should ensure the normal existence of lockfiles). However, if they are all empty, you can directly execute <span>mount -a</span> without rebooting the computer.

After applying changes, you can check if they are effective using <span>findmnt</span>:

$ findmnt --target /tmp
TARGETSOURCEFSTYPEOPTIONS
/tmp   tmpfs  tmpfs  rw,nosuid,nodev,relatime
Usage

Programs that require a lot of read and write operations generally see performance improvements when using tmpfs. Some programs see significant performance boosts when placing shared memory on tmpfs, such as placing the Firefox Profile folder in memory, which greatly enhances Firefox’s performance.

Note: When mounting tmpfs directories (<span>/tmp</span>), the <span>noexec</span> parameter needs to be removed; otherwise, some compilation programs cannot execute. Additionally, the default size of tmpfs is half of the memory, which may lead to insufficient space issues.

The following command can allow makepkg to edit in the tmpfs directory, or it can be set in <span>/etc/makepkg.conf</span>:

$ BUILDDIR=/tmp/makepkg makepkg

Normal User Read/Write on FAT32

To obtain write permissions on a FAT32 partition, you must modify the <span>/etc/fstab</span> file.

/etc/fstab
/dev/sdxY    /mnt/some_folder  vfat   user,rw,umask=00000

The “users” tag means any user (even non-root users) can mount or unmount the partition ‘/dev/sdX’. The “rw” tag grants read and write access. However, I do not know the meaning of the “umask” tag (umask is the permission mask command umask=000 means no one has privileges, and permissions are 777, meaning everyone can read, write, and execute). I tried to look it up in “man mount,” but there was no result.

For example, if your FAT32 partition is on ‘/dev/sda9’ and you want to mount it to ‘/mnt/fat32’, you need to enter and run:

/etc/fstab
/dev/sda9    /mnt/fat32        vfat   user,rw,umask=111,dmask=00000

Leave a Comment