Permanently mount a drive with fstab

If you want to use multiple disks in your linux-system you need a way to automaticly mount then at boot-time. The way to do this is to use the config-file /etc/fstab.

Difference between mount and /etc/fstab

If you did some research you probably read that you can mount a drive with the mount-command. The drawback is that the mount command is not permanent. If you reboot you system the drive will not get mounted again. Therefor you need to use the /etc/fstab.
In case you didn’t know it already the mount command looks like this.

# mount the drive sda to /mnt
mount /dev/sda /mnt

Use the /etc/fstab

First of all you need to open the file with you favorite text-editor.

vim /etc/fstab

And it will look something like this.

/dev/mapper/almalinux-root /                            xfs     defaults        0 0
UUID=8ccfd94c-81c4-48ba-b53e-7787064bc64f /boot         xfs     defaults        0 0
/dev/mapper/almalinux-swap none                         swap    defaults        0 0

The file is structured like this.

<drive-name or uuid> <mount-point> <filesystemtype> <options> <dump> <pass>

In this example there are 3 partitions mounted.
In line one the root-partition gets mounted. It uses the device-mapper to mount the drive. That way the device-mapper keeps track of your drives and their UUIDs and you can use a readable name.
Line 2 uses the UUID to mount the boot-partition. You can’t mount the boot partition with the device-mapper, since the boot-partition needs to be read before the device-mapper can be executed.
The last line is the swap-partition. Notice that swap does not have a mount-point. It is sufficient to specify the filesystem a swap.

On modern systems you usually can set pass and dump to zero.

Mount the drives

Now that you made your entry in the /etc/fstab nothing happens. You need to tell your system to mount the drives like you specified. You can use the mount-command for that.

mount -a

Be aware that this command will not unmount drives that are not specified.
So if you want to replace a mount you should unmount it first. Otherwise you will mount the new drive into the old and can’t unmount the old one.

umount /mnt

# if drive is busy, use lazy unmount
unmount -l /mnt

UUID vs Device-Name

One big question in regards to the fstab is wether to use the device-name (like /dev/sdc) or the UUID (like 8ccf….). Advantage of the device-name is it’s readablity.
The device-name is deteremend by the order the system picks up the drives on boot. There is no way for you to influence it! So you might run into problems that your system once to mount the drives on wrong mount-points if you use only the drive-name.
That is especially critical for raid-systems.
So if you don’t want your system to be stuck at boot, use the uuid or the device-mapper.

Get the UUID of a drive

To read the UUID of a drive use the blkid command.

[tux@server]$ sudo blkid
/dev/sda1: UUID="8ccfd94c-81c4-48ba-b53e-7787064bc64f"   TYPE="xfs"         PARTUUID="5fc71689-01"
/dev/sda2: UUID="jFZ291-lCX2-Efsu-Yely-335c-Yuxf-2QCUZY" TYPE="LVM2_member" PARTUUID="5fc71689-02"
/dev/sdb:  UUID="yGTsOE-xlJb-afE2-OEGB-3NEp-9RC6-sEy3a6" TYPE="LVM2_member"
/dev/sdc:  UUID="K0RD4T-5IrS-rfwq-0E9o-ADnD-2F17-JTyfVD" TYPE="LVM2_member"

Mount Network shares

You can mount NFS and other shares just like drives. Just replace the drivename with the share name and set the filesystem to NFS.

vim /etc/fstab
nfs-server:/share/data    /mnt/nfs-share   nfs   defaults  0 0
profile picture of the author

Jannik Rehkemper

I'm an professional Linux Administrator and Hobby Programmer. My training as an IT-Professional started in 2019 and ended in 2022. Since 2023 I'm working as an Linux Administrator.