Linux Crash Course – Automatically mounting storage volumes with /etc/fstab

In a previous video we went over the basics of storage, and in this episode of Linux Essentials, I’ll show you how to automatically mount storage volumes when you boot your server. The primary focus will be going over the /etc/fstab file.

YouTube player

Before we can work with storage, we need to know which storage volumes we have attached to the server, so ew can get the device name of the device we wish to work with. We can do that with the lsblk command:

lsblk

Next, we need to decide which directory to mount the storage volume to. Technically, we can mount it anywhere. However, there’s two directories that are used most often for this purpose:

/media – For temporary devices, such as flash drives, external hard drives, etc
/mnt – Used for more permanent devices, such as internal disks and virtual disks

The /etc/fstab file is the file that we’ll edit in order to set up the automatic mounting of storage volumes. Be careful when editing this file, though.

sudo nano /etc/fstab

Each uncommented line in that file represents a mount. There should be a number of lines there by default, for the volumes that were set up while you installed Linux. We can add another line to the end of the file, to set up another mount.

Add your new storage device to the end, for example:

/dev/sdb1 /mnt/mydisk ext4 defaults, 0 0

Note: Be sure to change /dev/sdb1 to whatever your storage volume device name actually is.

There are several “fields” that are separated by spaces or tabs. The first field is the device name, the second is the the target directory to mount the storage volume to, the third represents the filesystem that was used for the volume (ext4 is common) and then we have defaults which gives us a handful of typical options in one shot. At the end of the line, the 0 represents “dump” (which pertains to backing up the filesystem) but it’s not commonly used anymore. The last number refers to the order in which the filesystem is checked for errors. 0 means it will never be checked, a value of 1 is first priority for integrity checking, while 2 is the last priority.

We should create the directory that’s referenced in the /etc/fstab file:

sudo mkdir /mnt/mydisk

We can now mount the added storage volume, and we only need to refer to the target directory:

sudo mount /mnt/mydisk

To unmount the volume:

sudo umount /mnt/mydisk

To test the /etc/fstab file, we can run the following command to mount everything that hasn’t already been mounted:

sudo mount -a

Note: If a mount has the noauto option set, the sudo mount -a command will not mount it.

In the example line above for the /etc/fstab file, it’s actually a bad idea to use a device name such as /dev/sdb1. We should reference storage volumes by their UUID (Universally Unique Identifier) instead.

To find a volume’s UUID, we can enter the following:

sudo blkid

Note: sudo isn’t always required, but you may not see all the info without it.

The UUID is preferred over the device name, because the device name may not be the same every boot. This means that /dev/sdb1 could be /dev/sdc1 when the machine is rebooted, those device names are not static. But the UUID doesn’t actually change, so it’s preferred to use that instead.

Here’s an example line in /etc/fstab with a reference to its UUID rather than its device name:

UUID=38bee… /mnt/mydisk ext4 defaults 0 0

Be sure to test the file again:

sudo mount -a

Another option to consider adding to your mount in the /etc/fstab file is the noauto option. This option overrides the auto option that’s included in defaults, and will ensure that the mount is not automatically mounted at boot time. This allows you to reference a mount in /etc/fstab, but mount it manually in case it’s a device that comes and goes, but you still want it to mount to the same directory each time.

Check out the Shop!

Support Linux Learning and get yourself some cool Linux swag!


Support LearnLinuxTV and receive 5% off an LPI exam voucher!