Linux › Section D · Disks and filesystems › Topic D.3

Mounting filesystems and /etc/fstab

A new filesystem is invisible until it is mounted onto a directory in the single Linux tree. Mounting by hand, choosing options and writing a reliable /etc/fstab line are everyday administration skills, and a careless fstab line can stop a machine from booting normally.

Attaching a filesystem to the tree

Linux has one directory tree starting at /. Mounting attaches a filesystem to a directory in that tree, called the mount point, and from then on the filesystem's contents appear there. To mount you need three things: where the filesystem is (a device such as /dev/sdX2, or an identifier), its type (usually detected automatically) and the mount point, which must be an existing directory. Anything already inside that directory is hidden, not deleted, until you unmount. Most systems keep an empty /mnt for quick experiments. Removable media are usually mounted for you under /media or /run/media by the desktop.

mount, umount and findmnt

As root, mount /dev/sdX2 /data attaches the filesystem; add -t ext4 only when detection needs help, for example to choose between FAT variants. umount /data detaches it (note the spelling: umount, with no n). If umount says the target is busy, some process has a file open there or a shell has it as its current directory; lsof /data or fuser -vm /data shows who. To see what is mounted, use findmnt, which prints a tree of mount points, sources, types and options; findmnt /data shows one entry. Plain mount with no arguments still lists mounts but is kept only for backward compatibility.

Identify by UUID, not by name

Device names come from the order in which the kernel detects disks, so the disk that was /dev/sdb today may be /dev/sdc after you add a drive. Mounting the wrong one could hide or damage data. Instead, refer to the filesystem's own identifier, created by mkfs. blkid lists each one: UUID (the filesystem's universally unique identifier), LABEL if you set one, and on GPT disks PARTUUID for the partition itself. mount UUID=... /data and mount LABEL=data /data both work. FAT filesystems use a short volume serial number such as 4A1B-9C2D in place of a full UUID. Logical volumes have stable names of their own, as the LVM topic shows.

Mount options

Options go after -o, separated by commas. ro and rw choose read-only or read-write. noexec forbids running programs from the filesystem, nosuid ignores set-user-ID and set-group-ID bits, and nodev ignores device files: a sensible trio for removable or shared storage. relatime, the kernel default since Linux 2.6.30, updates a file's access time only occasionally, and noatime stops those updates altogether. To change options without unmounting, use remount: mount -o remount,ro /data makes the filesystem read-only in place. Some options belong to one filesystem type, such as uid=1000 on vfat, which makes every file appear to belong to user 1000.

Caching, sync and safe removal

The kernel does not write every change to disk at once. It holds pending writes in memory and flushes them when convenient, which makes the system much faster. It also caches recently read blocks, so a file read twice comes from RAM the second time. The cost is that, at any moment, the disk may be behind. Unmounting flushes everything for that filesystem, and the sync command flushes all filesystems. That is why you should unmount a USB drive, or use the desktop's eject button, before pulling it out: otherwise files you thought were saved can be incomplete or the filesystem left inconsistent.

The six fields of /etc/fstab

/etc/fstab lists filesystems to mount at boot, one per line, with six fields. 1. What to mount: UUID=..., LABEL=..., a device path or a network share. 2. Mount point, or none for swap. 3. Type: ext4, xfs, vfat, swap and so on. 4. Options, at least defaults, which means rw, suid, dev, exec, auto, nouser and async. 5. The dump field, used by the obsolete dump backup program; set it to 0. 6. The fsck pass: 1 for the root filesystem, 2 for other local disk filesystems, 0 for no check. Extra options for fstab include noauto (skip at boot and with mount -a), nofail (do not report an error if the device is missing) and user (let ordinary users mount it).

Worked example: a permanent mount

Task: mount the ext4 filesystem labelled data at /data on every boot. 1. Create the mount point: mkdir /data. 2. Find the identifier: blkid /dev/sdX2 prints its UUID; copy it exactly. 3. Back up the file with cp /etc/fstab /etc/fstab.bak, then add one line: UUID=<the uuid> /data ext4 defaults,nofail 0 2. The nofail option keeps the boot going if this second disk is ever absent. 4. Check the file: findmnt --verify reports syntax errors and unknown devices. 5. On systemd systems run systemctl daemon-reload, then mount /data, which reads the rest from fstab. 6. Confirm with findmnt /data. Testing now is far better than discovering a typo at the next boot.

fstab and systemd

On systemd distributions, fstab is not read by a script at boot. A generator, systemd-fstab-generator, converts each line into a mount unit, and systemd mounts them in dependency order alongside services (see "systemd units and unit files"). That is why systemctl daemon-reload is needed after editing the file. You can also write .mount units directly, though fstab remains the usual place. If a filesystem needed for boot cannot be mounted, for instance because of a mistyped UUID without nofail, systemd stops and offers an emergency shell; the topic "Emergency booting and single-user mode" explains how to recover.

Worked example: where did the space go?

df -h /data shows (illustrative) Size 100G, Used 20G, Avail 75G, Use% 22%. 1. Add used and available: 20 + 75 = 95G, so 5G is unaccounted for. 2. 5G is 5% of 100G, the space ext4 reserves for root by default, so the system can keep logging and services running when users fill the disk. 3. Use% is used ÷ (used + available) = 20 ÷ 95, about 21%, which df rounds up to 22%. 4. On a data-only disk you might lower the reserve with tune2fs -m 1 /dev/sdX2. Two related commands: du -sh /data/* shows which directories use the space, and df -i shows inode use, which can run out even while space remains.

Common mistakes to avoid

First, using /dev/sdb1 in fstab instead of a UUID or label: names can change. Second, a typo in fstab tested only by rebooting; check with findmnt --verify and mount the entry first. Third, forgetting nofail on removable or secondary disks, so a missing drive halts the boot. Fourth, writing swap as the mount point instead of none, or giving swap an fsck pass other than 0. Fifth, unplugging a drive without unmounting it. Sixth, mounting over a non-empty directory and thinking its files were deleted: unmount and they return. Seventh, typing unmount instead of umount.

Key takeaways

Mounting attaches a filesystem to a directory in the single tree; umount detaches it, and a busy error means a process still uses it. findmnt lists mounts. Identify filesystems by UUID or LABEL from blkid, because device names can change. Options such as ro, noexec, nosuid, nodev and noatime change behaviour, and remount changes them in place. The kernel caches writes, so unmount or sync before removal. Each fstab line has six fields: source, mount point, type, options, dump (0) and fsck pass (1, 2 or 0). Verify with findmnt --verify and test before rebooting.

Flashcards

Flashcard

Mount point

The existing directory where a filesystem is attached

Its old contents are hidden, not deleted.

Test your understanding

Question 1 of 5

Why should an /etc/fstab line use UUID= rather than /dev/sdc1?

Question 2 of 5

umount /media/usb fails with 'target is busy'. What is the most likely cause?

Question 3 of 5

Which fstab line correctly adds a swap partition?

Question 4 of 5

df -h shows Size 200G, Used 50G and Avail 140G for an ext4 filesystem. What explains the missing 10G?

Question 5 of 5

You have added a line to /etc/fstab for a new disk. What is the safest next step?

← Previous topicNext topic →