Linux systems use a file called fstab which is read whenever the system boots to let the operating system knows which partitions to mount and which to ignore. Understanding how to edit it is key to managing a Linux computer, whether you’re running a desktop or server distribution.
Where to find it
The fstab file is stored in the /etc folder, which can be accessed in two different ways.
From the GUI
Open Nautilus or your file manager of choice and click on Computer in the left pane, then scroll down until you see the folder named etc. Double-click on it to show its content.
From the command line
cd /etc to change your current directory, then use
dir to show that folder’s content. You can also edit the file directly with the command
sudo nano /etc/fstab.
Fstab contents explained
The default appearance of a fstab file will look somewhat like this:
# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
# # / was on /dev/sda5 during installation
UUID=126d04b9-2586-409e-8495-49933af7b7d6 / ext4 errors=remount-ro 0 1
# /boot was on /dev/sda1 during installation
UUID=81838a23-a9f1-4e12-a359-fbf099300f15 /boot ext4 defaults 0 2
The lines starting with a # are comments and are automatically ignored by the operating system. This is useful because the programmers can include brief descriptions inside a configuration file, but the user can also add its own comments to clarify what a specific entry in fstab is for.
Each line in fstab contains 6 fields separated by a tabulation character. The fields are, from left to right:
- file system, or which partition the computer should attempt to mount whenever it boots. This generally refers to a file named /dev/sdXN, where X is a letter assigned in alphabetic order and N a number ranging from 1 to 16 or 128. However, it can also be a UUID, a unique identifier that the operating system uses to make sure that it’s mounting the right partition, as is the case with the example above, a label, which is a short name assigned by the user when formatting a hard drive or a partition, or even a path on another computer entirely;
- mount point. This part tells the computer where to mount the file system specified in the previous field. Suppose, for example, that my /home folder is stored in a different hard drive, say, /dev/sdb1. I could write /home to have that partition mounted there;
- type. The type of file system the partition is formatted as. You can get a list of supported filesystems using the command
cat /proc/filesystems | grep -v nodev | cut -f 2;
- options. This field is used to specify additional options. We will discuss those briefly;
- dump. This field tells the operating system if this file system can be backed up with a program named dump. The value of 0 means no, the value of 1 means yes. However, dump is almost never used anymore due to the fact that better backup utilities exist and some distributions (e.g., Ubuntu) don’t install it by default, so you can leave this value to 0 for all file systems;
- check. This field is read to decide if the file system has to be checked at boot. A value of 0 means that the file system is never checked at boot, 1 means that it should be checked before other file systems, and a value of 2 means it should be checked after file systems marked with a 1. In general, you want to give a value of 1 to the file system that contains important folders such as /root, /bin and /sbin and 2 to all other file systems.
You can specify many different options in the fourth field. A few of the most useful are:
- noauto: do not mount the file system automatically at boot or when the
mount -acommand is issued;
- acl: enable Access Control List support for the file system. This is very useful if you want to give permissions to more than one user and/or groups to the files contained therein;
- ro: mount the file system as read only;
- user: allow users to mount this file system, even if they aren’t administrators;
- nofail: do not report errors if the device is not connected or does not respond.
How to edit the /etc/fstab file
Editing the fstab file is rather easy, as long as you follow the correct order. In this regard, following the same scheme of the entries already in it can be of great help. As an example, suppose that you have just added a second internal hard drive to your Linux box and formatted it with a single partition with the file system of your choosing. The operating system recognizes the new storage device as /dev/sdb and the only partition in it has the device file name /dev/sdb1.
Making sure the mount point exists
The first step in order to ensure that mounting any storage device (regardless of if it is to be mounted at boot or manually at a later time) is to make sure that its mount point actually exists. In our example, we want the operating system to mount /dev/sdb1 in the folder named /files. This isn’t a folder that generally exists in a Linux installation, so we’ll have to create it with the command
sudo mkdir -m=777 /files
You will be asked to enter your password. Do so and you’ll end up with a folder that belongs to root and where all users have all permissions permissions.
Pro-tip: The command above is provided to give you a folder that’s usable out of the box. However, it’s generally not a good idea to have regular files belonging to root, so you may want to change the folder ownership to the user you normally employ. To do this, you can use the chmod command. The syntax required to accomplish this is
sudo chmod username:username /files
Note that you’ll have to write your username twice with a colon in the middle. This is because Linux assigns every user to its own group named after the user by default.
You can edit the contents of /etc/fstab in two different ways, both of them requiring some command-line shenaningans, so you’ll need to open your Terminal.
Note: In actuality, it is possible to edit the file using a graphical text editor, but since every Linux desktop environment comes with its own editor, it’s impossible to write a guide that can possibly encompass all of them, so I decided to forgo editing in a GUI completely.
However, before we even think about editing anything on a computer running Linux, it’s safer to create a copy of the original file, just in case something goes wrong and we want to revert to a previous working state. To do this, use the following command:
sudo cp /etc/fstab /etc/fstab.old
This will create a copy of the original fstab file named fstab.old.
Editing with nano
Nano is a command-line text editor. It’s generally included in most, if not all, Linux distributions and is simple to use compared to other text editors such as vim. To open nano a file in nano, you’ll need to write nano followed by the file to open and then press Enter. However, since only root can open system files, you’ll have to use sudo.
sudo nano /etc/fstab
will open your fstab. From here, you can use the arrow keys to navigate inside the file, or press the End key to go at the end of the file. Add a new line to write a new entry.
If we want to mount our partition at boot in a folder named, say, /files, this is what we would start by writing the name of the device file of the partition, thus we have
Then press Tab to add a tabulation inside the file, and proceed to specify the mount point
We also need to specify the file system we formatted the partition as. In our case, it’s ext4, so we’ll add that to our line, thus having
/dev/sdb1 /files ext4
We also need to specify some options. If you’re going to use your new hard drive as regular storage, you can simply write defaults in this field
/dev/sdb1 /files ext4 defaults
Lastly, we have the dump and check field. Since dump is an obsolete application and this drive isn’t going to store operating system files, we can give them the values 0 and 2, respectively.
Our line will look like this:
/dev/sdb1 /files ext4 defaults 0 2
Now that we’re done editing, we can close the editor. To do this, press Ctrl + X. At the bottom of the terminal window you’ll see that the program is asking you if you want to save your changes. Press Y, then Enter to confirm. Press Enter again to confirm where to save the file.
Appending an entry without a text editor
You can also forgo a text editor entirely using a feature of the Linux command line called command redirection. To add a new line to /etc/fstab, you only need to enter a root shell by using
sudo -i. Type your password and press Enter. Now use the command
echo /dev/sdb1 /files ext4 defaults 0 2 >> /etc/fstab
Note: Take extra care in making sure that you’re using two greater than signs and not one. Two signs mean “append the previous text to the specified file”, whereas a single one means “replace the content of the file with the previous text”. You wouldn’t want to overwrite your /etc/fstab by accident!
Testing the new configuration
Okay, now we have edited the file, but the operating system hasn’t yet mounted our new partition. There are two ways we can make it do that. The first is simply rebooting. However, I suggest against this practice, because if there is an error in the fstab file, the operating system may have trouble booting. The safest approach is using the command partprobe. Note that this requires administrator privileges, so you’ll need to run it with sudo.
The terminal will hang for a couple seconds, attempting to mount all the partitions specified inside /etc/fstab. Now you can enjoy your new partition.