How to Write an fstab File on Linux

The trays inside of several hard drives.zentilia /

Add a new hard drive or SSD to your Linux computer? You will need to edit your fstab file. Many people find the idea even scary. Yes, it is essential to do things right, but with the right knowledge, it's really not difficult. We accompany you through the process of modifying your fstab file to integrate your new drive into your file system.

fstab, the table of filesystems

Although adding a new hard drive to a Linux computer is not too complicated, it can be a bit confusing the first time you try. You connect the hardware, turn on the computer, and log on to the operating system. But you can not see your new disk anywhere. Why does not he show up? How do you get Linux to "see" the drive so you can start configuring it?

In fact, Linux has seen your hardware, but it does not advertise easily. Or even give you a hint that he has found your new material. You must query Linux to get the information you will need to put in your fstab file.

Here's how to configure your new hard drive so that Linux – and you – can see it and use it. The process has two parts. The first part is being recognized to identify the hard drive and gather information about it. The second part is to edit the fstab file, using the information we gathered during the recognition phase.

Find your new player

We are adding two new readers to this system. One is a mechanical hard drive (HD) of 32 GB, and the other is a 16 GB hard drive. solid state drive (SSD).

We need to know that Linux can see them and which block devices Linux uses for them. In Linux and Unix operating systems, a block device is a special file which acts as an interface to a device from which data can be read and written (unless they are read-only). Block devices often represent a mass storage device (for example, a partition on a hard drive or CD-ROM). They are created in the / dev directory.

We can use the lsblk command for list block devices connected to your Linux computer.


lsblk in a terminal window

The output of lsblk is in columns.

Output of lsblk in a terminal window

The columns are:

Last name: This is the name of the device. Device names beginning with "sd" and followed by a letter represent SCSI hard disks. The letter identifies individual hard drives, where "a" is the first, "b". to be the second and so on. If a number is added, it indicates a partition. For example, "sdb2" is partition 2 on the second SCSI hard disk.
Maj: Min: This column contains the major and minor numbers of the device. The major number indicates the type of device (or, more specifically, the type of driver used to communicate with that device). The minor number is a count of the number of devices of this type.
rm: This column indicates whether the device is removable or not. Note that the sr0 device is set to 1, indicating that it is removable. This is a CD-ROM drive.
Cut: This is the amount of data that can be stored in the device.
ro: This column will display 1 for read-only devices and 0 for read-write devices. Loop devices are all readonly.
Type: This identifies the type of device. The entry "disk" denotes a disk drive, the entry "part" indicates the partition and "rom" means Read-only memory (CD ROM).
Mounting point: This shows the point in the file system on which this device is mounted. If it's empty, the device is not mounted.

In the screen capture above, you can see that looped devices all receive a major number of 7 (which means a loopback device or loop), and the minor numbers simply increment by 1 each time. loopback devices are used with the squashfs file system. A squashfs file system is created each time an application is installed using the file bright package management system.

SCSI hard drives have names such as sda, sdb, and sdc, and all have a major number of 8 (SCSI hard disk). Minor numbers are grouped by 16. The minor numbers of the first reader, nda, range from 0 to 15. The 0 represents the physical drive and the minor number of 1 represents the first partition of this drive. For the second reader, sdb, the minor numbers range from 16 to 31. 16 represents the physical drive and 17, the first partition of this drive. The next 16 numbers, 32 to 47, are used for minor sdc numbers, and so on.

The other major numbers common are 3 (for a IDE Hard Disk) and 11 for CD-ROMs.

In fact, the / dev / sr0 style for SDCSI CD-ROM drives is obsolete. The approved format is / dev / scd0. Despite this, the format / dev / sr0 was still used on all machines used for searching in this article.

The kernel documentation contains a long list of all values that major and minor numbers can take. It's a surprisingly long list.

To unclutter the output of lsblk, we can use grep to select only the points of interest ours. We know we have not added a loop device. We will therefore select all SCSI hard drives. we know that these will have "sd" in their names.

lsblk | grep sd

lsblk | grep sd in a terminal window

With this command, grep will only print lines containing "sd" in the directory. On our test machine, we see:

Release of lsblk | grep sd in a terminal window

So we have three SCSI drives. The first, / dev / sda, is mounted at the root of the file system, /. The other two are not mounted at all, which is predictable for the newest readers. We can see that the drive / dev / sdb has a size of 32 GB, which means that it is our traditional mechanical drive. Drive / dev / sdc has a size of 16 GB. This is our SSD drive.

In fact, because it's a virtual machine, it's also virtual disks. Thus, the SSD is presented as a mechanical SCSI drive. On my usual desk my NVMe SSD in the form / dev / nvme0n1, and the first partition is / dev / nvme0n1p1. Its major number is 259. These differences do not change what we have to do in the fstab file, but be aware that if you have an SSD, it will not be presented as a physical disk.

In addition, your disks will probably not have a partition if they are new. You can use fdisk to create a partition if necessary.

RELATED, RELATED, RELATED: How to use Fdisk to manage partitions on Linux

Identification of rotating and non-rotating discs

If we use the -o (output) option with lsblk and add the ROTA (rotary) column to the screen, lsblk will use a 1 to indicate a rotating storage device (mechanical drive) and a 0 to indicate a non-rotating storage device. (Hard disk).

lsblk -o + ROTA | grep sd

lsblk -o + ROTA | grep sd in a terminal window

We have an extra column to the right of the screen, which is the ROTA column (rotating). As you can see, the "SSD" has a 0 for the device and partition. This makes sense because an SSD is a non-rotating storage device.

release of lsblk -o + ROTA | grep sd in a terminal window

Mount file systems

Before we start thinking about the fstab file, let's check that we can mount the drives by hand. So, if something does not work when we use the fstab file, we will know that the problem must be our syntax and not a problem with the drive itself.

We will create temporary mount points in the / mnt directory. You will have to use sudo and you will be prompted for your password.

sudo mkdir / mnt / scsi

sudo mkdir / mnt / scsi in a terminal window

sudo mkdir / mnt / ssd

sudo mkdir / mnt / ssd in a terminal window

Now let's move the SCSI drive to the new mount point. We will use the mount command in its simplest form. We will tell him the name of the partition we want to mount and the mount point on which we want to install it. mount will mount the file system on this partition at the mount point we specify.

We specify the partition that contains the file system, not the drive, so be sure to include the partition number, in this case, "1".

sudo mount / dev / sdb1 / mnt / scsi

I do not drink at all in a terminal window

If all goes well, mount will not respond. You are silently returned to the command prompt.

Mounting the SSD is just as easy. We tell which partition to mount on which device, and which mount point to mount on.

sudo mount / dev / sdc1 / mnt / ssd

sudo mount / dev / sdc1 / mnt / ssd in a terminal window

Once again, the silence is golden.

RELATED, RELATED, RELATED: How to mount and unmount storage devices from the Linux terminal

Checking the frames

To verify that the editing has taken place, we will use lsblk again. We will route its output via grep and select the entries "sda1", "sdb2" and "sdc1".

lsblk -o + ROTA | grep sd[a-c]1

lsblk -o + ROTA | grep sd[a-c]1 in a terminal window

mount shows us the three mounted partitions. This is the two we just edited and the original score mounted on /.

mount output in a terminal window

The / dev / sdb1 partition is mounted on / mnt / scsi and on a spinning storage device. The / dev / sdc1 partition is mounted on / mnt / ssd and is on a non-rotating storage device. Everything looks good.

We now need to configure the fstab file for these devices to be mounted each time the computer starts up.

The fstab file

The fstab file contains an entry for each file system mounted when you restart your computer. Each entry is made up of six fields. The fields are:

File system: Not, as the name suggests, the type of file system of the partition (this is the role of the type field). This is the identifier of the partition to mount.
Mounting point: The location in the file system where you want the partition to be mounted.
Type: The type of file system on the partition.
The options: Each file system may have options specified to enable or disable features.
dump: Reference to an almost obsolete means of backing up file systems, in which the entire file system was "dumped" on tape.
Pass: This is the "passing" flag. It tells Linux which partitions should be checked using fsck, and in which order. Your boot partition and your operating system should be 1 and the rest can be set to 2. If the indicator is set to zero, it means "do not check at all". If your file system is not a journaled file system (such as ext2 or FAT16 / 32, for example), it is best to disable this option by setting it to 0.

These fields must be specified in this order and must be separated by a space or tab. Finding values ​​for these fields can be daunting, especially for the "options" field. Options in the "options" field must be in a comma-separated list, with no spaces.

The man page for each file system lists the options that can be used. ext4 a about 40 options. Here are some of the most common options:

Auto: The file system will be mounted automatically at startup.
noauto: The file system is mounted only when you enter the mount -a command.
Exec: The execution of binaries is allowed on this file system.
noexec: The execution of binaries is not allowed on this file system.
ro: The file system must be mounted read-only.
rw: The file system must be mounted read-write.
Sync: File writes must be done immediately and not buffered. It is better to reserve the floppies if someone still uses them. Incurs a performance penalty.
async: File writes need to be buffered and optimized.
User: Any user is allowed to mount the file system.
nouser: The root user is the only user who can mount this file system.
Defaults: This is an abbreviated way of specifying a set of common parameters: rw, suid, dev, exec, auto, user and async).
suid: Allows the operation of suid and sgid bits. The suid bit allows a normal user to execute a file as root. without giving the user full root privileges. When the sgid bit is set to a directory, the files and directories created in this directory have group ownership. set to that of repertoire, not the group of the user who created them.
nosuid: Do not allow use of suid and sgid bits.
noatime: – Do not update file access times on the file system. This can improve performance on old hardware.
nodiratime: Do not update the access times to the directory on the file system.
relatime: Update the access times to the file in relation to the file modification time.

The "default" option is a good gambit opening. You can add or remove additional options if a fine tuning is needed. If only there was a clever way to get the parameters you need, you must enter them in the order in the fstab file.

Enter the mtab file.

The mtab file

The mtab file is the list of currently mounted file systems. This contrasts with the fstab file that lists the file systems to mount at startup. The mtab file includes manually mounted file systems. We have already mounted our new disks, so they should appear in the mtab file.

We can see the contents of the mtab file using cat. We will limit the output by passing it through grep and looking only at / dev / sdb1 and / dev / sdc1.

cat / etc / mtab | grep sd[b-c]1

cat / etc / mtab | grep sd[b-c]1 in a terminal window

The output displays the mtab entries for these two partitions.

Output of cat / etc / mtab in a terminal window

We could lift these values ​​and drop them directly into the fstab file, making sure that there was a space or tab between each field. And that would be it. The drives would be mounted when we restarted.

There are two caveats to this. The one is the mounting point. We created temporary mount points to prove that we could mount the new partitions on the new disks. We would not need to enter real editing points instead of temporary points, if they were different.

The second caveat is that if we use the mtab file settings, we will use the block device file as the identifier for each partition. It would work, but the values ​​/ dev / sda and / dev / sdb, and so on. may change if new mass storage hardware is added to the computer. This would mean that the parameters of the fstab file would be incorrect.

Each partition has a Universal Unique Identifier (UUID), which we can use to identify the partition. It will never change. If we use the UUID to identify the partitions in the fstab file, the parameters will always remain accurate and true.

If you use your new partitions as part of a Redundant network of inexpensive disks (RAID), see the documentation for this system. This may indicate that you must use the bulk device identifier instead of the UUID.

Finding the UUID of the Partition

To find the UUID of a partition, we can use blkid for print device attributes in block mode. We will limit the output to our two new partitions on our new disks:

blkid | grep sd[b-c]1

blkid | grep sd[b-c]1 in a terminal window

The output includes the UUID for each partition.

use blkid to get the UUID of a partition

PARTUUID is a form of UUID that can be used with the GUID partition tables Partitioning method (GPT) (if you do not use the Master boot record Partitioning method (MBR)).

Edit the fstab file

Open the fstab file in an editor. were using gedit, an easy-to-use editor found in most Linux distributions.

sudo gedit / etc / fstab

sudo gedit / etc / fstab in a terminal window

The editor appears with your fstab file loaded.

the fstab file before editing

This fstab file already contains two entries. They are the existing hard disk partition / dev / sda1 and the exchange filesystem. Be careful not to modify these entries.

We need to add two new entries to the fstab file. One for the SCSI drive partition and one for the SSD drive partition. We will add the SCSI partition first. Note that lines starting with a hash # are comments.

In the "File System" field, we will use the UUID that blkid retrieved earlier. Start the line with "UUID =", then paste the UUID. Press space or tab.
For the "mount point" field, we will use the mount point created previously, / mnt / scsi. You will use the appropriate mount point of your system. Press space or tab.
For "type", we will enter ext4, which is the type of file system on our partition. Press space or tab.
In the "options" field, we will use the options retrieved using cat / etc / mtab. These are "rw, relatime". Press space or tab.
The field "dump" is set to zero. Press space or tab.
The "pass" field is set to zero.

We will now add the fstab input partition to the SSD on a separate line.

In the "file system" field, we will enter the UUID retrieved by blkid for the partition on the SSD. Start the line with "UUID =", then paste the UUID. Press space or tab.
For the "mount point" field, we will use the mount point created previously, / mnt / ssd. Press space or tab.
For "type", we will enter ext4, which is the type of file system on our partition. Press space or tab.
In the "options" field (so that the two new entries are different in our example), we will use the "default" option. Press space or tab.
The field "dump" is set to zero. Press space or tab.
The "pass" field is set to zero.

Fstab file after editing and adding SCSI and SSD drives

Save the file and close the editor.

RELATED, RELATED, RELATED: How to graphically edit text files in Linux with gedit

Fstab test without reboot

We can dismount our new disks, then force an update of the fstab file. The successful editing of our new partitions will verify that the parameters and parameters we have entered are syntactically correct. This means that our fstab file must be processed correctly during a restart or power-up sequence.

To unmount the SCSI drive, use this command. Note that there is only one "n" in "umount":

sudo umount / dev / sdb1

sudo umount / dev / sdb1 in a terminal window

To unmount the SSD, use this command:

sudo umount / dev / sdc1

sudo umount / dev / sdc1 in a terminal window

We will now use lsblk to check if these block devices are mounted.

lsblk | grep sd

And we see that block devices are present in the computer, but are not mounted anywhere.

Lsblk output showing unmounted block devices in a terminal window

We can use the mount command with the -a (all) option to trace all the file systems in fstab.

sudo mount -a

sudo mount -a in a terminal window

And we can check one more time with lsblk to see if our new partitions are now mounted:

lsblk | grep sd

Release of lsblk | grep sd showing the peripheral blocks mounted in a terminal window

Everything is mounted where it should be. All that remains is to change the ownership of the mount points, otherwise root will be the only one able to access the new storage devices.

We can do it easily using chown. Here is the command for the SCSI mount point:

sudo chown dave: users / mnt / scsi

sudo chown dave: users / mnt / scsi in a terminal window

And here is the command for the SSD mount point:

sudo chown dave: users / mnt / ssd

sudo chown dave: users / mnt / ssd in a terminal window

We can now restart our computer with confidence, knowing that the partitions we added will be mounted for us, and we have access to them.

Not so scary after all

All the work is in the recognition phase – and it was not difficult either. Editing the fstab file once you have collected the information you need is a child's game. Preparation is everything.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.