Mounting, /etc/fstab & swap
Mount file systems and swap persistently and safely by UUID.
By Invicly Tech, , in Linux Administration (RHCSA-style). About 10 minutes to read.
In short
Mounting attaches a file system to a directory. Manual mounts vanish at reboot, so persistent mounts go in /etc/fstab, preferably by UUID because device names can change. Swap space is added the same way and gives the kernel somewhere to move idle memory pages.
Explained simply
On Linux, a disk is not a separate drive letter. You attach it to a folder, and from then on that folder shows what is on the disk. This attaching is called mounting, and the folder is the mount point.
A mount you type by hand is forgotten at the next reboot. To make it permanent, you add one line to /etc/fstab, the file systems table that Linux reads at every boot. One wrong line there can stop the server from booting normally, so you always test it before rebooting.
Swap is disk space the kernel uses as overflow when RAM is full. It is much slower than RAM, so it is a safety net, not extra memory. It lets the kernel move rarely used pages out of the way and stops some programs from being killed when memory runs low.
These tasks show up everywhere: adding a data disk to a database server, mounting a share for backups, or adding a swap file to a small cloud VM that keeps running out of memory.
Press Play on a diagram to walk through it one step at a time.
- 1Identify
Get the UUID with blkid or lsblk -f.
- 2Create the mount point
mkdir -p /data.
- 3Add to fstab
UUID=... /data xfs defaults 0 0.
- 4Test
systemctl daemon-reload, then mount -a.
- 5Verify
findmnt /data, then reboot to be sure.
- Admin to Kernel: umount /data1umount /data
- Kernel to Admin: target is busy2target is busy
- Admin to Kernel: fuser -vm /data3fuser -vm /data
- Kernel to Admin: PID 2311 (app) has files open4PID 2311 (app) has files open
- Admin to app process: systemctl stop app5systemctl stop app
- Admin to Kernel: umount /data succeeds6umount /data succeeds
In depth
The six fields of an fstab line
Each fstab line has six fields separated by spaces. 1: what to mount, best written as UUID=... or LABEL=... rather than /dev/vdb1. 2: where, the mount point (none for swap). 3: the type, such as xfs, ext4, swap or nfs. 4: options, comma-separated with no spaces, or defaults. 5: dump, an old backup flag that is almost always 0. 6: fsck order: 1 for the root file system, 2 for other ext4 file systems, 0 to skip; xfs and swap use 0.
Common options: ro for read-only, noatime to skip access-time updates, nosuid, nodev and noexec to harden folders like /tmp, nofail so a missing disk does not block boot, and _netdev for network storage. Lines are processed in order, so a parent mount must come before mounts inside it.
- UUID=3f1c... /data xfs defaults,noatime 0 0
- UUID=8a2e... none swap defaults 0 0
- findmnt --verify checks fstab for errors
How systemd turns fstab into mounts
At boot, and whenever you run systemctl daemon-reload, a small program called systemd-fstab-generator reads /etc/fstab and creates a mount unit for each line. /data becomes data.mount and /srv/web becomes srv-web.mount. systemd then mounts them in the right order and in parallel where it can.
This explains two things. First, after editing fstab you should run systemctl daemon-reload, or systemd may act on the old version. Second, if a required mount fails, local-fs.target fails and the system drops into emergency mode, asking for the root password. That is what nofail prevents for non-critical disks. You can also add x-systemd.automount to mount a file system only when it is first accessed, which helps for slow or optional storage.
- 1/etc/fstab line
UUID=... /data xfs defaults 0 0
- 2fstab generator
Creates data.mount at boot or daemon-reload
- 3Wait for device
udev reports the UUID is present
- 4Mount
systemd runs mount for data.mount
- 5local-fs.target
Reached when all local mounts succeed
When umount says target is busy
You cannot unmount a file system while any process uses it: a shell whose current directory is inside it, an open log file, or a running program loaded from it. The first suspect is often your own shell, so cd / first.
To find the others, fuser -vm /data lists every process using that mount with its PID and user, and lsof /data gives similar output with file names. Stop the service cleanly, then unmount. fuser -km /data kills them all, which is quick but rough.
umount -l (lazy) detaches the mount point at once and cleans up when the last user lets go. It is handy for a stuck network mount, but the processes keep using the old file system, so do not then format or remove the device. To change options without unmounting, use mount -o remount,ro /data.
- cd / before you unmount
- fuser -vm /data or lsof /data shows users
- mount -o remount,rw / makes root writable in emergency mode
Swap partitions, swap files and swappiness
Swap can be a partition or a plain file. A file is easier on a VM with no spare disk. Create it with dd or fallocate, set mode 600 so only root can read it, run mkswap, then swapon. dd writing real zeros works on every file system; fallocate is faster and supported on ext4 and xfs.
With several swap areas, pri= in the options sets priority, and higher numbers are used first. swapon --show and free -h show what is active. swapoff /swapfile moves pages back into RAM, which needs enough free memory.
vm.swappiness (default 60) tells the kernel how willing it is to swap out application memory compared with dropping file cache. Lower values such as 10 are common on database servers. Set it at runtime with sysctl vm.swappiness=10 and persistently in a file under /etc/sysctl.d/.
- free -h: memory and swap totals
- swapon --show: active swap areas
- cat /proc/sys/vm/swappiness: current value
Learn it in 5 short steps
About 35m each. Track them with a timer in your plan.
- 1Mount and unmount manually~35m
- 2Add UUID entries to /etc/fstab~35m
- 3Test fstab with mount -a~35m
- 4Create and enable swap~35m
- 5Find what keeps a mount busy~35m
Key points
- fstab fields: device, mount point, type, options, dump, fsck order.
- Always run mount -a after editing fstab; a bad line can drop boot into emergency mode.
- Use nofail for non-critical disks so a missing disk does not block boot.
- Swap: mkswap, swapon, then an fstab line with type swap; check with swapon --show.
- lsof or fuser -m shows what keeps a mount busy.
Key terms
Tap a card to flip it. 0/5 known
Examples
sudo mkdir -p /data
UUID=$(sudo blkid -s UUID -o value /dev/vdb1)
echo "UUID=$UUID /data xfs defaults 0 0" | sudo tee -a /etc/fstab
sudo systemctl daemon-reload && sudo mount -a && findmnt /data
sudo mkswap /dev/vdb3
echo "UUID=$(sudo blkid -s UUID -o value /dev/vdb3) none swap defaults 0 0" | sudo tee -a /etc/fstab
sudo swapon -a && swapon --showPersistent data mount and swap by UUID.
Add a 2 GiB swap file to a small VM
A 1 GiB cloud VM keeps getting processes killed by the out-of-memory killer during package updates. Step 1: check there is no swap yet and enough free disk. Step 2: create a 2 GiB file with dd and lock it down to mode 600. Step 3: format it as swap and turn it on. Step 4: add it to fstab so it returns after a reboot, then confirm.
swapon --show; df -h /
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab
sudo swapoff /swapfile && sudo swapon -a
swapon --show; free -hCatch a bad fstab line before it breaks the boot
You added a mount for a new disk, but typed the UUID wrong. Step 1: run findmnt --verify, which checks every line and reports that the source cannot be found. Step 2: copy the UUID from blkid instead of typing it. Step 3: reload systemd and run mount -a; no output means success. Step 4: optional safety for a non-critical disk: add nofail so even a dead disk will not stop the boot.
sudo findmnt --verify
# [W] UUID=1234-bad does not exist
sudo blkid -s UUID -o value /dev/vdb1
sudoedit /etc/fstab
# UUID=<real-uuid> /backup xfs defaults,nofail 0 0
sudo systemctl daemon-reload
sudo mount -a && findmnt /backupQuick reference
- fstab fields: source, mount point, type, options, dump (0), fsck order (0 for xfs and swap)
- sudo findmnt --verify checks /etc/fstab before you reboot
- sudo systemctl daemon-reload && sudo mount -a applies new fstab lines
- findmnt /data shows source, type and options of one mount
- sudo fuser -vm /data lists processes that keep a mount busy
- sudo mount -o remount,ro /data changes options without unmounting
- Swap file: dd, chmod 600, mkswap, swapon, then add a line to fstab
- sysctl vm.swappiness=10 makes the kernel swap less (default is 60)
Common mistakes
- Rebooting after an fstab edit without testing it with mount -a.
- Using /dev/sdX names that change when disks are added.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
List all real (non-virtual) mounted file systems with their source, type and options.
- Exercise 2medium
Mount /dev/vdb1 at /data persistently by UUID with noatime, make sure a missing disk will not block boot, and prove it works without rebooting.
- Exercise 3hard
Remove a swap partition /dev/vdb3 completely and replace it with a 1 GiB swap file of higher priority, all without a reboot.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- How much swap do I need?
- There is no single rule. A few GiB is common for servers; it is a buffer, not extra RAM. If you need to hibernate, swap must be at least as large as RAM.
- My server boots into emergency mode after I edited fstab. How do I fix it?
- Log in with the root password, run mount -o remount,rw / so you can edit, fix or comment out the bad line in /etc/fstab, then run systemctl daemon-reload and reboot.
- Is a swap file slower than a swap partition?
- On modern kernels with ext4 or xfs the difference is negligible. Swap files are simply easier to add and resize.
- What is the difference between mount and findmnt?
- Both can show mounts, but findmnt prints a clear tree with chosen columns and can verify fstab. mount is mainly used to attach file systems.