Swap
TLDR
Goal: add a 2 GB swap file as a safety net against running out of memory, used only when RAM runs low.
Step: Check
Commands:
swapon --show
free -h
Purpose: Confirm there's no swap yet
────────────────────────────────────────
Step: 2.1
Command: fallocate -l 2G /swapfile
Purpose: Reserve 2 GB of disk
────────────────────────────────────────
Step: 2.2
Command: chmod 600 /swapfile
Purpose: Only root can read it (swap can hold secrets)
────────────────────────────────────────
Step: 2.3
Command: mkswap /swapfile
Purpose: Format the file as swap
────────────────────────────────────────
Step: 2.4
Command: swapon /swapfile
Purpose: Turn swap on now
────────────────────────────────────────
Step: 3.1
Command: cp /etc/fstab /etc/fstab.bak-2026-11-23
Purpose: Back up fstab
────────────────────────────────────────
Step: 3.2
Command: echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab
Purpose: Turn swap on at every boot (-a appends; without it the
file is overwritten)
────────────────────────────────────────
Step: 3.3
Command: findmnt --verify + systemctl daemon-reload
Purpose: Check fstab and refresh systemd's copy
────────────────────────────────────────
Step: 4.1
Command: sysctl vm.swappiness=10
Purpose: Prefer RAM, use swap only as a fallback (applies now)
────────────────────────────────────────
Step: 4.2
Command: echo 'vm.swappiness=10' | tee
/etc/sysctl.d/99-swappiness.conf
Purpose: Make the setting permanent
────────────────────────────────────────
Step: 4.3
Command: sysctl -p /etc/sysctl.d/99-swappiness.conf
Purpose: Test that the file loads
CREATING SWAP
1: Reserve the space
sudo fallocate -l 2G /swapfile
What each part does:
- fallocate: reserves disk space for a file right away without writing any data
- -l 2G: the length (size) of the file, 2 gigabytes.
- /swapfile: the file to create. The root of the disk is the usual place for it
Check with:
ls -lh /swapfile
2: Lock down the permissions
sudo chmod 600 /swapfile
What each part does:
- chmod: changes a file's permissions, i.e. who can read, write or run it.
- 600: three digits, one each for the owner, the group and everyone else:
- 6 = read (4) + write (2): the owner (root) can read and write
- 0: the group gets no access
- 0: everyone else gets no access
Why it matters: Linux moves memory from running programs into this file, and that memory can include passwords, database contents or your BookStack APP_KEY. If any user could read the file, they could pull those secrets out. mkswap will warn you if the permissions are too open.
3: format the file as swap
sudo mkswap /swapfile
What it does: mkswap writes a small header at the start of the file that marks it as swap space. It's like formatting a disk, except it formats just this one file. Swap doesn't use a filesystem like ext4: Linux writes memory pages straight into the file.
4: turn swap on
sudo swapon /swapfile
What it does: swapon tells the kernel to start using /swapfile as swap from now on. It takes effect immediately, with no reboot.
5. confirm it worked:
swapon --show
Expected output:
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
- TYPE file: a swap file, as opposed to a swap partition.
- USED 0B: normal, because you have plenty of free RAM right now.
- PRIO -2: the priority. It only matters if there are several swap areas, so you can ignore it.
6. Make it mount on boot
Backup current fstab:
cp /etc/fstab /etc/fstab.bak-X #where X can be current year/date
add the swap line
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
What each part does:
- echo '...': prints the line we want to add.
- |: a "pipe". It passes that output on to the next command.
- tee -a /etc/fstab: tee writes what it receives to a file, and -a means append, i.e. add to the end. ⚠️ Without -a, tee would overwrite the whole file and wipe out your disk entries, so check the -a is there before you press Enter.
The six columns of the new line:
┌───────────┬───────────┬─────────────────────────────────────┐
│ Column │ Value │ Meaning │
├───────────┼───────────┼─────────────────────────────────────┤
│ file │ /swapfile │ the file we created │
│ system │ │ │
├───────────┼───────────┼─────────────────────────────────────┤
│ mount │ none │ swap isn't mounted at a folder │
│ point │ │ │
├───────────┼───────────┼─────────────────────────────────────┤
│ type │ swap │ │
├───────────┼───────────┼─────────────────────────────────────┤
│ options │ sw │ the standard option for swap │
├───────────┼───────────┼─────────────────────────────────────┤
│ dump │ 0 │ old backup tool, not used │
├───────────┼───────────┼─────────────────────────────────────┤
│ pass │ 0 │ skip the disk check at boot (only │
│ │ │ real filesystems get one) │
└───────────┴───────────┴─────────────────────────────────────┘
What you should see: the line printed once on screen. That's tee showing what it wrote.
Verify before trusting it at boot
tail -3 /etc/fstab
Your two original lines should still be there, plus /swapfile none swap sw 0 0 exactly once at the bottom.
findmnt --verify
This checks the whole file for mistakes without rebooting. You want it to say Success, no errors or warnings detected.
use systemctl daemon-reload to reload fstabif needed
6. swappiness
What swappiness is
Swappiness (vm.swappiness) is a kernel setting from 0 to 100 that controls how readily Linux moves memory to swap:
- High (60, Ubuntu's default): Linux moves memory that programs aren't actively using into swap fairly early, to keep RAM free for file caching. That suits a desktop.
- Low (10): Linux keeps program memory in RAM and uses swap mainly when RAM is genuinely running out. That suits a server, because reading from swap on disk is much slower than RAM, and a database like MariaDB slows down noticeably if its memory gets swapped out.
- 0: Linux avoids swap almost completely, which weakens the safety net we just added.
10 is a common value for servers: swap stays an emergency buffer instead of being part of normal operation
7. Set up
check the current value
cat /proc/sys/vm/swappiness
Use command below to change the value:
sysctl vm.swappiness=10
What it does: sysctl reads and changes kernel settings while the system is running. This sets swappiness to 10 immediately, with no reboot, and doesn't affect running programs or containers.
What each part does:
- sysctl: the tool for kernel settings. It's the clean way to change the files under /proc/sys/.
- vm.swappiness: the setting's name. The dot stands for a folder level, so it maps to /proc/sys/vm/swappiness, the file you just read.
- =10: the new value.
What you should see:
vm.swappiness = 10
sysctl prints the setting back to confirm the change.
make the setting permanent
echo 'vm.swappiness=10' | tee /etc/sysctl.d/99-swappiness.conf
What it does: creates a small config file that the system reads at every boot, so swappiness is set to 10 automatically.
What each part does:
- echo 'vm.swappiness=10': prints the setting, written exactly as sysctl expects it.
- | tee /etc/sysctl.d/99-swappiness.conf: writes it to a new file. There's no -a this time: we're creating our own file instead of adding to an existing one, so overwriting is fine.
Why a file in /etc/sysctl.d/:
- At boot, the system reads every .conf file in /etc/sysctl.d/ and applies the settings in them.
- The files are applied in alphabetical order, and a later file overrides an earlier one. The 99- prefix makes ours run last, so it wins over any default.
- Keeping your own settings in your own file, rather than editing the main /etc/sysctl.conf, makes them easy to find, and package updates won't overwrite them.
- To undo it: delete the file.
test that the file loads correctly
sysctl -p /etc/sysctl.d/99-swappiness.conf
What it does: -p loads the settings from the file you name and applies them, the same way it will happen at boot. If the file had a typo, you'd see an error now instead of finding out after a reboot.
No comments to display
No comments to display