Free security runbook · SSH

SSH on the public internet: keys, tunnels and the audit trail

Port 22 answers from the public internet, authorized_keys has grown for years, and nobody can say which key still works. The fix is not a stronger password policy: it is an inventory, one path in, and an audit trail you can read after the fact.

This page is free. It covers detection, the order that closes the exposure without locking yourself out, and the checks that prove the change held. It contains no attack steps.

Scope: reviewed from primary sources, no live reproduction
Verified against the OpenSSH manual pages and the Cloudflare Tunnel SSH documentation on 2026-11-12. The directives used here are stable across OpenSSH 8.x and 10.x, but the effective configuration is what matters: run sshd -T on the host instead of trusting a file. Commands are read-only.
What exposed means

Four things, not one

A listener

ss -tlnp shows sshd bound to 0.0.0.0 or ::. Binding to loopback or a private interface removes the internet path entirely.

An auth method

Keys are not automatically safer if password auth is still on for one group, or if root can log in with a key that was copied to a laptop.

Key material

Every key in every authorized_keys is a standing credential. It survives staff departures, laptop resets and CI images unless someone removes it.

A path

A jump host, a VPN or a Cloudflare Tunnel is only a control if the origin port is unreachable from outside. Otherwise it is a second door.

Detect

Ask the running daemon, not the config file

CommandWhat it tells you
ss -tlnp | grep ':22'Which address sshd listens on, and which process owns it.
sshd -T | grep -E '^(permitrootlogin|passwordauthentication|pubkeyauthentication|allowusers|allowgroups|maxauthtries)'Effective global config, including defaults that were never written down.
sshd -T -C user=deploy,host=example,addr=203.0.113.10Effective config for one connection, after Match blocks. This catches the exception everyone forgot.
find /home /root -name authorized_keys -exec ssh-keygen -lf {} +Every key with its fingerprint, type and size, per account.
wc -l /home/*/.ssh/authorized_keysHow many standing credentials exist, and in whose home.
last -a | head -20; lastb | head -20Recent successful and failed logins, with source addresses.
journalctl -u ssh -u sshd --since '7 days ago' | grep -E 'Accepted|Failed|Invalid user'Auth events. Empty output usually means the unit name is wrong, not that nobody tried.
cloudflared tunnel listTunnels that could publish SSH; confirm the hostname sits behind an Access policy.
Failure modes

Six ways this stays open

#Failure modeWhat you seeWhat to do
1The public listener is still the real pathss shows 0.0.0.0:22 while the team says "we only use the tunnel".Bind sshd to loopback or a private interface, allow 22 only from the tunnel or management network, and verify from an external network.
2A Match block re-enables password authThe global config says no; sshd -T -C ... says yes for one group.Remove the override or scope it to a network that cannot reach the internet; test with ssh -o PreferredAuthentications=password.
3Key sprawl with no restrictionsThirty keys in one file, none with from= or restrict; nobody knows which laptop owns a fingerprint.Inventory by fingerprint, re-issue with from= or restrict, remove the rest, and add a review date to the file.
4The audit trail is local and shortjournalctl is volatile, auth logs rotate in days, and nobody recorded the session.Enable persistent journal storage, ship auth logs off-host, and record sessions at the access layer. Do this before the incident.
5fail2ban is treated as the controlHundreds of banned IPs, but one leaked key still works and never triggers a ban.Fix keys and the network path first. Bans reduce log noise; they do not authenticate anyone.
6Agent forwarding and shared jump hostsForwardAgent yes on a laptop, root's agent socket reachable on a shared bastion.ForwardAgent no, per-host keys, ssh -J with -o IdentitiesOnly=yes, and no shared root account.
Close it

In this order, with a second session open

1. Inventory first

Dump listeners, effective config, keys and recent logins before changing anything. You need a baseline and a second way in.

2. One path in

Bind to loopback or private, put the tunnel or VPN in front, and block 22 at the edge. Keep an out-of-band console.

3. Then disable passwords

PasswordAuthentication no, KbdInteractiveAuthentication no, PermitRootLogin no. Run sshd -t before any reload.

4. Prove it from outside

From a network you do not control, confirm the port is closed and the tunnel path enforces the Access policy. Then confirm the login appears in the audit trail.

Do not reload sshd over the session you are testing with
Keep a second session open, run sshd -t, then reload. If the new session fails, the old one can still fix the daemon.
Tunnels

A tunnel does not replace SSH authentication

Access decides who reaches the port

cloudflared access ssh --hostname ssh.internal puts the connection through Cloudflare Access; the policy decides who can reach port 22 on the origin. The origin still authenticates the SSH user, so keys and AllowUsers still matter.

The tunnel token is a credential

A tunnel token can publish routes. Scope it to the tunnel, rotate it, and do not leave a second copy on a laptop. A token in a shell history is the same as a key in a public repo.

Verify

Four checks after the change

CheckExpected result
ss -tlnp | grep ':22'Loopback or a private address only; no 0.0.0.0 or ::.
ssh -o ConnectTimeout=5 -o BatchMode=yes user@host from an external networkTimeout or connection refused, not an SSH banner.
sshd -T -C user=...,host=...,addr=...passwordauthentication no for every user and group that matters.
journalctl -u ssh --since -1h after a test loginThe event is present with the source address, and it is shipped off-host.
FAQ

Questions that come up during the change

We use a VPN. Is that enough?

Only if the public port is closed. A VPN in front of an open sshd is a second door; check from outside the VPN before you call it done.

Can we keep password auth for a break-glass account?

If you must, scope it to a management network and an account without sudo, and alert on every use. A global yes is not break-glass.

Do SSH keys expire?

No. That is why an inventory and a removal date matter more than key length. A five-year-old key on a lost laptop is still valid today.

Does moving SSH to port 2222 help?

It reduces log noise, not exposure. Treat it as cosmetic unless the port is also unreachable from the internet.

Sources

Primary sources

Going deeper on the change window?

The paid runbooks cover the change windows around this: production defaults, certificate rollovers, and the checks that fail only on a real domain.

This page is free, no checkout. Paid runbooks are $9.99 each and covered by the 7-day refund policy at /runbooks/refund.