ServerHandbook live check: run log ------------------------------------------------------------------------------ Article https://serverhandbook.com/kb/ufw-firewall-ubuntu-24-04 Check scripts/live/checks/ufw-firewall-ubuntu-24-04.mjs (sha256 10be4346f2017b6cebefe0d896157ea0a91bf13a7cf8ccfebb900fd735635ce2) Ran at 2026-11-11T16:43:17Z (171 s) Result PASS, all 51 claims held Tested on System Ubuntu 24.04.5 LTS Kernel 6.8.0-147-generic Package ufw 0.36.2-6 Package iptables 1.8.10-3ubuntu2 Package openssh-server 1:9.6p1-3ubuntu13.20 Package linux-image-virtual 6.8.0-147.147 Setup A fresh Ubuntu 24.04 server as a provider hands it over, 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 (51), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 24.04 server box (web-server) starts 2. ok (setup) the test web server on port 80 answers from outside before any firewall 3. ok ufw is installed on a fresh server but inactive | Status: inactive 4. ok SSH listens on port 22 (the port the OpenSSH profile opens) | LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1003,fd=3),("systemd",pid=1,fd=59)) | LISTEN 0 4096 [::]:22 [::]:* users:(("sshd",pid=1003,fd=4),("systemd",pid=1,fd=60)) 5. ok the defaults are already deny incoming (DROP), allow outgoing (ACCEPT) | DEFAULT_INPUT_POLICY="DROP" | DEFAULT_OUTPUT_POLICY="ACCEPT" | DEFAULT_FORWARD_POLICY="DROP" | DEFAULT_APPLICATION_POLICY="SKIP" 6. ok ufw app list shows the OpenSSH profile | Available applications: | OpenSSH 7. ok the OpenSSH profile opens 22/tcp | Profile: OpenSSH | Title: Secure shell server, an rshd replacement | Description: OpenSSH is a free implementation of the Secure Shell protocol. | | Port: | 22/tcp 8. ok ufw allow OpenSSH adds the rule for IPv4 and IPv6 while ufw is still off | Rules updated | Rules updated (v6) 9. ok ufw show added lists the saved rule before the firewall is on | Added user rules (see 'ufw status' for running firewall): | ufw allow OpenSSH 10. ok (setup) the reader’s open terminal (one kept SSH connection) opens 11. ok ufw enable warns "Command may disrupt existing ssh connections" and asks y/n | Command may disrupt existing ssh connections. Proceed with operation (y|n)? y | Firewall is active and enabled on system startup 12. ok after y: "Firewall is active and enabled on system startup" | Command may disrupt existing ssh connections. Proceed with operation (y|n)? y | Firewall is active and enabled on system startup 13. ok the open terminal still works after enable 14. ok a NEW SSH login from outside still gets in (step 4’s second terminal) 15. ok status verbose: active, deny incoming, allow outgoing, OpenSSH allowed for v4 and v6 | Status: active | Logging: on (low) | Default: deny (incoming), allow (outgoing), disabled (routed) | New profiles: skip | | To Action From | -- ------ ---- | 22/tcp (OpenSSH) ALLOW IN Anywhere | 22/tcp (OpenSSH (v6)) ALLOW IN Anywhere (v6) 16. ok logging is on (low) by default | Status: active | Logging: on (low) | Default: deny (incoming), allow (outgoing), disabled (routed) | New profiles: skip | | To Action From | -- ------ ---- | 22/tcp (OpenSSH) ALLOW IN Anywhere | 22/tcp (OpenSSH (v6)) ALLOW IN Anywhere (v6) 17. ok port 80 no longer answers from outside: the web server is still running but ufw drops the packets 18. ok (the test web server is still listening on port 80) | 1 19. ok /var/log/ufw.log records the drop: [UFW BLOCK] … DPT=80 … SYN | 2026-11-11T16:44:25.202430+00:00 web-server kernel: [UFW BLOCK] IN=enp0s2 OUT= MAC=52:54:00:12:34:56:52:55:0a:00:02:02:08:00 SRC=203.0.113.1 DST=203.0.113.10 LEN=44 TOS=0x00 PREC=0x00 TTL=64 ID=549 PROTO=TCP SPT=54270 DPT=80 WINDOW=65535 RES=0x00 SYN URGP=0 20. ok ufw allow 80/tcp: "Rule added" and "Rule added (v6)" | Rule added | Rule added (v6) 21. ok ufw allow 443/tcp adds both rules | Rule added | Rule added (v6) 22. ok port 80 answers from outside again at once, no reload needed 23. ok adding the same rule twice is harmless: "Skipping adding existing rule" | Skipping adding existing rule | Skipping adding existing rule (v6) 24. ok status numbered lists IPv4 and IPv6 rules as separate numbers | Status: active | | To Action From | -- ------ ---- | [ 1] OpenSSH ALLOW IN Anywhere | [ 2] 80/tcp ALLOW IN Anywhere | [ 3] 443/tcp ALLOW IN Anywhere | [ 4] OpenSSH (v6) ALLOW IN Anywhere (v6) | [ 5] 80/tcp (v6) ALLOW IN Anywhere (v6) | [ 6] 443/tcp (v6) ALLOW IN Anywhere (v6) 25. ok ufw delete allow 443/tcp removes the IPv4 AND the IPv6 rule | Rule deleted | Rule deleted (v6) 26. ok (found the number of the IPv4 80/tcp rule) | Status: active | | To Action From | -- ------ ---- | [ 1] OpenSSH ALLOW IN Anywhere | [ 2] 80/tcp ALLOW IN Anywhere | [ 3] OpenSSH (v6) ALLOW IN Anywhere (v6) | [ 4] 80/tcp (v6) ALLOW IN Anywhere (v6) 27. ok ufw delete asks first, then deletes ONE rule | Deleting: | allow 80/tcp | Proceed with operation (y|n)? y | Rule deleted 28. ok …the IPv6 80/tcp rule is still there, and the numbers below it moved up | Status: active | | To Action From | -- ------ ---- | [ 1] OpenSSH ALLOW IN Anywhere | [ 2] OpenSSH (v6) ALLOW IN Anywhere (v6) | [ 3] 80/tcp (v6) ALLOW IN Anywhere (v6) 29. ok deleting by rule text then removes the IPv6 leftover ("Could not delete non-existent rule" for the IPv4 one) | Could not delete non-existent rule | Rule deleted (v6) 30. ok ufw limit OpenSSH turns the allow rule into a limit rule: "Rule updated" | Rule updated | Rule updated (v6) 31. ok the limit rule is the kernel "recent" match: 6 new connections in 30 seconds from one address | -A ufw-user-input -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -m recent --set --name DEFAULT --mask 255.255.255.255 --rsource -m comment --comment "\'dapp_OpenSSH\'" | -A ufw-user-input -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 30 --hitcount 6 --name DEFAULT --mask 255.255.255.255 --rsource -m comment --comment "\'dapp_OpenSSH\'" -j ufw-user-limit | -A ufw-user-input -p tcp -m tcp --dport 22 -m comment --comment "\'dapp_OpenSSH\'" -j ufw-user-limit-accept 32. ok with the limit on, 5 new logins in a row get in and the 6th and 7th are refused | true true true true true false false 33. ok the refused logins are logged as [UFW LIMIT BLOCK] | 3 34. ok 10 seconds later, while the address keeps trying, it is still refused 35. ok ufw allow OpenSSH turns the limit back into a plain allow: "Rule updated" | Rule updated | Rule updated (v6) 36. ok a new login gets in at once after that 37. ok the server reboots and answers over SSH again (port 22 allowed) | 22 38. ok port 80 answers from outside after the reboot (its rule was kept) 39. ok after a reboot ufw is active with the same rules | Status: active | | To Action From | -- ------ ---- | OpenSSH ALLOW Anywhere | 80/tcp ALLOW Anywhere | OpenSSH (v6) ALLOW Anywhere (v6) | 80/tcp (v6) ALLOW Anywhere (v6) 40. ok /etc/ufw/before.rules accepts RELATED,ESTABLISHED traffic before any user rule | 25:-A ufw-before-input -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT 41. 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 iptables 1.8.10-3ubuntu2 | PKG linux-image-virtual 6.8.0-147.147 | PKG openssh-server 1:9.6p1-3ubuntu13.20 | PKG ufw 0.36.2-6 42. ok ufw reset asks, backs up the rule files with a date, and leaves ufw inactive | Resetting all rules to installed defaults. This may disrupt existing ssh | connections. Proceed with operation (y|n)? y | Backing up 'user.rules' to '/etc/ufw/user.rules.20261111_164556' | Backing up 'before.rules' to '/etc/ufw/before.rules.20261111_164556' | Backing up 'after.rules' to '/etc/ufw/after.rules.20261111_164556' | Backing up 'user6.rules' to '/etc/ufw/user6.rules.20261111_164556' | Backing up 'before6.rules' to '/etc/ufw/before6.rules.20261111_164556' | Backing up 'after6.rules' to '/etc/ufw/after6.rules.20261111_164556' 43. ok after reset: Status: inactive and no rules | Status: inactive 44. ok (setup) a terminal is open before the mistake 45. ok ufw enable with no SSH rule still succeeds (the warning is the only guard) | Command may disrupt existing ssh connections. Proceed with operation (y|n)? y | Firewall is active and enabled on system startup 46. ok a NEW SSH login is now refused 47. ok the terminal that was already open keeps working 48. ok from that open terminal, ufw allow OpenSSH adds the rule to the running firewall: "Rule added" | Rule added | Rule added (v6) 49. ok new logins work again 50. ok ufw disable: "Firewall stopped and disabled on system startup" | Firewall stopped and disabled on system startup 51. ok port 80 answers again with the firewall off ------------------------------------------------------------------------------ Commands and what they printed (32) ------------------------------------------------------------------------------ $ sudo ufw status Status: inactive $ sudo ss -tlnp | grep ':22 ' LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1003,fd=3),("systemd",pid=1,fd=59)) LISTEN 0 4096 [::]:22 [::]:* users:(("sshd",pid=1003,fd=4),("systemd",pid=1,fd=60)) $ grep '^DEFAULT_.*POLICY' /etc/default/ufw DEFAULT_INPUT_POLICY="DROP" DEFAULT_OUTPUT_POLICY="ACCEPT" DEFAULT_FORWARD_POLICY="DROP" DEFAULT_APPLICATION_POLICY="SKIP" $ sudo ufw app list Available applications: OpenSSH $ sudo ufw app info OpenSSH Profile: OpenSSH Title: Secure shell server, an rshd replacement Description: OpenSSH is a free implementation of the Secure Shell protocol. Port: 22/tcp $ sudo ufw allow OpenSSH Rules updated Rules updated (v6) $ sudo ufw show added Added user rules (see 'ufw status' for running firewall): ufw allow OpenSSH $ sudo ufw enable Command may disrupt existing ssh connections. Proceed with operation (y|n)? y Firewall is active and enabled on system startup $ echo still here still here $ sudo ufw status verbose Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), disabled (routed) New profiles: skip To Action From -- ------ ---- 22/tcp (OpenSSH) ALLOW IN Anywhere 22/tcp (OpenSSH (v6)) ALLOW IN Anywhere (v6) $ sudo grep 'DPT=80 ' /var/log/ufw.log | tail -n 1 2026-11-11T16:44:25.202430+00:00 web-server kernel: [UFW BLOCK] IN=enp0s2 OUT= MAC=52:54:00:12:34:56:52:55:0a:00:02:02:08:00 SRC=203.0.113.1 DST=203.0.113.10 LEN=44 TOS=0x00 PREC=0x00 TTL=64 ID=549 PROTO=TCP SPT=54270 DPT=80 WINDOW=65535 RES=0x00 SYN URGP=0 $ sudo ufw allow 80/tcp Rule added Rule added (v6) $ sudo ufw allow 443/tcp Rule added Rule added (v6) $ sudo ufw allow 80/tcp Skipping adding existing rule Skipping adding existing rule (v6) $ sudo ufw status numbered Status: active To Action From -- ------ ---- [ 1] OpenSSH ALLOW IN Anywhere [ 2] 80/tcp ALLOW IN Anywhere [ 3] 443/tcp ALLOW IN Anywhere [ 4] OpenSSH (v6) ALLOW IN Anywhere (v6) [ 5] 80/tcp (v6) ALLOW IN Anywhere (v6) [ 6] 443/tcp (v6) ALLOW IN Anywhere (v6) $ sudo ufw delete allow 443/tcp Rule deleted Rule deleted (v6) $ sudo ufw status numbered Status: active To Action From -- ------ ---- [ 1] OpenSSH ALLOW IN Anywhere [ 2] 80/tcp ALLOW IN Anywhere [ 3] OpenSSH (v6) ALLOW IN Anywhere (v6) [ 4] 80/tcp (v6) ALLOW IN Anywhere (v6) $ sudo ufw delete 2 Deleting: allow 80/tcp Proceed with operation (y|n)? y Rule deleted $ sudo ufw status numbered Status: active To Action From -- ------ ---- [ 1] OpenSSH ALLOW IN Anywhere [ 2] OpenSSH (v6) ALLOW IN Anywhere (v6) [ 3] 80/tcp (v6) ALLOW IN Anywhere (v6) $ sudo ufw delete allow 80/tcp Could not delete non-existent rule Rule deleted (v6) $ sudo ufw limit OpenSSH Rule updated Rule updated (v6) $ sudo iptables -S ufw-user-input | grep -- '--dport 22' -A ufw-user-input -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -m recent --set --name DEFAULT --mask 255.255.255.255 --rsource -m comment --comment "\'dapp_OpenSSH\'" -A ufw-user-input -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 30 --hitcount 6 --name DEFAULT --mask 255.255.255.255 --rsource -m comment --comment "\'dapp_OpenSSH\'" -j ufw-user-limit -A ufw-user-input -p tcp -m tcp --dport 22 -m comment --comment "\'dapp_OpenSSH\'" -j ufw-user-limit-accept $ sudo grep -c 'UFW LIMIT BLOCK' /var/log/ufw.log 3 $ sudo ufw allow OpenSSH Rule updated Rule updated (v6) $ sudo ufw status Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere 80/tcp ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) 80/tcp (v6) ALLOW Anywhere (v6) $ sudo grep -n 'ctstate RELATED,ESTABLISHED -j ACCEPT' /etc/ufw/before.rules | head -n 1 25:-A ufw-before-input -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT $ sudo ufw reset Resetting all rules to installed defaults. This may disrupt existing ssh connections. Proceed with operation (y|n)? y Backing up 'user.rules' to '/etc/ufw/user.rules.20261111_164556' Backing up 'before.rules' to '/etc/ufw/before.rules.20261111_164556' Backing up 'after.rules' to '/etc/ufw/after.rules.20261111_164556' Backing up 'user6.rules' to '/etc/ufw/user6.rules.20261111_164556' Backing up 'before6.rules' to '/etc/ufw/before6.rules.20261111_164556' Backing up 'after6.rules' to '/etc/ufw/after6.rules.20261111_164556' $ sudo ufw status Status: inactive $ sudo ufw enable Command may disrupt existing ssh connections. Proceed with operation (y|n)? y Firewall is active and enabled on system startup $ echo still here still here $ sudo ufw allow OpenSSH Rule added Rule added (v6) $ sudo ufw disable Firewall stopped and disabled on system startup ------------------------------------------------------------------------------ The check's source (scripts/live/checks/ufw-firewall-ubuntu-24-04.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB: Set up a UFW firewall on Ubuntu 24.04 (without locking yourself out). * * The server is a fresh VM box (real kernel, real netfilter) reached as the sudo user `deploy`. "From outside" means * the laptop: the VM's port 80 is forwarded to 127.0.0.1:PROBE_PORT and its SSH to 127.0.0.1:2297, so every probe is a * real packet arriving on the server's network card, which ufw drops or lets through. The probes are not shown on the * page (through qemu's user network the laptop's own error text differs from what a reader's computer prints); what * the page shows is what the SERVER printed and logged. * * "Your open terminal" is an SSH ControlMaster connection opened before the firewall changes: commands sent over it * travel on that one established TCP connection, exactly like typing in a terminal you already have open, while every * other runner command is a NEW login. That is how the check proves both "the open session survives" and "a new login * is refused". Every claim below is one the article makes. */ import { spawnSync } from 'node:child_process' import { mkdtempSync } from 'node:fs' import { tmpdir } from 'node:os' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import { rebootBox, redact, screen, sshOpts } from '../box.mjs' import { PROBE_PORT } from '../box/vm.mjs' import { bareBox, reportBox } from '../lib.mjs' export const meta = {} const here = dirname(fileURLToPath(import.meta.url)) const sleep = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms) const sh = (cmd, args, opts = {}) => spawnSync(cmd, args, { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, ...opts }) /** An HTTP request to the server's port 80 from outside. true = it answered. */ const web = () => sh('curl', ['-s', '-o', '/dev/null', '-m', '6', '-w', '%{http_code}', `http://127.0.0.1:${PROBE_PORT}/`]).stdout === '200' /** A NEW ssh login from outside (no ControlMaster). true = it got in. */ const newLogin = () => sh('ssh', ['-o', 'BatchMode=yes', '-o', 'ConnectTimeout=6', '-o', 'ControlPath=none', ...sshOpts(), 'deploy@127.0.0.1', 'true'], { timeout: 20000 }).status === 0 /** The reader's already-open terminal: one SSH connection kept open, every command multiplexed over it. */ function openTerminal(ctx) { const sock = join(mkdtempSync(join(tmpdir(), 'shb-ufw-')), 'master') const opts = [...sshOpts(), '-o', `ControlPath=${sock}`] const up = sh('ssh', ['-o', 'BatchMode=yes', '-o', 'ControlMaster=yes', '-o', 'ControlPersist=yes', '-fN', ...opts, 'deploy@127.0.0.1'], { timeout: 30000 }) const clean = (raw) => redact(screen(raw)).replace(/\s+$/, '') const parse = (raw) => { const m = raw.match(/\n?__exit=(\d+)$/) return { out: (m ? raw.slice(0, m.index) : raw).replace(/\s+$/, ''), exit: m ? Number(m[1]) : -1 } } const line = (cmd) => { const b64 = Buffer.from(`${cmd}; echo "__exit=$?"`).toString('base64') return ['ssh', '-tt', ...opts, 'deploy@127.0.0.1', `'bash -lc "$(echo ${b64} | base64 -d)"'`].join(' ') } const run = (cmd, { id } = {}) => { const r = parse(clean(sh('bash', ['-c', line(cmd)], { timeout: 120000 }).stdout ?? '')) ctx.capture(cmd, r.out, { id }) return r } const ask = (cmd, answers, { id } = {}) => { const p = sh('python3', ['-I', join(here, '..', 'box', 'pty-run.py'), line(cmd), JSON.stringify(answers), '90'], { timeout: 150000 }) const r = parse(clean(p.stdout ?? '')) ctx.capture(cmd, r.out, { id }) return r } const close = () => sh('ssh', [...opts, '-O', 'exit', 'deploy@127.0.0.1']) return { ok: up.status === 0, err: `${up.stderr ?? ''}`, run, ask, close } } const check = (ctx) => bareBox(ctx, async ({ sudoUser }) => { const { run, ask, L } = sudoUser() // a test web server on port 80 (the reader's would be nginx or Apache), so "port 80 open/closed" is observable L(`sudo -n sh -c 'nohup python3 -m http.server 80 --directory /tmp >/dev/null 2>&1 &' ; sleep 1`) ctx.check('(setup) the test web server on port 80 answers from outside before any firewall', web(), '') // ── before you start ───────────────────────────────────────────────────────────────────── const st = run('sudo ufw status', { id: 'status-start' }) ctx.check('ufw is installed on a fresh server but inactive', /^Status: inactive$/m.test(st.out), st.out) const port = run(`sudo ss -tlnp | grep ':22 '`, { id: 'ssh-port' }) ctx.check('SSH listens on port 22 (the port the OpenSSH profile opens)', /0\.0\.0\.0:22\b/.test(port.out) && /sshd/.test(port.out), port.out) const defs = run(`grep '^DEFAULT_.*POLICY' /etc/default/ufw`, { id: 'defaults' }) ctx.check('the defaults are already deny incoming (DROP), allow outgoing (ACCEPT)', /DEFAULT_INPUT_POLICY="DROP"/.test(defs.out) && /DEFAULT_OUTPUT_POLICY="ACCEPT"/.test(defs.out), defs.out) // ── step 1: allow SSH ──────────────────────────────────────────────────────────────────── const apps = run('sudo ufw app list', { id: 'app-list' }) ctx.check('ufw app list shows the OpenSSH profile', /^\s+OpenSSH$/m.test(apps.out), apps.out) const info = run('sudo ufw app info OpenSSH', { id: 'app-info' }) ctx.check('the OpenSSH profile opens 22/tcp', /22\/tcp/.test(info.out), info.out) const allowSsh = run('sudo ufw allow OpenSSH', { id: 'allow-ssh' }) ctx.check('ufw allow OpenSSH adds the rule for IPv4 and IPv6 while ufw is still off', /^Rules updated$/m.test(allowSsh.out) && /^Rules updated \(v6\)$/m.test(allowSsh.out), allowSsh.out) const added = run('sudo ufw show added', { id: 'show-added' }) ctx.check('ufw show added lists the saved rule before the firewall is on', /ufw allow OpenSSH/.test(added.out), added.out) // ── step 2: enable, with a terminal already open ───────────────────────────────────────── const term = openTerminal(ctx) if (!ctx.check('(setup) the reader’s open terminal (one kept SSH connection) opens', term.ok, term.err)) return try { const en = ask('sudo ufw enable', [['Proceed with operation \\(y\\|n\\)\\?', 'y']], { id: 'enable' }) ctx.check('ufw enable warns "Command may disrupt existing ssh connections" and asks y/n', /Command may disrupt existing ssh connections\. Proceed with operation \(y\|n\)\?/.test(en.out), en.out) ctx.check('after y: "Firewall is active and enabled on system startup"', /Firewall is active and enabled on system startup/.test(en.out), en.out) ctx.check('the open terminal still works after enable', term.run('echo still here', { id: 'open-after-enable' }).out.endsWith('still here'), '') ctx.check('a NEW SSH login from outside still gets in (step 4’s second terminal)', newLogin(), '') const verbose = run('sudo ufw status verbose', { id: 'status-verbose' }) ctx.check('status verbose: active, deny incoming, allow outgoing, OpenSSH allowed for v4 and v6', /^Status: active$/m.test(verbose.out) && /Default: deny \(incoming\), allow \(outgoing\), disabled \(routed\)/.test(verbose.out) && /22\/tcp \(OpenSSH\)\s+ALLOW IN\s+Anywhere/.test(verbose.out) && /\(v6\)/.test(verbose.out), verbose.out) ctx.check('logging is on (low) by default', /^Logging: on \(low\)$/m.test(verbose.out), verbose.out) // ── a port you did not allow is closed, and ufw logs it ──────────────────────────────── ctx.check('port 80 no longer answers from outside: the web server is still running but ufw drops the packets', !web(), '') const listen = L(`sudo ss -tln | grep -c ':80 '`) ctx.check('(the test web server is still listening on port 80)', /^[1-9]/.test(listen.split('\n').pop()), listen) const blk = run(`sudo grep 'DPT=80 ' /var/log/ufw.log | tail -n 1`, { id: 'blocked-log' }) ctx.check('/var/log/ufw.log records the drop: [UFW BLOCK] … DPT=80 … SYN', /\[UFW BLOCK\].*PROTO=TCP.*DPT=80 .*SYN/.test(blk.out), blk.out) // ── step 3: open the web ports ──────────────────────────────────────────────────────── const a80 = run('sudo ufw allow 80/tcp', { id: 'allow-80' }) ctx.check('ufw allow 80/tcp: "Rule added" and "Rule added (v6)"', /^Rule added$/m.test(a80.out) && /^Rule added \(v6\)$/m.test(a80.out), a80.out) const a443 = run('sudo ufw allow 443/tcp', { id: 'allow-443' }) ctx.check('ufw allow 443/tcp adds both rules', /^Rule added$/m.test(a443.out), a443.out) ctx.check('port 80 answers from outside again at once, no reload needed', web(), '') const again = run('sudo ufw allow 80/tcp', { id: 'allow-80-again' }) ctx.check('adding the same rule twice is harmless: "Skipping adding existing rule"', /Skipping adding existing rule/.test(again.out), again.out) // ── deleting rules ───────────────────────────────────────────────────────────────────── const num = run('sudo ufw status numbered', { id: 'numbered' }) ctx.check('status numbered lists IPv4 and IPv6 rules as separate numbers', /\[ 1\] OpenSSH/.test(num.out) && /443\/tcp \(v6\)/.test(num.out), num.out) const delByRule = run('sudo ufw delete allow 443/tcp', { id: 'delete-rule' }) ctx.check('ufw delete allow 443/tcp removes the IPv4 AND the IPv6 rule', /^Rule deleted$/m.test(delByRule.out) && /^Rule deleted \(v6\)$/m.test(delByRule.out), delByRule.out) const n2 = run('sudo ufw status numbered', { id: 'numbered-2' }) const v4num = (n2.out.match(/\[\s*(\d+)\] 80\/tcp\s+ALLOW IN\s+Anywhere\s*$/m) ?? [])[1] ctx.check('(found the number of the IPv4 80/tcp rule)', Boolean(v4num), n2.out) const delNum = ask(`sudo ufw delete ${v4num}`, [['Proceed with operation \\(y\\|n\\)\\?', 'y']], { id: 'delete-number' }) ctx.check('ufw delete asks first, then deletes ONE rule', /Deleting:\s*\n?\s*allow 80\/tcp/.test(delNum.out) && /^Rule deleted$/m.test(delNum.out), delNum.out) const n3 = run('sudo ufw status numbered', { id: 'numbered-3' }) ctx.check('…the IPv6 80/tcp rule is still there, and the numbers below it moved up', /80\/tcp \(v6\)\s+ALLOW IN/.test(n3.out) && !/80\/tcp\s+ALLOW IN\s+Anywhere\s*$/m.test(n3.out), n3.out) const rest = run('sudo ufw delete allow 80/tcp', { id: 'delete-80-rest' }) ctx.check('deleting by rule text then removes the IPv6 leftover ("Could not delete non-existent rule" for the IPv4 one)', /Could not delete non-existent rule/.test(rest.out) && /^Rule deleted \(v6\)$/m.test(rest.out), rest.out) L('sudo ufw allow 80/tcp >/dev/null') // back on for the rest of the run // ── rate-limit SSH ───────────────────────────────────────────────────────────────────── const lim = run('sudo ufw limit OpenSSH', { id: 'limit' }) ctx.check('ufw limit OpenSSH turns the allow rule into a limit rule: "Rule updated"', /^Rule updated$/m.test(lim.out) && /^Rule updated \(v6\)$/m.test(lim.out), lim.out) // from here every NEW login counts; the checks below go through the open terminal (no new connection) const rule = term.run(`sudo iptables -S ufw-user-input | grep -- '--dport 22'`, { id: 'limit-rule' }) ctx.check('the limit rule is the kernel "recent" match: 6 new connections in 30 seconds from one address', /--update --seconds 30 --hitcount 6/.test(rule.out) && /--set/.test(rule.out), rule.out) sleep(31000) // the logins made before the limit rule are out of the 30-second window const tries = [] for (let i = 0; i < 7; i++) tries.push(newLogin()) ctx.check('with the limit on, 5 new logins in a row get in and the 6th and 7th are refused', tries.slice(0, 5).every(Boolean) && !tries[5] && !tries[6], tries.join(' ')) const lb = term.run(`sudo grep -c 'UFW LIMIT BLOCK' /var/log/ufw.log`, { id: 'limit-log' }) ctx.check('the refused logins are logged as [UFW LIMIT BLOCK]', Number(lb.out.split('\n').pop()) > 0, lb.out) sleep(10000) ctx.check('10 seconds later, while the address keeps trying, it is still refused', !newLogin(), '') const unlim = term.run('sudo ufw allow OpenSSH', { id: 'unlimit' }) ctx.check('ufw allow OpenSSH turns the limit back into a plain allow: "Rule updated"', /^Rule updated$/m.test(unlim.out), unlim.out) ctx.check('a new login gets in at once after that', newLogin(), '') } finally { term.close() } // ── it survives a reboot ───────────────────────────────────────────────────────────────── const secs = rebootBox() ctx.check('the server reboots and answers over SSH again (port 22 allowed)', typeof secs === 'number', String(secs)) const after = run('sudo ufw status', { id: 'after-reboot' }) // the test web server is a plain process, not a service: start it again after the reboot L(`sudo -n sh -c 'nohup python3 -m http.server 80 --directory /tmp >/dev/null 2>&1 &' ; sleep 1`) ctx.check('port 80 answers from outside after the reboot (its rule was kept)', web(), '') ctx.check('after a reboot ufw is active with the same rules', /^Status: active$/m.test(after.out) && /OpenSSH\s+ALLOW/.test(after.out) && /80\/tcp\s+ALLOW/.test(after.out), after.out) // why an open session survives enable: ufw's own before.rules accepts established connections first const est = run(`sudo grep -n 'ctstate RELATED,ESTABLISHED -j ACCEPT' /etc/ufw/before.rules | head -n 1`, { id: 'established' }) ctx.check('/etc/ufw/before.rules accepts RELATED,ESTABLISHED traffic before any user rule', /ufw-before-input -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT/.test(est.out), est.out) reportBox(ctx, L, 'A fresh Ubuntu 24.04 server as a provider hands it over, reached over SSH as a sudo user.', ['ufw', 'iptables', 'openssh-server', 'linux-image-virtual']) // ── undo: reset ────────────────────────────────────────────────────────────────────────── const reset = ask('sudo ufw reset', [['Proceed with operation \\(y\\|n\\)\\?', 'y']], { id: 'reset' }) ctx.check('ufw reset asks, backs up the rule files with a date, and leaves ufw inactive', /Resetting all rules to installed defaults/.test(reset.out) && /Backing up 'user\.rules' to '\/etc\/ufw\/user\.rules\.\d{8}_\d{6}'/.test(reset.out), reset.out) const st2 = run('sudo ufw status', { id: 'status-after-reset' }) ctx.check('after reset: Status: inactive and no rules', /^Status: inactive$/m.test(st2.out), st2.out) // ── if you enabled ufw BEFORE allowing SSH ─────────────────────────────────────────────── const t2 = openTerminal(ctx) if (!ctx.check('(setup) a terminal is open before the mistake', t2.ok, t2.err)) return try { const bad = t2.ask('sudo ufw enable', [['Proceed with operation \\(y\\|n\\)\\?', 'y']], { id: 'enable-no-ssh' }) ctx.check('ufw enable with no SSH rule still succeeds (the warning is the only guard)', /Firewall is active and enabled on system startup/.test(bad.out), bad.out) ctx.check('a NEW SSH login is now refused', !newLogin(), '') ctx.check('the terminal that was already open keeps working', t2.run('echo still here', { id: 'open-after-mistake' }).out.endsWith('still here'), '') const fix = t2.run('sudo ufw allow OpenSSH', { id: 'fix-from-open' }) ctx.check('from that open terminal, ufw allow OpenSSH adds the rule to the running firewall: "Rule added"', /^Rule added$/m.test(fix.out), fix.out) sleep(2000) ctx.check('new logins work again', newLogin(), '') } finally { t2.close() } // ── undo: turn it off ──────────────────────────────────────────────────────────────────── const dis = run('sudo ufw disable', { id: 'disable' }) ctx.check('ufw disable: "Firewall stopped and disabled on system startup"', /Firewall stopped and disabled on system startup/.test(dis.out), dis.out) ctx.check('port 80 answers again with the firewall off', web(), '') }) export default check