ServerHandbook live check: run log ------------------------------------------------------------------------------ Article https://serverhandbook.com/kb/swap-file-ubuntu Check scripts/live/checks/swap-file-ubuntu.mjs (sha256 acf8e88eb9e1085c0fa7be3ed66b506e45255ca3323bdac81da1bf39ed3c051f) Ran at 2026-11-18T11:51:51Z (138 s) Result PASS, all 37 claims held Tested on System Ubuntu 26.04.1 LTS Kernel 7.0.0-43-generic Package util-linux 2.41.3-3ubuntu2.2 Package mount 2.41.3-3ubuntu2.2 Package procps 2:4.0.4-9ubuntu1 Package linux-image-virtual 7.0.0-43.43 Setup A fresh Ubuntu 26.04 server from Ubuntu’s cloud image with 2 GB of RAM and a 25 GB ext4 disk, reached over SSH as a sudo user. Every version above was read from the server at run time. Passwords used on the throwaway test server are shown as [password]. How we test: https://serverhandbook.com/how-we-test ------------------------------------------------------------------------------ What the steps changed on the server (recorded before the first step and after the last) Packages added (0) Packages upgraded (0) Packages removed (0) Services enabled at boot (0) Services disabled (0) Listening sockets opened (0) Listening sockets closed (0) Files added in /etc (2) /etc/fstab.bak /etc/sysctl.d/99-swappiness.conf Files changed in /etc (1) /etc/fstab Files removed from /etc (0) Disk used on / (includes logs written during the run): +4096.5 MB ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Check-your-server script (3 items): https://cdn.serverhandbook.com/check/swap-file-ubuntu/ubuntu-26.04.1-20261118t115151z.sh SHA-256 f55379fcea7d11e588e8f8deb1e1d4e1e58b63662e56a9f7f2b66140e135c5f3 ------------------------------------------------------------------------------ On the stock server, before the first step: FAIL /swapfile is on as swap FAIL the /etc/fstab line turns it on at boot FAIL swappiness is 10 After the last step: PASS /swapfile is on as swap PASS the /etc/fstab line turns it on at boot PASS swappiness is 10 ------------------------------------------------------------------------------ Claims (37), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 26.04 server box (web-server) starts 2. ok a fresh Ubuntu 26.04 cloud server has no swap: swapon --show prints nothing 3. ok free -h shows Swap: 0B | total used free shared buff/cache available | Mem: 1.9Gi 343Mi 1.3Gi 5.0Mi 455Mi 1.6Gi | Swap: 0B 0B 0B 4. ok the disk has room for a 2 GB file | Filesystem Size Used Avail Use% Mounted on | /dev/vda1 24G 2.0G 22G 9% / 5. ok fallocate creates the 2 GB file at once and prints nothing 6. ok the file is 2.0G, owned by root and readable by root only | -rw------- 1 root root 2.0G Nov 18 11:52 /swapfile 7. ok mkswap formats it: "Setting up swapspace version 1, size = 2 GiB" | Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) | no label, UUID=35439019-a25a-435b-aeaf-cdeec730d061 8. ok swapon turns it on and prints nothing 9. ok swapon --show lists /swapfile, 2G, nothing used yet | NAME TYPE SIZE USED PRIO | /swapfile file 2G 0B -1 10. ok free -h now shows 2.0Gi of swap | total used free shared buff/cache available | Mem: 1.9Gi 347Mi 1.3Gi 5.0Mi 456Mi 1.6Gi | Swap: 2.0Gi 0B 2.0Gi 11. ok tee prints the line it added to /etc/fstab | /swapfile none swap sw 0 0 12. ok findmnt --verify finds no errors in /etc/fstab | none | [W] non-bind mount source /swapfile is a directory or regular file | [W] your fstab has been modified, but systemd still uses the old version; | use 'systemctl daemon-reload' to reload | | 0 parse errors, 0 errors, 2 warnings 13. ok …only two warnings: a swap FILE (expected) and systemd still holding the old fstab | none | [W] non-bind mount source /swapfile is a directory or regular file | [W] your fstab has been modified, but systemd still uses the old version; | use 'systemctl daemon-reload' to reload | | 0 parse errors, 0 errors, 2 warnings 14. ok after daemon-reload only the expected swap-file warning is left | none | [W] non-bind mount source /swapfile is a directory or regular file | | 0 parse errors, 0 errors, 1 warning 15. ok (swapoff took it off) 16. ok swapon -a turns it on again from /etc/fstab alone: the line works | NAME TYPE SIZE USED PRIO | /swapfile file 2G 0B -1 17. ok the default swappiness is 60 | 60 18. ok tee prints the setting it wrote to /etc/sysctl.d/99-swappiness.conf | vm.swappiness=10 19. ok sysctl -p applies it now: "vm.swappiness = 10" | vm.swappiness = 10 20. ok the server comes back after the reboot | 29 21. ok after the reboot /swapfile is on again (from /etc/fstab) | NAME TYPE SIZE USED PRIO | /swapfile file 2G 0B -1 22. ok after the reboot swappiness is still 10 (from /etc/sysctl.d) | 10 23. ok (hidden) when a program needs more memory than the 2 GB of RAM, the kernel writes pages to the swap file | pswpout 0 → 202017 24. ok swapon --show afterwards still shows some swap USED (most was freed when the program ended) | NAME TYPE SIZE USED PRIO | /swapfile file 2G 428K -1 25. ok swapon on a file that is already on: "swapon failed: Device or resource busy" | swapon: /swapfile: swapon failed: Device or resource busy 26. ok a world-readable file: swapon warns "insecure permissions 0644, 0600 suggested."… | swapon: /swapfile: insecure permissions 0644, 0600 suggested. 27. ok …but turns it on anyway, so only the warning tells you | NAME TYPE SIZE USED PRIO | /swapfile file 2G 0B -1 28. ok (chmod 600 fixes it) | -rw------- 1 root root 2147483648 Nov 18 11:52 /swapfile 29. ok mkswap on the old file warns it is wiping the old swap signature and sets up 4 GiB | mkswap: /swapfile: warning: wiping old swap signature. | Setting up swapspace version 1, size = 4 GiB (4294963200 bytes) | no label, UUID=745b783b-f8d4-48e5-acc6-f54b1bc92243 30. ok swapon --show now lists 4G; the /etc/fstab line needs no change | NAME TYPE SIZE USED PRIO | /swapfile file 4G 0B -1 31. ok (the /etc/fstab line is in place) | /swapfile none swap sw 0 0 32. ok (tested on) the box reports its OS, kernel and the version of 4 package(s) | PRETTY_NAME="Ubuntu 26.04.1 LTS" | VIRT=none | KERNEL=7.0.0-43-generic | PKG linux-image-virtual 7.0.0-43.43 | PKG mount 2.41.3-3ubuntu2.2 | PKG procps 2:4.0.4-9ubuntu1 | PKG util-linux 2.41.3-3ubuntu2.2 33. ok (the root file system is ext4, where fallocate works for swap) | ext4 34. ok sysctl vm.swappiness=60 puts the default back now | vm.swappiness = 60 35. ok after the undo there is no swap 36. ok (no /swapfile line is left in /etc/fstab) | LABEL=cloudimg-rootfs / ext4 discard,commit=30,errors=remount-ro 0 1 | LABEL=BOOT /boot ext4 defaults 0 2 | LABEL=UEFI /boot/efi vfat umask=0077 0 1 37. ok (nothing in /etc/fstab differs from the backup) | 0 ------------------------------------------------------------------------------ Commands and what they printed (38) ------------------------------------------------------------------------------ $ swapon --show $ free -h total used free shared buff/cache available Mem: 1.9Gi 343Mi 1.3Gi 5.0Mi 455Mi 1.6Gi Swap: 0B 0B 0B $ df -h / Filesystem Size Used Avail Use% Mounted on /dev/vda1 24G 2.0G 22G 9% / $ sudo fallocate -l 2G /swapfile $ sudo chmod 600 /swapfile $ ls -lh /swapfile -rw------- 1 root root 2.0G Nov 18 11:52 /swapfile $ sudo mkswap /swapfile Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=35439019-a25a-435b-aeaf-cdeec730d061 $ sudo swapon /swapfile $ swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -1 $ free -h total used free shared buff/cache available Mem: 1.9Gi 347Mi 1.3Gi 5.0Mi 456Mi 1.6Gi Swap: 2.0Gi 0B 2.0Gi $ sudo cp /etc/fstab /etc/fstab.bak $ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab /swapfile none swap sw 0 0 $ sudo findmnt --verify none [W] non-bind mount source /swapfile is a directory or regular file [W] your fstab has been modified, but systemd still uses the old version; use 'systemctl daemon-reload' to reload 0 parse errors, 0 errors, 2 warnings $ sudo systemctl daemon-reload $ sudo findmnt --verify none [W] non-bind mount source /swapfile is a directory or regular file 0 parse errors, 0 errors, 1 warning $ sudo swapoff /swapfile $ sudo swapon -a $ swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -1 $ cat /proc/sys/vm/swappiness 60 $ echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf vm.swappiness=10 $ sudo sysctl -p /etc/sysctl.d/99-swappiness.conf vm.swappiness = 10 $ sudo reboot $ swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -1 $ cat /proc/sys/vm/swappiness 10 $ swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 428K -1 $ sudo swapon /swapfile swapon: /swapfile: swapon failed: Device or resource busy $ sudo swapon /swapfile swapon: /swapfile: insecure permissions 0644, 0600 suggested. $ sudo swapoff /swapfile $ sudo fallocate -l 4G /swapfile $ sudo mkswap /swapfile mkswap: /swapfile: warning: wiping old swap signature. Setting up swapspace version 1, size = 4 GiB (4294963200 bytes) no label, UUID=745b783b-f8d4-48e5-acc6-f54b1bc92243 $ sudo swapon /swapfile $ swapon --show NAME TYPE SIZE USED PRIO /swapfile file 4G 0B -1 $ sudo swapoff /swapfile $ sudo sed -i '\|^/swapfile |d' /etc/fstab $ sudo rm /swapfile $ sudo rm /etc/sysctl.d/99-swappiness.conf $ sudo sysctl vm.swappiness=60 vm.swappiness = 60 $ swapon --show ------------------------------------------------------------------------------ The check-your-server script, its full screen before the steps ------------------------------------------------------------------------------ ServerHandbook check, swap-file-ubuntu (proved 2026-11-18) FAIL /swapfile is on as swap fix: (proof run)#step-2-create FAIL the /etc/fstab line turns it on at boot fix: (proof run)#step-3-fstab FAIL swappiness is 10 fix: (proof run)#step-4-swappiness 0 passed, 3 failed To fix a FAIL, follow the steps in (proof run) ------------------------------------------------------------------------------ The check-your-server script, its full screen after the steps ------------------------------------------------------------------------------ ServerHandbook check, swap-file-ubuntu (proved 2026-11-18) PASS /swapfile is on as swap PASS the /etc/fstab line turns it on at boot PASS swappiness is 10 3 passed, 0 failed ------------------------------------------------------------------------------ The check's source (scripts/live/checks/swap-file-ubuntu.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB: Add a swap file on Ubuntu 26.04 and 24.04 (run per release, --releases=26.04,24.04): check there is none, create it, keep it after a reboot, set swappiness, see it * used, the two errors people hit, resize, undo. * * The server is a fresh VM box from Ubuntu's cloud image (2 GB RAM, 25 GB disk: a small VPS), reached as the sudo user * `deploy`. Cloud images ship with no swap at all, which is what a new VPS looks like. "Swap is used when RAM runs * out" is proved by a hidden step that allocates more memory than the box has and reads the kernel's own swap-out * counter; the reader is never asked to do that to a live server. Every claim below is one the article makes. */ import { rebootBox } from '../box.mjs' import { bareBox, reportBox } from '../lib.mjs' export const meta = { // The reader's check-your-server script (plan05 phase 3): proved by the runner on the stock server and after the steps. reader: [ { item: "/swapfile is on as swap", cmd: "swapon --show", expect: "^/swapfile\\s+file\\s", step: "step-2-create" }, { item: "the /etc/fstab line turns it on at boot", cmd: "grep \"^/swapfile \" /etc/fstab", expect: "^/swapfile none swap sw 0 0$", step: "step-3-fstab" }, { item: "swappiness is 10", cmd: "cat /proc/sys/vm/swappiness", expect: "^10$", step: "step-4-swappiness" }, ], } const RELEASE = process.env.LIVE_BOX_RELEASE || '24.04' const FSTAB_LINE = '/swapfile none swap sw 0 0' const SYSCTL = '/etc/sysctl.d/99-swappiness.conf' /** `/swapfile file 2G 0B -2` → the SIZE column. */ const sizeOf = (show) => (show.match(/^\/swapfile\s+file\s+(\S+)/m) ?? [])[1] ?? '' const pswpout = (L) => Number((L('grep ^pswpout /proc/vmstat').match(/(\d+)\s*$/) ?? [])[1] ?? Number.NaN) const check = (ctx) => bareBox(ctx, async ({ sudoUser }) => { const { run, L } = sudoUser() L('sudo true') // a reader has used sudo before (earlier articles), so Ubuntu's first-sudo hint is not on their screen // ── step 1: is there swap already? ─────────────────────────────────────────────────────── const none = run('swapon --show', { id: 'swapon-empty' }) ctx.check(`a fresh Ubuntu ${RELEASE} cloud server has no swap: swapon --show prints nothing`, none.exit === 0 && none.out === '', none.out) const free0 = run('free -h', { id: 'free-before' }) ctx.check('free -h shows Swap: 0B', /^Swap:\s+0B\s+0B\s+0B/m.test(free0.out), free0.out) const df = run('df -h /', { id: 'df' }) ctx.check('the disk has room for a 2 GB file', /\/$/m.test(df.out), df.out) // ── step 2: create it ──────────────────────────────────────────────────────────────────── const fa = run('sudo fallocate -l 2G /swapfile', { id: 'fallocate' }) ctx.check('fallocate creates the 2 GB file at once and prints nothing', fa.exit === 0 && fa.out === '', fa.out) run('sudo chmod 600 /swapfile', { id: 'chmod' }) const ls = run('ls -lh /swapfile', { id: 'ls' }) ctx.check('the file is 2.0G, owned by root and readable by root only', /^-rw------- 1 root root 2\.0G /.test(ls.out), ls.out) const mk = run('sudo mkswap /swapfile', { id: 'mkswap' }) ctx.check('mkswap formats it: "Setting up swapspace version 1, size = 2 GiB"', /Setting up swapspace version 1, size = 2 GiB/.test(mk.out), mk.out) const on = run('sudo swapon /swapfile', { id: 'swapon' }) ctx.check('swapon turns it on and prints nothing', on.exit === 0 && on.out === '', on.out) const show = run('swapon --show', { id: 'swapon-show' }) ctx.check('swapon --show lists /swapfile, 2G, nothing used yet', sizeOf(show.out) === '2G' && /\/swapfile\s+file\s+2G\s+0B/.test(show.out), show.out) const free1 = run('free -h', { id: 'free-after' }) ctx.check('free -h now shows 2.0Gi of swap', /^Swap:\s+2\.0Gi\s/m.test(free1.out), free1.out) // ── step 3: keep it after a reboot ─────────────────────────────────────────────────────── run('sudo cp /etc/fstab /etc/fstab.bak', { id: 'fstab-bak' }) const add = run(`echo '${FSTAB_LINE}' | sudo tee -a /etc/fstab`, { id: 'fstab-add' }) ctx.check('tee prints the line it added to /etc/fstab', add.out === FSTAB_LINE, add.out) const ver = run('sudo findmnt --verify', { id: 'findmnt-verify' }) ctx.check('findmnt --verify finds no errors in /etc/fstab', /0 parse errors, 0 errors/.test(ver.out), ver.out) ctx.check('…only two warnings: a swap FILE (expected) and systemd still holding the old fstab', /non-bind mount source \/swapfile is a directory or regular file/.test(ver.out) && /systemd still uses the old version/.test(ver.out), ver.out) run('sudo systemctl daemon-reload', { id: 'daemon-reload' }) const ver2 = run('sudo findmnt --verify', { id: 'findmnt-verify-2' }) ctx.check('after daemon-reload only the expected swap-file warning is left', /0 parse errors, 0 errors, 1 warning/.test(ver2.out) && !/systemd still uses/.test(ver2.out), ver2.out) run('sudo swapoff /swapfile', { id: 'swapoff' }) ctx.check('(swapoff took it off)', L('swapon --show') === '', L('swapon --show')) run('sudo swapon -a', { id: 'swapon-a' }) const fromFstab = run('swapon --show', { id: 'swapon-show-a' }) ctx.check('swapon -a turns it on again from /etc/fstab alone: the line works', sizeOf(fromFstab.out) === '2G', fromFstab.out) // ── step 4: swappiness ─────────────────────────────────────────────────────────────────── const sw0 = run('cat /proc/sys/vm/swappiness', { id: 'swappiness' }) ctx.check('the default swappiness is 60', sw0.out === '60', sw0.out) const conf = run(`echo 'vm.swappiness=10' | sudo tee ${SYSCTL}`, { id: 'sysctl-file' }) ctx.check(`tee prints the setting it wrote to ${SYSCTL}`, conf.out === 'vm.swappiness=10', conf.out) const apply = run(`sudo sysctl -p ${SYSCTL}`, { id: 'sysctl-apply' }) ctx.check('sysctl -p applies it now: "vm.swappiness = 10"', apply.out === 'vm.swappiness = 10', apply.out) // ── reboot: both survive ───────────────────────────────────────────────────────────────── const secs = rebootBox() ctx.capture('sudo reboot', '', { id: 'reboot' }) ctx.check('the server comes back after the reboot', typeof secs === 'number', String(secs)) const after = run('swapon --show', { id: 'after-reboot' }) ctx.check('after the reboot /swapfile is on again (from /etc/fstab)', sizeOf(after.out) === '2G', after.out) const sw1 = run('cat /proc/sys/vm/swappiness', { id: 'swappiness-after' }) ctx.check('after the reboot swappiness is still 10 (from /etc/sysctl.d)', sw1.out === '10', sw1.out, { reader: { cmd: 'cat /proc/sys/vm/swappiness', expect: '^10$' }, }) // ── it is used when RAM runs out (hidden: allocate more than the box has) ─────────────── const out0 = pswpout(L) L(`python3 -c "x = b'x' * (2300 * 1024**2); print(len(x))"`, 300000) const out1 = pswpout(L) ctx.check('(hidden) when a program needs more memory than the 2 GB of RAM, the kernel writes pages to the swap file', out1 > out0, `pswpout ${out0} → ${out1}`) const used = run('swapon --show', { id: 'swap-used' }) ctx.check('swapon --show afterwards still shows some swap USED (most was freed when the program ended)', /\/swapfile\s+file\s+2G\s+(?!0B)\d/.test(used.out), used.out) // ── troubleshooting ────────────────────────────────────────────────────────────────────── const busy = run('sudo swapon /swapfile', { id: 'busy' }) ctx.check('swapon on a file that is already on: "swapon failed: Device or resource busy"', busy.exit !== 0 && /swapon: \/swapfile: swapon failed: Device or resource busy/.test(busy.out), busy.out) L('sudo swapoff /swapfile && sudo chmod 644 /swapfile') const insecure = run('sudo swapon /swapfile', { id: 'insecure' }) ctx.check('a world-readable file: swapon warns "insecure permissions 0644, 0600 suggested."…', /swapon: \/swapfile: insecure permissions 0644, 0600 suggested\./.test(insecure.out), insecure.out) ctx.check('…but turns it on anyway, so only the warning tells you', sizeOf(L('swapon --show')) === '2G', L('swapon --show')) L('sudo chmod 600 /swapfile') ctx.check('(chmod 600 fixes it)', /^-rw------- /.test(L('ls -l /swapfile')), L('ls -l /swapfile')) // ── resize to 4 GB ─────────────────────────────────────────────────────────────────────── run('sudo swapoff /swapfile', { id: 'resize-off', timeout: 600000 }) run('sudo fallocate -l 4G /swapfile', { id: 'resize-fallocate' }) const rmk = run('sudo mkswap /swapfile', { id: 'resize-mkswap' }) ctx.check('mkswap on the old file warns it is wiping the old swap signature and sets up 4 GiB', /warning: wiping old swap signature\./.test(rmk.out) && /size = 4 GiB/.test(rmk.out), rmk.out) run('sudo swapon /swapfile', { id: 'resize-on' }) const r4 = run('swapon --show', { id: 'resize-show' }) ctx.check('swapon --show now lists 4G; the /etc/fstab line needs no change', sizeOf(r4.out) === '4G', r4.out, { reader: { cmd: 'swapon --show', expect: '^/swapfile\\s+file\\s' }, }) const fst = L('grep "^/swapfile " /etc/fstab') ctx.check('(the /etc/fstab line is in place)', fst === '/swapfile none swap sw 0 0', fst, { reader: { cmd: 'grep "^/swapfile " /etc/fstab', expect: '^/swapfile none swap sw 0 0$' }, }) ctx.stepsDone() // the article's steps end here; the undo follows reportBox( ctx, L, `A fresh Ubuntu ${RELEASE} server from Ubuntu’s cloud image with 2 GB of RAM and a 25 GB ext4 disk, reached over SSH as a sudo user.`, ['util-linux', 'mount', 'procps', 'linux-image-virtual'], ) ctx.check('(the root file system is ext4, where fallocate works for swap)', L('findmnt -no FSTYPE /') === 'ext4', L('findmnt -no FSTYPE /')) // ── undo ───────────────────────────────────────────────────────────────────────────────── run('sudo swapoff /swapfile', { id: 'undo-off', timeout: 600000 }) run(`sudo sed -i '\\|^/swapfile |d' /etc/fstab`, { id: 'undo-fstab' }) run('sudo rm /swapfile', { id: 'undo-rm' }) run(`sudo rm ${SYSCTL}`, { id: 'undo-sysctl-rm' }) const back = run('sudo sysctl vm.swappiness=60', { id: 'undo-sysctl' }) ctx.check('sysctl vm.swappiness=60 puts the default back now', back.out === 'vm.swappiness = 60', back.out) const gone = run('swapon --show', { id: 'undo-show' }) ctx.check('after the undo there is no swap', gone.out === '', gone.out) ctx.check('(no /swapfile line is left in /etc/fstab)', !L('cat /etc/fstab').includes('/swapfile'), L('cat /etc/fstab')) ctx.check('(nothing in /etc/fstab differs from the backup)', L('sudo diff /etc/fstab /etc/fstab.bak && echo same') === 'same', L('sudo diff /etc/fstab /etc/fstab.bak; echo $?')) }) export default check