What key-only login actually protects against
PasswordAuthentication no in sshd_config, you remove online password guessing. That matters, and it cuts out most of the noise in your auth logs. If you haven't done the basic hardening yet, this article on SSH brute force and attack surface covers it.
Key-only login does nothing about these cases:
- A private key that leaked. It may have been copied off a laptop, left with loose file permissions, or committed to a public Git repository.
- An attacker who got a shell some other way, such as a vulnerable plugin, an uploaded webshell, or a reused panel password, and is now running commands as a real user.
- A former contractor or staff member whose key nobody removed.
~/.ssh/authorized_keys for the user they are running as, or for root if they managed to escalate. From then on they log in with their own key, like any admin.
Why changing passwords doesn't remove them
AuthorizedKeysFile directive for that user. If it does, and the client proves it holds the matching private key, the client gets in. The password hash in /etc/shadow plays no part in that check. A planted key therefore survives password resets and survives patching the plugin that let the attacker in. It also survives removing the webshell, because the key sits in a file outside the web root that a malware scanner focused on web files will probably never open.
The file is also easy to overlook. It is a hidden file in a hidden directory, and each user can have one. It can also live somewhere other than ~/.ssh/ if someone changed the sshd config. Support engineers often see this pattern: a site is cleaned and passwords are rotated, then the same account is modified again a week later. In that situation, the authorized_keys files are one of the first places worth checking.
Find every file sshd will actually read
sshd -T prints the effective configuration after includes and defaults have been applied:
BASH
# Effective config: where sshd looks for keys, and whether a command supplies them
sudo sshd -T | grep -Ei '^(authorizedkeysfile|authorizedkeyscommand|permitrootlogin)'
# Every authorized_keys-style file under common locations, with last-modified time and owner
sudo find / -xdev \( -name 'authorized_keys' -o -name 'authorized_keys2' \) -type f -exec stat -c '%y %U %n' {} + 2>/dev/nullauthorized_keys2. On many systems the defaultAuthorizedKeysFilestill includes.ssh/authorized_keys2, a legacy name most admins have forgotten. That makes it a good place to hide a key.- Paths outside
/home. Service accounts often have home directories under/varor/srv. The-xdevflag keepsfindon one filesystem, so if/homeis a separate mount, runfindagainst it as well. AuthorizedKeysCommand. If this is set, keys may come from an external system such as LDAP, and the files on disk are only part of the picture.permitrootlogin. If root can log in with a key,/root/.ssh/authorized_keysdeserves the closest look.
touch. A recent mtime is a useful hint, but an old mtime doesn't prove the file is clean.
Read the keys and match them to logins
deploy@ci and it will look routine. Fingerprints are better for comparison. The loop below walks every account from getent passwd, so it also catches users whose home directory isn't under /home. Run it as root:
BASH
# Run as root. Prints one fingerprint per key, grouped by user and file.
getent passwd | cut -d: -f1,6 | while IFS=: read -r user home; do
for f in "home/.ssh/authorized_keys" "home/.ssh/authorized_keys2"; do
[ -f "f" ] && echo "==user f" && ssh-keygen -lf "f"
done
doneSHA256:...), the comment, and the key type. If sshd -T showed a custom AuthorizedKeysFile path, add it to the inner loop.
Next, find out which of those keys are actually used. When a login succeeds with a key, sshd logs the fingerprint of the key that was accepted:
BASH
# Debian/Ubuntu log path; on RHEL-family systems use /var/log/secure
sudo grep 'Accepted publickey' /var/log/auth.log
# Systems that log only to the journal (unit may be 'ssh' or 'sshd')
sudo journalctl -u ssh --since '30 days ago' | grep 'Accepted publickey'- Known and used. Leave them alone.
- Known but unused for months. These are stale. They usually belong to a departed staff member, an old CI server, or a one-time migration. Remove them after confirming with the owner.
- Unknown. Nobody on the team recognizes the key. Treat it as an incident. Save a copy of the file and the matching log lines before removing anything, and check where else that source IP has logged in.
Make it a monthly habit instead of a cleanup task
- Save the fingerprint loop output to a dated file, for example
/root/ssh-keys-$(date +%Y-%m).txt. - Run
diffagainst last month's file. Any added line needs an owner and a reason. - Check each new fingerprint against the
Accepted publickeylines for the same period. - Remove stale keys and record who approved the removal.
- Annotate every key. Agree on a comment format such as owner name plus the date added. It isn't a security control, since anyone can write any comment, but it makes the known keys quick to recognize.
- Move key files out of reach of users. Setting
AuthorizedKeysFile /etc/ssh/authorized_keys/%uand keeping that directory root-owned means a compromised account can no longer add a key for itself. The trade-off is that users can't manage their own keys, so every change goes through an admin or a config management tool. On shared hosting where customers add their own keys, this often isn't practical. On a team-run VPS it usually is. - Restrict keys that don't need a shell. Options at the start of an authorized_keys line, such as
from=to limit source addresses orrestrictto disable forwarding and terminal allocation, limit the damage a leaked backup or deploy key can do. - Look past SSH during incident response. An attacker who planted a key may also have added a cron job or a systemd service. The key audit is one check among several.
