SSH key hygiene on a machine you rarely touch

2026-09-24 · securityssh

For a server I log into twice a month, the risk is not that I forget the password. It is that I make a change, disconnect, and discover the problem the next time I need it — which may be months later, on a day when something else is already broken.

Order of operations

The sequence matters more than the settings. Every step below is verified before the next one, so there is never a window where the only working access method has already been removed.

  1. Confirm key authentication works, in a second session, while the first one stays open.
  2. Only then disable password authentication.
  3. Confirm again with a fresh connection before closing the old one.
The old session is the recovery path. Closing it before the new configuration is proven is how people end up at a rescue console.

Check the effective configuration, not the file you edited — includes and drop-in directories mean the file is a suggestion:

sshd -T | grep -Ei 'passwordauth|permitroot|pubkey'

What is actually worth setting

PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
PubkeyAuthentication yes
MaxAuthTries 4

KbdInteractiveAuthentication is the one people miss. With PAM in play it can still allow a password-like exchange even when PasswordAuthentication is off, which makes the setting look applied when it is not.

For root: prohibit-password keeps key-based root login and removes password login. Fully disabling root login is stricter, but if your only key lives with sudo rights on a non-root account you have moved the same secret one step to the side — it is a preference, not a guarantee.

Brute force: it is background noise

An internet-facing SSH port receives constant attempts. This is not a sign of targeting; it is weather. The countermeasure is automated and boring: a rate limit plus a ban on repeat offenders.

[sshd]
maxretry = 4
findtime = 1h
bantime  = 2h

The common default is five attempts in ten minutes, banned for ten minutes — which means the same source comes back indefinitely. A longer window costs nothing when key authentication is the only way in.

One thing worth verifying rather than assuming: that bans apply to both address families. A host can be reachable over IPv4 and IPv6, and a firewall helper configured for one family only will leave the other completely unguarded. Testing it is one command:

fail2ban-client set sshd banip 2001:db8::1
ip6tables -S | grep f2b

If a REJECT rule for that address shows up, dual-stack banning works. If the chain does not exist yet, that is not proof of a problem — some setups create the chain lazily on the first ban. Try the ban before concluding anything.

The unglamorous part