ServerHandbook live check: run log ------------------------------------------------------------------------------ Check scripts/live/checks/swap-file-ubuntu-24-04.mjs (sha256 b5e6b3bea343a5d7c837c67cb73cb80f989797ae47d11f4e9dcbe1877fdf055a) Ran at 2026-11-11T19:00:14Z (105 s) Result PASS, all 34 claims held Tested on System Ubuntu 24.04.5 LTS Kernel 6.8.0-147-generic Package util-linux 2.39.3-9ubuntu6.6 Package mount 2.39.3-9ubuntu6.6 Package procps 2:4.0.4-4ubuntu3.3 Package linux-image-virtual 6.8.0-147.147 Setup A fresh Ubuntu 24.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 ------------------------------------------------------------------------------ Claims (34), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 24.04 server box (web-server) starts 2. ok a fresh Ubuntu 24.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 292Mi 1.5Gi 1.1Mi 324Mi 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 1.7G 22G 8% / 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 11 19:01 /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=ab1dad03-2bca-4c43-ab5d-cf41b547e8dd 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 -2 10. ok free -h now shows 2.0Gi of swap | total used free shared buff/cache available | Mem: 1.9Gi 293Mi 1.5Gi 1.0Mi 325Mi 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 (swapoff took it off) 14. ok swapon -a turns it on again from /etc/fstab alone: the line works | NAME TYPE SIZE USED PRIO | /swapfile file 2G 0B -2 15. ok the default swappiness is 60 | 60 16. ok tee prints the setting it wrote to /etc/sysctl.d/99-swappiness.conf | vm.swappiness=10 17. ok sysctl -p applies it now: "vm.swappiness = 10" | vm.swappiness = 10 18. ok the server comes back after the reboot | 27 19. ok after the reboot /swapfile is on again (from /etc/fstab) | NAME TYPE SIZE USED PRIO | /swapfile file 2G 0B -2 20. ok after the reboot swappiness is still 10 (from /etc/sysctl.d) | 10 21. ok (hidden) when a program needs more memory than the 2 GB of RAM, the kernel writes pages to the swap file | pswpout 0 → 181687 22. ok swapon --show then shows some swap USED | NAME TYPE SIZE USED PRIO | /swapfile file 2G 8.1M -2 23. ok swapon on a file that is already on: "swapon failed: Device or resource busy" | swapon: /swapfile: swapon failed: Device or resource busy 24. ok a world-readable file: swapon warns "insecure permissions 0644, 0600 suggested."… | swapon: /swapfile: insecure permissions 0644, 0600 suggested. 25. ok …but turns it on anyway, so only the warning tells you | NAME TYPE SIZE USED PRIO | /swapfile file 2G 0B -2 26. ok (chmod 600 fixes it) | -rw------- 1 root root 2147483648 Nov 11 19:01 /swapfile 27. 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=48d3d45b-ecd7-4742-afaa-b0c0129b673f 28. ok swapon --show now lists 4G; the /etc/fstab line needs no change | NAME TYPE SIZE USED PRIO | /swapfile file 4G 0B -2 29. ok (tested on) the box reports its OS, kernel and the version of 4 package(s) | PRETTY_NAME="Ubuntu 24.04.5 LTS" | VIRT=none | KERNEL=6.8.0-147-generic | PKG linux-image-virtual 6.8.0-147.147 | PKG mount 2.39.3-9ubuntu6.6 | PKG procps 2:4.0.4-4ubuntu3.3 | PKG util-linux 2.39.3-9ubuntu6.6 30. ok (the root file system is ext4, where fallocate works for swap) | ext4 31. ok sysctl vm.swappiness=60 puts the default back now | vm.swappiness = 60 32. ok after the undo there is no swap 33. 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 34. ok (nothing in /etc/fstab differs from the backup) | 0 ------------------------------------------------------------------------------ Commands and what they printed (36) ------------------------------------------------------------------------------ $ swapon --show $ free -h total used free shared buff/cache available Mem: 1.9Gi 292Mi 1.5Gi 1.1Mi 324Mi 1.6Gi Swap: 0B 0B 0B $ df -h / Filesystem Size Used Avail Use% Mounted on /dev/vda1 24G 1.7G 22G 8% / $ sudo fallocate -l 2G /swapfile $ sudo chmod 600 /swapfile $ ls -lh /swapfile -rw------- 1 root root 2.0G Nov 11 19:01 /swapfile $ sudo mkswap /swapfile Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=ab1dad03-2bca-4c43-ab5d-cf41b547e8dd $ sudo swapon /swapfile $ swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -2 $ free -h total used free shared buff/cache available Mem: 1.9Gi 293Mi 1.5Gi 1.0Mi 325Mi 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 swapoff /swapfile $ sudo swapon -a $ swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -2 $ 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 -2 $ cat /proc/sys/vm/swappiness 10 $ swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 8.1M -2 $ 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=48d3d45b-ecd7-4742-afaa-b0c0129b673f $ sudo swapon /swapfile $ swapon --show NAME TYPE SIZE USED PRIO /swapfile file 4G 0B -2 $ 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's source (scripts/live/checks/swap-file-ubuntu-24-04.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB: Add a swap file on Ubuntu 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 = {} 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 24.04 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) 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) // ── 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 then shows some swap USED', /\/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) reportBox( ctx, L, 'A fresh Ubuntu 24.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.', ['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