What is fstab and why it’s useful?
fstab is a configuration file that contains information of all the partitions and storage devices in your linux and other Unix-like operating systems. The file is located under /etc, so the full path to this file is /etc/fstab.
/etc/fstab contains information of where your partitions and storage devices should be mounted and how.
/etc/fstab is just a plain text file, so you can open and edit it with any text editor you’re familiar with. However, note that you must have the root privileges before editing fstab. So, in order to edit the file, you must either log in as root or use the su command to become root.
Overview of the file
Of course everybody has a bit different /etc/fstab file because the partitions, devices and their properties are different on different systems. But the basic structure of fstab is always the same. Here’s an example of the contents of /etc/fstab:
/dev/sda2 / ext4 defaults 1 1 /dev/sda1 /boot ext4 defaults 1 2 /dev/sdb1 /backup ext4 defaults 0 0 /dev/sda3 swap swap pri=0,defaults 0 0
- The first column contains the device name (Volume)
- the second one its mount point
- third its file system type
- fourth the mount options
- fifth (a number) dump options
- sixth (another number) file system check options.
1st and 2nd columns: Device and default mount point
The first and second columns should be pretty straightforward. They tell the mount command exactly the same things that you tell mount when you mount stuff manually: what is the device or partition, and what is the mount point. The mount point specified for a device in /etc/fstab is its default mount point. That is the directory where the device will be mounted if you don’t specify any other mount point when mounting the device.
Some partitions and devices are also automatically mounted when your Linux system boots up. For example, have a look at the example fstab above. There are lines that look like this:
/dev/sda2 / ext4 defaults 1 1 /dev/sda1 /boot ext4 defaults 1 2
As you’ve learned, these lines mean that /dev/sda2 will be mounted to / and /dev/sda1 to /boot.
This is done automatically when your Linux system boots up… if it wouldn’t, you’d have a hard time using your cool Linux system because all the programs you use are in / and you wouldn’t be able to run them if / wasn’t mounted! But how does the system know where you want to mount /dev/sda2 and /dev/sdb1? By looking at the /etc/fstab file of course.
3rd column: Filesystem type
The third column in /etc/fstab specifies the file system type of the device or partition. Many different file systems are supported but we’ll take a look at the most common ones only.
Ext2 used to be the standard file system for Linux, but these days, Ext4 the default file systems for almost every new Linux distro. Ext4 is a newer file system type that differs from Ext2 and Ext3 in it’s journaled, meaning that if you turn the computer off without properly shutting down, you shouldn’t lose any data.
swap – The file system name is self-explanatory. The file system type “swap” is used in your swap partitions.
4th column: Mount options
The fourth column in fstab lists all the mount options for the device or partition. This is also the most confusing column in the fstab file, but knowing what some of the most common options mean, saves you from a big headache. Yes, there are many options available, but I’ll take a look at the most widely used ones only. For more information, check out the man page of mount.
auto and noauto With the auto option, the device will be mounted automatically (at bootup, just like I told you a bit earlier, or when you issue the mount -a command). auto is the default option. If you don’t want the device to be mounted automatically, use the noauto option in /etc/fstab. With noauto, the device can be mounted only explicitly.
user and nouser These are very useful options. The user option allows normal users to mount the device, whereas nouser lets only the root to mount the device. nouser is the default, which is a major cause of headache for new Linux users. If you’re not able to mount your cdrom, external usb drive, Windows partition, or something else as a normal user, add the user option into /etc/fstab.
exec and noexec – exec lets you execute binaries that are on that partition, whereas noexec doesn’t let you do that. noexec might be useful for a partition that contains binaries you don’t want to execute on your system, or that can’t even be executed on your system. This might be the case of a Windows partition.
exec is the default option, which is a good thing. Imagine what would happen if you accidentally used the noexec option with your Linux root partition…
ro Mount the file system read-only.
rw Mount the file system read-write. Again, using this option might cure the headache of many new Linux users who are tearing their hair off because they can’t write to their external drives, Windows partitions, or something else.
async and sync – async is the opposite of sync, which is rarely used. async is the default, you don’t need to specify that explicitly.
The option sync means that all changes to the according filesystem are immediately flushed to disk; the respective write operations are being waited for. For mechanical drives that means a huge slow down since the system has to move the disk heads to the right position; with sync the userland process has to wait for the operation to complete. In contrast, with async the system buffers the write operation and optimizes the actual writes; meanwhile, instead of being blocked the process in userland continues to run. (If something goes wrong, then close() returns -1 with errno = EIO.)
defaults Uses the options that are (rw, suid, dev, exec, auto, nouser, async, and relatime).
|sync||All I/O to the filesystem should be done synchronously.|
|async||All I/O to the filesystem should be done asynchronously.|
|atime||inode access time is controlled by kernel defaults.|
|noatime||Do not update inode access times on this filesystem|
|auto||Mount it when -a used (mount -a)|
|dev||Interpret character or block special devices on the filesystem|
|diratime||Update directory inode access times on this filesystem.|
|dirsync||All directory updates within the filesystem should be done synchronously.|
|exec||Permit execution of binaries|
|group||Allow normal group users to mount|
|mand||Allow mandatory locks on this filesystem.|
|relatime||Update inode access times relative to modify or change time.|
|delaylog||Affect how vxfs maintains journals which impacts performance and ability to recover the file system|
|suid||Allow set-user-identifier or set-group-identifier bits to take effect.|
|remount||Attempt to remount an already-mounted filesystem.|
|rw||Read write mode|
|ro||Read only mode|
|owner||Allow non-root user to mount if he is owner of device|
|user||Allow an ordinary user to mount the filesystem.|
|largefiles||Allow file size more than 2TB|
5th and 6th columns: Dump and fsck options
Well, dump is a backup utility and fsck is a file system check utility.
The 5th column in /etc/fstab is the dump option. Dump checks it and uses the number to decide if a file system should be backed up. If it’s zero, dump will ignore that file system. If you take a look at the example fstab, you’ll notice that the 5th column is zero in most cases.
The 6th column is a fsck option. fsck looks at the number in the 6th column to determine in which order the file systems should be checked. If it’s zero, fsck won’t check the file system.