All ArticlesSecurity2026-11-25T19:13:56+08:00

Thinking SSH Keys Make You Safe from Takeover? Audit authorized_keys Monthly

Admins often assume key-only SSH login closes the door, but attackers who get in once can add their own key to authorized_keys and keep access after passwords change. Readers will learn how to list every authorized_keys file on a server, spot stale or unknown keys, and make the check a monthly habit.

A common belief among server admins is that once password login is disabled and everyone uses SSH keys, takeover is off the table. Bots can't guess a key, so the door is closed. That belief covers only one way in. It says nothing about who has already added their own key to the server, and that is how attackers usually keep access after they get in once.

The question of how secure SSH keys really are comes up regularly, for example in this r/linuxadmin thread. The cryptography is rarely the weak spot. The weak spot is the list of keys the server trusts, and very few people ever read that list.

What key-only login actually protects against

When you set 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.

In the second case the attacker doesn't need to break SSH at all. They append one line to ~/.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

This part surprises people during incident cleanup. The usual reflex after a compromise is to reset every password: the cPanel account, root, the database. That feels thorough, but SSH public key authentication never looks at the password.

When a client connects with a key, sshd checks whether the offered public key appears in one of the files named by the 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

Don't assume the path. Ask sshd what it uses. sshd -T prints the effective configuration after includes and defaults have been applied:

# 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/null

Check a few things in this output:

  • authorized_keys2. On many systems the default AuthorizedKeysFile still 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 /var or /srv. The -xdev flag keeps find on one filesystem, so if /home is a separate mount, run find against 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_keys deserves the closest look.

Don't put too much trust in the modification time. Anyone with write access to the file can reset it with 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

A raw authorized_keys line is a long base64 string with an optional comment at the end. The comment is free text, so an attacker can label their key 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:

# 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
done

Each line of output shows the key size, the fingerprint (for example SHA256:...), 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:

# 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'

Those lines show the user, the source IP, the key type, and the SHA256 fingerprint, so you can match each login to a line from the previous step. You end up with three groups of keys:

  • 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.

Before you delete anything, keep in mind that removing the wrong line can lock out a legitimate user, or yourself. Keep an open session while you edit, and test a new login before closing it.

Make it a monthly habit instead of a cleanup task

A one-time audit tells you the state of the server today. Running it on a schedule is what catches a key added next week. The simplest version takes a few minutes a month:

  1. Save the fingerprint loop output to a dated file, for example /root/ssh-keys-$(date +%Y-%m).txt.
  2. Run diff against last month's file. Any added line needs an owner and a reason.
  3. Check each new fingerprint against the Accepted publickey lines for the same period.
  4. Remove stale keys and record who approved the removal.

A few structural changes make the monthly check shorter and harder to get around:

  • 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/%u and 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 or restrict to 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.

The practical takeaway: disabling passwords controls how people log in, and the authorized_keys files control who can log in. Find every file sshd actually reads, fingerprint the keys, match them against the login logs, and diff the result every month. Any key nobody can account for should be treated as an open door until proven otherwise.