ServerHandbook live check: run log ------------------------------------------------------------------------------ Check scripts/live/checks/change-ssh-port-ubuntu.mjs (sha256 6aa8bf1052750c8e9ca04ece6cec9d5afb313013de9e017059aa5432a98f01cc) Ran at 2026-11-19T20:52:18Z (26 s) Result PASS, all 14 claims held Tested on System Ubuntu 24.04.5 LTS Kernel 6.8.0-147-generic Package openssh-server 1:9.6p1-3ubuntu13.20 Package openssh-client 1:9.6p1-3ubuntu13.20 Package systemd 255.4-1ubuntu8.18 Package ufw 0.36.2-6 Setup A fresh Ubuntu 24.04 server (a VM from Ubuntu’s cloud image) with OpenSSH from Ubuntu’s archive (socket activated), reached over SSH as the sudo user deploy. The "new login" is deploy logging in to the same server with its key. 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 (2) tcp 0.0.0.0:2222 sshd, systemd tcp [::]:2222 sshd, systemd Listening sockets closed (0) Files added in /etc (1) /etc/ssh/sshd_config.d/10-port.conf Files changed in /etc (2) /etc/ufw/user.rules /etc/ufw/user6.rules Files removed from /etc (0) Disk used on / (includes logs written during the run): +0.0 MB ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Claims (14), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 24.04 server box (web-server) starts 2. ok (setup) a key login on port 22 works | in-on-22 3. ok ssh.socket listens on port 22 (socket activation) | 0.0.0.0:22 ssh.socket ssh.service | [::]:22 ssh.socket ssh.service 4. ok the drop-in holds Port 22 and Port 2222 | Port 22 | Port 2222 5. ok sshd -t prints nothing: the config is valid 6. ok ufw allow 2222/tcp: rules updated (ufw is not on yet on a fresh server, the rule waits for it) | Rules updated | Rules updated (v6) 7. ok systemctl restart ssh 8. ok after restart ssh: still only port 22, nothing on 2222 | 0.0.0.0:22 [::]:22 | State Recv-Q Send-Q Local Address:Port Peer Address:Port Process | LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=2058,fd=3),("systemd",pid=1,fd=59)) | LISTEN 0 4096 [::]:22 [::]:* users:(("sshd",pid=2058,fd=4),("systemd",pid=1,fd=60)) 9. ok daemon-reload, restart ssh.socket 10. ok now systemd listens on 22 AND 2222 (IPv4 and IPv6) | 0.0.0.0:22 0.0.0.0:2222 [::]:22 [::]:2222 | State Recv-Q Send-Q Local Address:Port Peer Address:Port Process | LISTEN 0 4096 0.0.0.0:2222 0.0.0.0:* users:(("sshd",pid=2318,fd=3),("systemd",pid=1,fd=69)) | LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=2318,fd=5),("systemd",pid=1,fd=71)) | LISTEN 0 4096 [::]:2222 [::]:* users:(("sshd",pid=2318,fd=4),("systemd",pid=1,fd=70)) | LISTEN 0 4096 [::]:22 [::]:* users:(("sshd",pid=2318,fd=6),("systemd",pid=1,fd=72)) 11. ok Ubuntu's generator wrote the ports from sshd_config into ssh.socket | # Automatically generated by sshd-socket-generator | | [Socket] | ListenStream= | ListenStream=0.0.0.0:2222 | ListenStream=[::]:2222 | ListenStream=0.0.0.0:22 | ListenStream=[::]:22 12. ok ssh -p 2222: a new login works on the new port | logged in on 2222 13. 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 openssh-client 1:9.6p1-3ubuntu13.20 | PKG openssh-server 1:9.6p1-3ubuntu13.20 | PKG systemd 255.4-1ubuntu8.18 | PKG ufw 0.36.2-6 14. ok after dropping Port 22: only 2222 listens | State Recv-Q Send-Q Local Address:Port Peer Address:Port Process | LISTEN 0 4096 0.0.0.0:2222 0.0.0.0:* users:(("systemd",pid=1,fd=70)) | LISTEN 0 4096 [::]:2222 [::]:* users:(("systemd",pid=1,fd=71)) ------------------------------------------------------------------------------ Commands and what they printed (11) ------------------------------------------------------------------------------ $ systemctl list-sockets --no-pager | grep ssh 0.0.0.0:22 ssh.socket ssh.service [::]:22 ssh.socket ssh.service $ printf 'Port 22\nPort 2222\n' | sudo tee /etc/ssh/sshd_config.d/10-port.conf Port 22 Port 2222 $ sudo sshd -t $ sudo ufw allow 2222/tcp Rules updated Rules updated (v6) $ sudo systemctl restart ssh $ sudo ss -tlnp 'sport = :22 or sport = :2222' State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=2058,fd=3),("systemd",pid=1,fd=59)) LISTEN 0 4096 [::]:22 [::]:* users:(("sshd",pid=2058,fd=4),("systemd",pid=1,fd=60)) $ sudo systemctl daemon-reload && sudo systemctl restart ssh.socket $ sudo ss -tlnp 'sport = :22 or sport = :2222' State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 4096 0.0.0.0:2222 0.0.0.0:* users:(("sshd",pid=2318,fd=3),("systemd",pid=1,fd=69)) LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=2318,fd=5),("systemd",pid=1,fd=71)) LISTEN 0 4096 [::]:2222 [::]:* users:(("sshd",pid=2318,fd=4),("systemd",pid=1,fd=70)) LISTEN 0 4096 [::]:22 [::]:* users:(("sshd",pid=2318,fd=6),("systemd",pid=1,fd=72)) $ cat /run/systemd/generator/ssh.socket.d/addresses.conf # Automatically generated by sshd-socket-generator [Socket] ListenStream= ListenStream=0.0.0.0:2222 ListenStream=[::]:2222 ListenStream=0.0.0.0:22 ListenStream=[::]:22 $ ssh -p 2222 deploy@localhost echo logged in on 2222 logged in on 2222 $ sudo sed -i '/^Port 22$/d' /etc/ssh/sshd_config.d/10-port.conf && sudo systemctl daemon-reload && sudo systemctl restart ssh.socket && sudo ss -tlnp 'sport = :22 or sport = :2222' State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 4096 0.0.0.0:2222 0.0.0.0:* users:(("systemd",pid=1,fd=70)) LISTEN 0 4096 [::]:2222 [::]:* users:(("systemd",pid=1,fd=71)) ------------------------------------------------------------------------------ The check's source (scripts/live/checks/change-ssh-port-ubuntu.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB how-to: change the SSH port on Ubuntu 26.04 and 24.04, where sshd is socket activated (ssh.socket). Safe order: * add the new port NEXT TO 22 in a drop-in, open it in the firewall, apply it the socket way (daemon-reload + restart * ssh.socket, which reruns Ubuntu's generator), prove a NEW login on the new port, and only then drop 22. * The trap shown on the way: `systemctl restart ssh` after the edit leaves the listening ports as they were. * The runner itself logs in on port 22, so 22 is removed in the very last command (after the report). * Runs on the local VM. */ import { bareBox, reportBox } from '../lib.mjs' const RELEASE = process.env.LIVE_BOX_RELEASE || '24.04' const T = 60000 const check = (ctx) => bareBox(ctx, async ({ sudoUser }) => { const { run, L } = sudoUser() const ports = () => L("sudo ss -tlnH 'sport = :22 or sport = :2222' | awk '{print $4}' | sort | tr '\\n' ' '") // ── hidden: a key the sudo user can log in to itself with (stands in for the reader's computer) ── // a first sudo makes 24.04's bash stop printing its "To run a command as administrator" hint into the next output L('sudo true') L("ssh-keygen -q -t ed25519 -N '' -f ~/.ssh/id_ed25519 && cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys") L('ssh-keyscan -t ed25519 localhost 2>/dev/null >> ~/.ssh/known_hosts') const base = L('ssh -o BatchMode=yes deploy@localhost echo in-on-22', T) ctx.check('(setup) a key login on port 22 works', /in-on-22/.test(base), base) // ── 1. who listens on 22 ──────────────────────────────────────────────────────────────── const s1 = run('systemctl list-sockets --no-pager | grep ssh', { id: 'sockets' }) ctx.check('ssh.socket listens on port 22 (socket activation)', /:22\b/.test(s1.out) && /ssh\.socket/.test(s1.out), s1.out) // ── 2. the new port, next to 22 ───────────────────────────────────────────────────────── const w1 = run("printf 'Port 22\\nPort 2222\\n' | sudo tee /etc/ssh/sshd_config.d/10-port.conf", { id: 'dropin' }) ctx.check('the drop-in holds Port 22 and Port 2222', w1.out === 'Port 22\nPort 2222', w1.out) const t1 = run('sudo sshd -t', { id: 'sshd-t' }) ctx.check('sshd -t prints nothing: the config is valid', t1.exit === 0 && t1.out === '', t1.out) const u1 = run('sudo ufw allow 2222/tcp', { id: 'ufw' }) ctx.check('ufw allow 2222/tcp: rules updated (ufw is not on yet on a fresh server, the rule waits for it)', /Rules updated/.test(u1.out), u1.out) // ── 3. the trap: restart ssh ──────────────────────────────────────────────────────────── const r1 = run('sudo systemctl restart ssh', { id: 'restart-ssh' }) ctx.check('systemctl restart ssh', r1.exit === 0, r1.out) const p1 = run("sudo ss -tlnp 'sport = :22 or sport = :2222'", { id: 'ss-after-restart' }) const after1 = ports() ctx.check('after restart ssh: still only port 22, nothing on 2222', /:22\b/.test(after1) && !/:2222\b/.test(after1), `${after1}\n${p1.out}`) // ── 4. the socket way ─────────────────────────────────────────────────────────────────── const r2 = run('sudo systemctl daemon-reload && sudo systemctl restart ssh.socket', { id: 'restart-socket' }) ctx.check('daemon-reload, restart ssh.socket', r2.exit === 0, r2.out) const p2 = run("sudo ss -tlnp 'sport = :22 or sport = :2222'", { id: 'ss-after-socket' }) const after2 = ports() ctx.check('now systemd listens on 22 AND 2222 (IPv4 and IPv6)', /0\.0\.0\.0:22 /.test(after2) && /0\.0\.0\.0:2222 /.test(after2) && /\[::\]:2222/.test(after2), `${after2}\n${p2.out}`) const g1 = run('cat /run/systemd/generator/ssh.socket.d/addresses.conf', { id: 'generator' }) ctx.check("Ubuntu's generator wrote the ports from sshd_config into ssh.socket", /ListenStream=/.test(g1.out) && /2222/.test(g1.out), g1.out) // ── 5. prove a NEW login on the new port ──────────────────────────────────────────────── L('ssh-keyscan -p 2222 -t ed25519 localhost 2>/dev/null >> ~/.ssh/known_hosts') const n1 = run('ssh -p 2222 deploy@localhost echo logged in on 2222', { id: 'login-2222', timeout: T }) ctx.check('ssh -p 2222: a new login works on the new port', /^logged in on 2222$/m.test(n1.out), n1.out) ctx.stepsDone() reportBox( ctx, L, `A fresh Ubuntu ${RELEASE} server (${process.env.LIVE_VPS ? 'a rented cloud server, rebuilt to the provider’s stock image' : 'a VM from Ubuntu’s cloud image'}) with OpenSSH from Ubuntu’s archive (socket activated), reached over SSH as the sudo user deploy. The "new login" is deploy logging in to the same server with its key.`, ['openssh-server', 'openssh-client', 'systemd', 'ufw'], ) // ── 6. last: drop port 22 (the runner's own logins use 22, so this is the final command) ── const d1 = run( "sudo sed -i '/^Port 22$/d' /etc/ssh/sshd_config.d/10-port.conf && sudo systemctl daemon-reload && sudo systemctl restart ssh.socket && sudo ss -tlnp 'sport = :22 or sport = :2222'", { id: 'drop-22', timeout: T }, ) ctx.check('after dropping Port 22: only 2222 listens', /:2222\b/.test(d1.out) && !/:22\s/.test(d1.out), d1.out) }) export default check