ServerHandbook live check: run log ------------------------------------------------------------------------------ Check scripts/live/checks/systemd-service-ubuntu.mjs (sha256 0c417663cf7f8a04845d52ad99987e773a718064116895692ceca0c7b595a453) Ran at 2026-11-19T21:12:56Z (76 s) Result PASS, all 36 claims held Tested on System Ubuntu 26.04.1 LTS Kernel 7.0.0-43-generic Package systemd 259.9-0ubuntu0.1 Package python3 3.14.3-0ubuntu2 Setup A fresh Ubuntu 26.04 server (a VM from Ubuntu’s cloud image) reached over SSH as the sudo user deploy; the service is a small Python 3 web app from Ubuntu’s own python3 package, nothing downloaded. 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 (1) hello.service Services disabled (0) Listening sockets opened (1) tcp 127.0.0.1:8001 python3 Listening sockets closed (0) Files added in /etc (3) /etc/systemd/system/hello.service /etc/systemd/system/hello.service.d/hardening.conf /etc/systemd/system/multi-user.target.wants/hello.service Files changed in /etc (8) /etc/group /etc/group- /etc/gshadow /etc/gshadow- /etc/passwd /etc/passwd- /etc/shadow /etc/shadow- Files removed from /etc (0) Disk used on / (includes logs written during the run): +0.5 MB ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Claims (36), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 26.04 server box (web-server) starts 2. ok the app file is written | import http.server, os | | PORT = int(os.environ.get('PORT', '8000')) | | class Hello(http.server.BaseHTTPRequestHandler): | def do_GET(self): | self.send_response(200) | self.end_headers() | self.wfile.write(b'hello from a systemd service\n') | | print(f'listening on 127.0.0.1:{PORT}', flush=True) | http.server.HTTPServer(('127.0.0.1', PORT), Hello).serve_forever() 3. ok a fresh Ubuntu 26.04 server has python3 at /usr/bin/python3, and no "python" command | /usr/bin/python3 4. ok useradd --system creates the user and prints nothing 5. ok id hello: a system user (uid below 1000) with its own group | uid=999(hello) gid=986(hello) groups=986(hello) 6. ok tee writes the unit file and prints it back | [Unit] | Description=Hello web app | After=network.target | | [Service] | User=hello | Group=hello | WorkingDirectory=/opt/hello | Environment=PORT=8000 | ExecStart=/usr/bin/python3 /opt/hello/app.py | Restart=on-failure | RestartSec=2 | | [Install] | WantedBy=multi-user.target 7. ok systemd-analyze verify finds nothing wrong with it (no output about hello.service) | /usr/lib/systemd/system/xfs_scrub_all.service:26: Support for option CPUAccounting= has been removed and it is ignored | /usr/lib/systemd/system/system-xfs_scrub.slice:15: Support for option CPUAccounting= has been removed and it is ignored 8. ok enable --now creates the boot link: "Created symlink … multi-user.target.wants/hello.service" | Created symlink '/etc/systemd/system/multi-user.target.wants/hello.service' → '/etc/systemd/system/hello.service'. 9. ok status: loaded, enabled, active (running), the python3 process under hello.service | ● hello.service - Hello web app | Loaded: loaded (/etc/systemd/system/hello.service; enabled; preset: enabled) | Active: active (running) since Thu 2026-11-19 21:13:22 UTC; 2s ago | Invocation: 98138d3c198b463bb392eb1d94128014 | Main PID: 2881 (python3) | Tasks: 1 (limit: 1486) | Memory: 9.1M (peak: 9.3M) | CPU: 45ms | CGroup: /system.slice/hello.service | └─2881 /usr/bin/python3 /opt/hello/app.py 10. ok the app answers on port 8000 | hello from a systemd service 11. ok it runs as the user hello, not root | hello /usr/bin/python3 /opt/hello/app.py 12. ok journalctl -u hello shows the app's own print and the request it served | Nov 19 21:13:22 web-server systemd[1]: Started hello.service - Hello web app. | Nov 19 21:13:22 web-server python3[2881]: listening on 127.0.0.1:8000 | Nov 19 21:13:24 web-server python3[2881]: 127.0.0.1 - - [19/Nov/2026 21:13:24] "GET / HTTP/1.1" 200 - 13. ok the journal shows the kill (status=9/KILL) and "Scheduled restart job, restart counter is at 1" | Nov 19 21:13:26 web-server systemd[1]: hello.service: Killed unit cgroup '/system.slice/hello.service' with SIGKILL on client request. | Nov 19 21:13:26 web-server systemd[1]: hello.service: Main process exited, code=killed, status=9/KILL | Nov 19 21:13:26 web-server systemd[1]: hello.service: Failed with result 'signal'. | Nov 19 21:13:28 web-server systemd[1]: hello.service: Scheduled restart job, restart counter is at 1. | Nov 19 21:13:28 web-server systemd[1]: Started hello.service - Hello web app. | Nov 19 21:13:28 web-server python3[3392]: listening on 127.0.0.1:8000 14. ok Restart=on-failure brought it back by itself, as a new process | 2881 | > → 3392 | > | active 15. ok until daemon-reload, status warns the unit file "changed on disk" and to run daemon-reload | Warning: The unit file, source configuration file or drop-ins of hello.service changed on disk. Run 'systemctl daemon-reload' to reload units. 16. ok daemon-reload + restart 17. ok the app now answers on port 8001 | hello from a systemd service 18. ok (the broken unit says python, not python3) | ExecStart=/usr/bin/python /opt/hello/app.py 19. ok status shows "failed" and "status=203/EXEC" | × broken.service | Loaded: loaded (/etc/systemd/system/broken.service; static) | Active: failed (Result: exit-code) since Thu 2026-11-19 21:13:37 UTC; 2s ago | Duration: 9ms | Invocation: b09ce44840724079b8242191d02d3231 | Process: 4420 ExecStart=/usr/bin/python /opt/hello/app.py (code=exited, status=203/EXEC) | Main PID: 4420 (code=exited, status=203/EXEC) | Mem peak: 1.9M | CPU: 5ms 20. ok the journal records status=203/EXEC | Nov 19 21:13:37 web-server (python)[4420]: broken.service: Unable to locate executable '/usr/bin/python': No such file or directory | Nov 19 21:13:37 web-server (python)[4420]: broken.service: Failed at step EXEC spawning /usr/bin/python: No such file or directory | Nov 19 21:13:37 web-server systemd[1]: broken.service: Main process exited, code=exited, status=203/EXEC | Nov 19 21:13:37 web-server systemd[1]: broken.service: Failed with result 'exit-code'. 21. ok ls -l on the ExecStart path: "No such file or directory" | ls: cannot access '/usr/bin/python': No such file or directory 22. ok (the broken unit now has a mistyped user) | User=helo 23. ok status shows "status=217/USER" | × broken.service | Loaded: loaded (/etc/systemd/system/broken.service; static) | Active: failed (Result: exit-code) since Thu 2026-11-19 21:13:42 UTC; 2s ago | Duration: 8ms | Invocation: 2dd43a45643f4ba981178db14f445d6e | Process: 4982 ExecStart=/usr/bin/python3 /opt/hello/app.py (code=exited, status=217/USER) | Main PID: 4982 (code=exited, status=217/USER) | Mem peak: 2M | CPU: 5ms 24. ok the journal records status=217/USER | Nov 19 21:13:42 web-server (python3)[4982]: broken.service: Failed to determine credentials for user 'helo': Unknown user | Nov 19 21:13:42 web-server (python3)[4982]: broken.service: Failed at step USER spawning /usr/bin/python3: Invalid argument | Nov 19 21:13:42 web-server systemd[1]: broken.service: Main process exited, code=exited, status=217/USER | Nov 19 21:13:42 web-server systemd[1]: broken.service: Failed with result 'exit-code'. 25. ok id on the User= name: "no such user" | id: 'helo': no such user 26. ok systemd-analyze security rates the plain unit (an exposure level and a word) | → Overall exposure level for hello.service: 9.2 UNSAFE 😨 27. ok the hardening drop-in is written | [Service] | NoNewPrivileges=yes | ProtectSystem=strict | ProtectHome=yes | PrivateTmp=yes | PrivateDevices=yes | ProtectKernelTunables=yes | ProtectKernelModules=yes | ProtectControlGroups=yes | RestrictSUIDSGID=yes | CapabilityBoundingSet= 28. ok the exposure level drops after the drop-in | 9.2 → 4.8 | → Overall exposure level for hello.service: 4.8 OK 🙂 29. ok the app still answers with the hardening on | hello from a systemd service 30. ok systemctl cat lists the unit file and the drop-in it reads | # /etc/systemd/system/hello.service | # /etc/systemd/system/hello.service.d/hardening.conf 31. ok the server comes back after the reboot | 14 32. ok after the reboot hello is active and enabled: it started by itself | active | enabled 33. ok and answers | hello from a systemd service 34. ok (tested on) the box reports its OS, kernel and the version of 2 package(s) | PRETTY_NAME="Ubuntu 26.04.1 LTS" | VIRT=none | KERNEL=7.0.0-43-generic | PKG python3 3.14.3-0ubuntu2 | PKG systemd 259.9-0ubuntu0.1 35. ok disable --now removes the boot link: "Removed …hello.service" | Removed '/etc/systemd/system/multi-user.target.wants/hello.service'. 36. ok systemctl status: "Unit hello.service could not be found." | Unit hello.service could not be found. ------------------------------------------------------------------------------ Commands and what they printed (44) ------------------------------------------------------------------------------ $ sudo mkdir -p /opt/hello $ sudo tee /opt/hello/app.py > /dev/null <<'EOF' $ import http.server, os $ $ PORT = int(os.environ.get('PORT', '8000')) $ $ class Hello(http.server.BaseHTTPRequestHandler): $ def do_GET(self): $ self.send_response(200) $ self.end_headers() $ self.wfile.write(b'hello from a systemd service\n') $ $ print(f'listening on 127.0.0.1:{PORT}', flush=True) $ http.server.HTTPServer(('127.0.0.1', PORT), Hello).serve_forever() $ EOF $ command -v python3 python /usr/bin/python3 $ sudo useradd --system --no-create-home --shell /usr/sbin/nologin hello $ id hello uid=999(hello) gid=986(hello) groups=986(hello) $ sudo tee /etc/systemd/system/hello.service <<'EOF' $ [Unit] $ Description=Hello web app $ After=network.target $ $ [Service] $ User=hello $ Group=hello $ WorkingDirectory=/opt/hello $ Environment=PORT=8000 $ ExecStart=/usr/bin/python3 /opt/hello/app.py $ Restart=on-failure $ RestartSec=2 $ $ [Install] $ WantedBy=multi-user.target $ EOF [Unit] Description=Hello web app After=network.target [Service] User=hello Group=hello WorkingDirectory=/opt/hello Environment=PORT=8000 ExecStart=/usr/bin/python3 /opt/hello/app.py Restart=on-failure RestartSec=2 [Install] WantedBy=multi-user.target $ sudo systemd-analyze verify /etc/systemd/system/hello.service /usr/lib/systemd/system/xfs_scrub_all.service:26: Support for option CPUAccounting= has been removed and it is ignored /usr/lib/systemd/system/system-xfs_scrub.slice:15: Support for option CPUAccounting= has been removed and it is ignored $ sudo systemctl daemon-reload $ sudo systemctl enable --now hello Created symlink '/etc/systemd/system/multi-user.target.wants/hello.service' → '/etc/systemd/system/hello.service'. $ systemctl status hello --no-pager ● hello.service - Hello web app Loaded: loaded (/etc/systemd/system/hello.service; enabled; preset: enabled) Active: active (running) since Thu 2026-11-19 21:13:22 UTC; 2s ago Invocation: 98138d3c198b463bb392eb1d94128014 Main PID: 2881 (python3) Tasks: 1 (limit: 1486) Memory: 9.1M (peak: 9.3M) CPU: 45ms CGroup: /system.slice/hello.service └─2881 /usr/bin/python3 /opt/hello/app.py $ curl -s http://127.0.0.1:8000/ hello from a systemd service $ ps -o user=,cmd= -C python3 hello /usr/bin/python3 /opt/hello/app.py $ sudo journalctl -u hello --no-pager -n 5 Nov 19 21:13:22 web-server systemd[1]: Started hello.service - Hello web app. Nov 19 21:13:22 web-server python3[2881]: listening on 127.0.0.1:8000 Nov 19 21:13:24 web-server python3[2881]: 127.0.0.1 - - [19/Nov/2026 21:13:24] "GET / HTTP/1.1" 200 - $ sudo systemctl kill -s KILL hello $ sudo journalctl -u hello --no-pager -n 6 Nov 19 21:13:26 web-server systemd[1]: hello.service: Killed unit cgroup '/system.slice/hello.service' with SIGKILL on client request. Nov 19 21:13:26 web-server systemd[1]: hello.service: Main process exited, code=killed, status=9/KILL Nov 19 21:13:26 web-server systemd[1]: hello.service: Failed with result 'signal'. Nov 19 21:13:28 web-server systemd[1]: hello.service: Scheduled restart job, restart counter is at 1. Nov 19 21:13:28 web-server systemd[1]: Started hello.service - Hello web app. Nov 19 21:13:28 web-server python3[3392]: listening on 127.0.0.1:8000 $ systemctl is-active hello active $ sudo sed -i 's/PORT=8000/PORT=8001/' /etc/systemd/system/hello.service $ systemctl status hello --no-pager | grep -i warning Warning: The unit file, source configuration file or drop-ins of hello.service changed on disk. Run 'systemctl daemon-reload' to reload units. $ sudo systemctl daemon-reload && sudo systemctl restart hello $ curl -s http://127.0.0.1:8001/ hello from a systemd service $ sudo sed -n '/ExecStart/p' /etc/systemd/system/broken.service ExecStart=/usr/bin/python /opt/hello/app.py $ sudo systemctl start broken $ systemctl status broken --no-pager × broken.service Loaded: loaded (/etc/systemd/system/broken.service; static) Active: failed (Result: exit-code) since Thu 2026-11-19 21:13:37 UTC; 2s ago Duration: 9ms Invocation: b09ce44840724079b8242191d02d3231 Process: 4420 ExecStart=/usr/bin/python /opt/hello/app.py (code=exited, status=203/EXEC) Main PID: 4420 (code=exited, status=203/EXEC) Mem peak: 1.9M CPU: 5ms $ sudo journalctl -u broken --no-pager -n 4 Nov 19 21:13:37 web-server (python)[4420]: broken.service: Unable to locate executable '/usr/bin/python': No such file or directory Nov 19 21:13:37 web-server (python)[4420]: broken.service: Failed at step EXEC spawning /usr/bin/python: No such file or directory Nov 19 21:13:37 web-server systemd[1]: broken.service: Main process exited, code=exited, status=203/EXEC Nov 19 21:13:37 web-server systemd[1]: broken.service: Failed with result 'exit-code'. $ ls -l /usr/bin/python ls: cannot access '/usr/bin/python': No such file or directory $ sudo sed -n '/User=/p' /etc/systemd/system/broken.service User=helo $ sudo systemctl start broken $ systemctl status broken --no-pager × broken.service Loaded: loaded (/etc/systemd/system/broken.service; static) Active: failed (Result: exit-code) since Thu 2026-11-19 21:13:42 UTC; 2s ago Duration: 8ms Invocation: 2dd43a45643f4ba981178db14f445d6e Process: 4982 ExecStart=/usr/bin/python3 /opt/hello/app.py (code=exited, status=217/USER) Main PID: 4982 (code=exited, status=217/USER) Mem peak: 2M CPU: 5ms $ sudo journalctl -u broken --no-pager -n 4 Nov 19 21:13:42 web-server (python3)[4982]: broken.service: Failed to determine credentials for user 'helo': Unknown user Nov 19 21:13:42 web-server (python3)[4982]: broken.service: Failed at step USER spawning /usr/bin/python3: Invalid argument Nov 19 21:13:42 web-server systemd[1]: broken.service: Main process exited, code=exited, status=217/USER Nov 19 21:13:42 web-server systemd[1]: broken.service: Failed with result 'exit-code'. $ id helo id: 'helo': no such user $ systemd-analyze security hello --no-pager | tail -n 1 → Overall exposure level for hello.service: 9.2 UNSAFE 😨 $ sudo mkdir -p /etc/systemd/system/hello.service.d $ sudo tee /etc/systemd/system/hello.service.d/hardening.conf <<'EOF' $ [Service] $ NoNewPrivileges=yes $ ProtectSystem=strict $ ProtectHome=yes $ PrivateTmp=yes $ PrivateDevices=yes $ ProtectKernelTunables=yes $ ProtectKernelModules=yes $ ProtectControlGroups=yes $ RestrictSUIDSGID=yes $ CapabilityBoundingSet= $ EOF [Service] NoNewPrivileges=yes ProtectSystem=strict ProtectHome=yes PrivateTmp=yes PrivateDevices=yes ProtectKernelTunables=yes ProtectKernelModules=yes ProtectControlGroups=yes RestrictSUIDSGID=yes CapabilityBoundingSet= $ sudo systemctl daemon-reload && sudo systemctl restart hello $ systemd-analyze security hello --no-pager | tail -n 1 → Overall exposure level for hello.service: 4.8 OK 🙂 $ curl -s http://127.0.0.1:8001/ hello from a systemd service $ systemctl cat hello --no-pager | grep -E "^# /" # /etc/systemd/system/hello.service # /etc/systemd/system/hello.service.d/hardening.conf $ sudo reboot $ systemctl is-active hello && systemctl is-enabled hello active enabled $ curl -s http://127.0.0.1:8001/ hello from a systemd service $ sudo systemctl disable --now hello Removed '/etc/systemd/system/multi-user.target.wants/hello.service'. $ sudo rm -r /etc/systemd/system/hello.service /etc/systemd/system/hello.service.d && sudo systemctl daemon-reload $ systemctl status hello --no-pager Unit hello.service could not be found. $ sudo userdel hello && sudo rm -r /opt/hello ------------------------------------------------------------------------------ The check's source (scripts/live/checks/systemd-service-ubuntu.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB how-to: run a program as a systemd service on Ubuntu 26.04 and 24.04 (run per release, --releases=26.04,24.04). * A small Python web app (Python 3 is on every Ubuntu cloud image, so nothing is downloaded), its own system user, a * unit file, `systemd-analyze verify`, enable --now, the journal, a crash and the automatic restart, the "changed on * disk" warning, the two start errors people search (203/EXEC with `python` instead of `python3`, 217/USER with a * mistyped user), `systemd-analyze security` before and after a hardening drop-in, a reboot, and removing it. * The server is a fresh VM box from Ubuntu's cloud image, reached as the sudo user `deploy`. */ import { spawnSync } from 'node:child_process' import { rebootBox } from '../box.mjs' import { bareBox, reportBox } from '../lib.mjs' const RELEASE = process.env.LIVE_BOX_RELEASE || '24.04' const sleep = (s) => spawnSync('sleep', [String(s)]) const APP = [ 'import http.server, os', '', "PORT = int(os.environ.get('PORT', '8000'))", '', 'class Hello(http.server.BaseHTTPRequestHandler):', ' def do_GET(self):', ' self.send_response(200)', ' self.end_headers()', " self.wfile.write(b'hello from a systemd service\\n')", '', "print(f'listening on 127.0.0.1:{PORT}', flush=True)", "http.server.HTTPServer(('127.0.0.1', PORT), Hello).serve_forever()", ].join('\n') const UNIT = [ '[Unit]', 'Description=Hello web app', 'After=network.target', '', '[Service]', 'User=hello', 'Group=hello', 'WorkingDirectory=/opt/hello', 'Environment=PORT=8000', 'ExecStart=/usr/bin/python3 /opt/hello/app.py', 'Restart=on-failure', 'RestartSec=2', '', '[Install]', 'WantedBy=multi-user.target', ].join('\n') const HARDENING = [ '[Service]', 'NoNewPrivileges=yes', 'ProtectSystem=strict', 'ProtectHome=yes', 'PrivateTmp=yes', 'PrivateDevices=yes', 'ProtectKernelTunables=yes', 'ProtectKernelModules=yes', 'ProtectControlGroups=yes', 'RestrictSUIDSGID=yes', 'CapabilityBoundingSet=', ].join('\n') /** `→ Overall exposure level for hello.service: 9.2 UNSAFE` → 9.2 */ const exposure = (out) => Number((out.match(/Overall exposure level for \S+: (\d+(?:\.\d+)?)/) ?? [])[1] ?? Number.NaN) const check = (ctx) => bareBox(ctx, async ({ sudoUser }) => { const { run, L } = sudoUser() L('sudo true') // a reader has used sudo before, so Ubuntu's first-sudo hint is not on their screen // ── step 1: the program ────────────────────────────────────────────────────────────────── run('sudo mkdir -p /opt/hello', { id: 'mkdir' }) const app = run(`sudo tee /opt/hello/app.py > /dev/null <<'EOF'\n${APP}\nEOF`, { id: 'app' }) ctx.check('the app file is written', app.exit === 0 && L('cat /opt/hello/app.py') === APP, L('cat /opt/hello/app.py')) const py = run('command -v python3 python', { id: 'which-python' }) ctx.check(`a fresh Ubuntu ${RELEASE} server has python3 at /usr/bin/python3, and no "python" command`, /^\/usr\/bin\/python3$/m.test(py.out) && !/\/python$/m.test(py.out), py.out) // ── step 2: its own user ───────────────────────────────────────────────────────────────── const ua = run('sudo useradd --system --no-create-home --shell /usr/sbin/nologin hello', { id: 'useradd' }) ctx.check('useradd --system creates the user and prints nothing', ua.exit === 0 && ua.out === '', ua.out) const id = run('id hello', { id: 'id' }) ctx.check('id hello: a system user (uid below 1000) with its own group', /^uid=(\d+)\(hello\) gid=\d+\(hello\) groups=\d+\(hello\)$/.test(id.out) && Number(id.out.match(/uid=(\d+)/)[1]) < 1000, id.out) // ── step 3: the unit file ──────────────────────────────────────────────────────────────── const unit = run(`sudo tee /etc/systemd/system/hello.service <<'EOF'\n${UNIT}\nEOF`, { id: 'unit' }) ctx.check('tee writes the unit file and prints it back', unit.out === UNIT, unit.out) const ver = run('sudo systemd-analyze verify /etc/systemd/system/hello.service', { id: 'verify' }) ctx.check('systemd-analyze verify finds nothing wrong with it (no output about hello.service)', ver.exit === 0 && !/hello\.service/.test(ver.out), ver.out) // ── step 4: start it now and at every boot ─────────────────────────────────────────────── run('sudo systemctl daemon-reload', { id: 'daemon-reload' }) const en = run('sudo systemctl enable --now hello', { id: 'enable' }) ctx.check('enable --now creates the boot link: "Created symlink … multi-user.target.wants/hello.service"', /Created symlink.*multi-user\.target\.wants\/hello\.service/.test(en.out), en.out) sleep(2) const st = run('systemctl status hello --no-pager', { id: 'status' }) ctx.check('status: loaded, enabled, active (running), the python3 process under hello.service', /Loaded: loaded \(\/etc\/systemd\/system\/hello\.service; enabled/.test(st.out) && /Active: active \(running\)/.test(st.out) && /\/usr\/bin\/python3 \/opt\/hello\/app\.py/.test(st.out), st.out) const cu = run('curl -s http://127.0.0.1:8000/', { id: 'curl' }) ctx.check('the app answers on port 8000', cu.out === 'hello from a systemd service', cu.out) const who = run('ps -o user=,cmd= -C python3', { id: 'ps' }) ctx.check('it runs as the user hello, not root', /^hello\s+\/usr\/bin\/python3 \/opt\/hello\/app\.py$/m.test(who.out), who.out) // ── step 5: its output is in the journal ───────────────────────────────────────────────── const jr = run('sudo journalctl -u hello --no-pager -n 5', { id: 'journal' }) ctx.check('journalctl -u hello shows the app\'s own print and the request it served', /listening on 127\.0\.0\.1:8000/.test(jr.out) && /"GET \/ HTTP\/1\.1" 200/.test(jr.out), jr.out) // ── step 6: it restarts after a crash ──────────────────────────────────────────────────── const pid0 = L('systemctl show -p MainPID --value hello') run('sudo systemctl kill -s KILL hello', { id: 'kill' }) sleep(5) const jk = run('sudo journalctl -u hello --no-pager -n 6', { id: 'journal-crash' }) ctx.check('the journal shows the kill (status=9/KILL) and "Scheduled restart job, restart counter is at 1"', /status=9\/KILL/.test(jk.out) && /Scheduled restart job, restart counter is at 1/.test(jk.out), jk.out) const pid1 = L('systemctl show -p MainPID --value hello') const ac = run('systemctl is-active hello', { id: 'is-active-crash' }) ctx.check('Restart=on-failure brought it back by itself, as a new process', ac.out === 'active' && pid1 !== pid0 && pid1 !== '0', `${pid0} → ${pid1}\n${ac.out}`) // ── step 7: change it: the "changed on disk" warning ───────────────────────────────────── run("sudo sed -i 's/PORT=8000/PORT=8001/' /etc/systemd/system/hello.service", { id: 'sed-port' }) const warn = run('systemctl status hello --no-pager | grep -i warning', { id: 'changed-on-disk' }) ctx.check('until daemon-reload, status warns the unit file "changed on disk" and to run daemon-reload', /changed on disk/.test(warn.out) && /systemctl daemon-reload/.test(warn.out), warn.out) const rr = run('sudo systemctl daemon-reload && sudo systemctl restart hello', { id: 'reload-restart' }) ctx.check('daemon-reload + restart', rr.exit === 0, rr.out) sleep(2) const c1 = run('curl -s http://127.0.0.1:8001/', { id: 'curl-8001' }) ctx.check('the app now answers on port 8001', c1.out === 'hello from a systemd service', c1.out) // ── errors: 203/EXEC and 217/USER ─────────────────────────────────────────────────────── L(`sudo tee /etc/systemd/system/broken.service > /dev/null <<'EOF'\n[Service]\nUser=hello\nExecStart=/usr/bin/python /opt/hello/app.py\nEOF`) const b1 = run("sudo sed -n '/ExecStart/p' /etc/systemd/system/broken.service", { id: 'broken-exec-line' }) ctx.check('(the broken unit says python, not python3)', b1.out === 'ExecStart=/usr/bin/python /opt/hello/app.py', b1.out) L('sudo systemctl daemon-reload') run('sudo systemctl start broken', { id: 'start-broken' }) sleep(2) const s203 = run('systemctl status broken --no-pager', { id: 'status-203' }) ctx.check('status shows "failed" and "status=203/EXEC"', /Active: failed/.test(s203.out) && /status=203\/EXEC/.test(s203.out), s203.out) const j203 = run('sudo journalctl -u broken --no-pager -n 4', { id: 'journal-203' }) // the child's own "Unable to locate executable" line reaches the journal only sometimes (journald cannot always // tie a process that already exited to its unit: seen on both releases, 2026-11-20), so the page relies on status= ctx.check('the journal records status=203/EXEC', /status=203\/EXEC/.test(j203.out), j203.out) const lsp = run('ls -l /usr/bin/python', { id: 'ls-python' }) ctx.check('ls -l on the ExecStart path: "No such file or directory"', /No such file or directory/.test(lsp.out), lsp.out) L("sudo sed -i 's|/usr/bin/python |/usr/bin/python3 |; s/User=hello/User=helo/' /etc/systemd/system/broken.service && sudo systemctl daemon-reload && sudo systemctl reset-failed broken") const b2 = run("sudo sed -n '/User=/p' /etc/systemd/system/broken.service", { id: 'broken-user-line' }) ctx.check('(the broken unit now has a mistyped user)', b2.out === 'User=helo', b2.out) run('sudo systemctl start broken', { id: 'start-broken-user' }) sleep(2) const s217 = run('systemctl status broken --no-pager', { id: 'status-217' }) ctx.check('status shows "status=217/USER"', /Active: failed/.test(s217.out) && /status=217\/USER/.test(s217.out), s217.out) const j217 = run('sudo journalctl -u broken --no-pager -n 4', { id: 'journal-217' }) ctx.check('the journal records status=217/USER', /status=217\/USER/.test(j217.out), j217.out) const idh = run('id helo', { id: 'id-helo' }) ctx.check('id on the User= name: "no such user"', /no such user/.test(idh.out), idh.out) L('sudo rm /etc/systemd/system/broken.service && sudo systemctl daemon-reload && sudo systemctl reset-failed broken 2>/dev/null; true') // ── step 8: lock it down ──────────────────────────────────────────────────────────────── const sec0 = run('systemd-analyze security hello --no-pager | tail -n 1', { id: 'security-before' }) const e0 = exposure(sec0.out) ctx.check('systemd-analyze security rates the plain unit (an exposure level and a word)', Number.isFinite(e0), sec0.out) run('sudo mkdir -p /etc/systemd/system/hello.service.d', { id: 'dropin-dir' }) const hd = run(`sudo tee /etc/systemd/system/hello.service.d/hardening.conf <<'EOF'\n${HARDENING}\nEOF`, { id: 'hardening' }) ctx.check('the hardening drop-in is written', hd.out === HARDENING, hd.out) run('sudo systemctl daemon-reload && sudo systemctl restart hello', { id: 'reload-restart-2' }) sleep(2) const sec1 = run('systemd-analyze security hello --no-pager | tail -n 1', { id: 'security-after' }) const e1 = exposure(sec1.out) ctx.check('the exposure level drops after the drop-in', Number.isFinite(e1) && e1 < e0, `${e0} → ${e1}\n${sec1.out}`) const c2 = run('curl -s http://127.0.0.1:8001/', { id: 'curl-hardened' }) ctx.check('the app still answers with the hardening on', c2.out === 'hello from a systemd service', c2.out) const cat = run('systemctl cat hello --no-pager | grep -E "^# /"', { id: 'systemctl-cat' }) ctx.check('systemctl cat lists the unit file and the drop-in it reads', /# \/etc\/systemd\/system\/hello\.service/.test(cat.out) && /# \/etc\/systemd\/system\/hello\.service\.d\/hardening\.conf/.test(cat.out), cat.out) // ── step 9: reboot ─────────────────────────────────────────────────────────────────────── const secs = rebootBox() ctx.capture('sudo reboot', '', { id: 'reboot' }) ctx.check('the server comes back after the reboot', typeof secs === 'number', String(secs)) sleep(3) const ab = run('systemctl is-active hello && systemctl is-enabled hello', { id: 'after-reboot' }) ctx.check('after the reboot hello is active and enabled: it started by itself', ab.out === 'active\nenabled', ab.out) const c3 = run('curl -s http://127.0.0.1:8001/', { id: 'curl-after-reboot' }) ctx.check('and answers', c3.out === 'hello from a systemd service', c3.out) ctx.stepsDone() reportBox( ctx, L, `A fresh Ubuntu ${RELEASE} server (a VM from Ubuntu’s cloud image) reached over SSH as the sudo user deploy; the service is a small Python 3 web app from Ubuntu’s own python3 package, nothing downloaded.`, ['systemd', 'python3'], ) // ── remove it ─────────────────────────────────────────────────────────────────────────── const dis = run('sudo systemctl disable --now hello', { id: 'disable' }) ctx.check('disable --now removes the boot link: "Removed …hello.service"', /Removed .*hello\.service/.test(dis.out), dis.out) run('sudo rm -r /etc/systemd/system/hello.service /etc/systemd/system/hello.service.d && sudo systemctl daemon-reload', { id: 'rm-unit' }) const gone = run('systemctl status hello --no-pager', { id: 'gone' }) ctx.check('systemctl status: "Unit hello.service could not be found."', /Unit hello\.service could not be found\./.test(gone.out), gone.out) run('sudo userdel hello && sudo rm -r /opt/hello', { id: 'rm-user' }) }) export default check