Mounting at Boot with /etc/fstab

Mounts don't survive a reboot

A mount command mounts a disk until the machine restarts; then it's gone. To mount a filesystem automatically at every boot, you list it in /etc/fstab (the filesystem table). This machine mounts its root over the network before /etc/fstab is even read, so the file is still empty — we'll add the first entry:

cat /etc/fstab
findmnt /

In the terminal

student@vulntech:~$ cat /etc/fstab
# UNCONFIGURED FSTAB FOR BASE SYSTEM
student@vulntech:~$ findmnt /
TARGET SOURCE FSTYPE OPTIONS
/      host9p 9p     rw,relatime,dirsync,loose,access=client,trans=virtio

The six fields

Each line in /etc/fstab describes one filesystem, in six whitespace-separated fields:

FieldMeaning
deviceWhat to mount — best given as UUID=...
mount pointThe directory to mount it on, e.g. /mnt/store
typeThe filesystem type, e.g. ext4
optionsMount options, e.g. defaults,nofail
dumpBackup flag (almost always 0)
passfsck order at boot (0 to skip, 2 for data disks)

Why UUID= and not /dev/sdb1? Because device names can change when disks are added or reordered, but a filesystem's UUID never does. Read it with blkid, then add the line. nofail lets the machine boot even if the disk is missing:

sudo losetup -fP --show data.img
UUID=$(sudo blkid -s UUID -o value /dev/loop0)
echo $UUID
echo "UUID=$UUID  /mnt/store  ext4  defaults,nofail  0 2" | sudo tee -a /etc/fstab
sudo mkdir -p /mnt/store
sudo mount -a
findmnt /mnt/store

In the terminal

student@vulntech:~$ sudo losetup -fP --show data.img
[sudo] password for student:
/dev/loop0
student@vulntech:~$ UUID=$(sudo blkid -s UUID -o value /dev/loop0)
student@vulntech:~$ echo $UUID
df71eec3-5c5b-413b-9fce-de92c4eea1ee
student@vulntech:~$ echo "UUID=$UUID  /mnt/store  ext4  defaults,nofail  0 2" | sudo tee -a /etc/fstab
UUID=df71eec3-5c5b-413b-9fce-de92c4eea1ee  /mnt/store  ext4  defaults,nofail  0 2
student@vulntech:~$ sudo mkdir -p /mnt/store
student@vulntech:~$ sudo mount -a
mount: (hint) your fstab has been modified, but systemd still uses
       the old version; use 'systemctl daemon-reload' to reload.
student@vulntech:~$ findmnt /mnt/store
TARGET     SOURCE     FSTYPE OPTIONS
/mnt/store /dev/loop0 ext4   rw,relatime

mount -a mounts everything in /etc/fstab that isn't mounted yet — so it applies the new line immediately, and it's also how the system mounts these disks at boot.

Common mistake: A wrong line in /etc/fstab can stop the machine booting. Always add nofail for removable or data disks, and test a new entry with sudo mount -a before rebooting — if it errors, fix it now.

Tip: findmnt --verify checks /etc/fstab for mistakes without mounting anything. Mounting by LABEL= works too, if you gave the filesystem a label with mkfs -L.

Remember: /etc/fstab lists filesystems to mount at boot, one per line, six fields: device (use UUID=), mount point, type, options, dump, pass. sudo mount -a applies it now; nofail keeps a missing disk from blocking boot.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…