A password gets changed when someone leaves. A key in authorized_keys usually doesn’t. It keeps working until someone deletes the line, and most servers collect them: the key the hosting panel added at creation, a contractor’s laptop from last year, a CI key for a pipeline that no longer exists. Each one is a standing login, and nothing in SSH reminds you it’s there.
This guide lists every key on a server, uses the login log to show when each one was last used, and then covers removing keys safely and restricting the ones you keep. The commands were tested on Debian 13 with OpenSSH 10.0 and work the same on Ubuntu and RHEL-family servers. Run them as root, since you need to read every user’s home directory.
Check where sshd looks for keys
Don’t assume ~/.ssh/authorized_keys is the only file. If AuthorizedKeysFile isn’t set, sshd reads both ~/.ssh/authorized_keys and ~/.ssh/authorized_keys2, and a key hidden in the second file is easy to miss. Ask sshd what it actually uses: sshd(8): AUTHORIZED_KEYS FILE FORMAT
sudo sshd -T | grep -iE "^(authorizedkeysfile|authorizedkeyscommand|trustedusercakeys) " On a stock Debian 13 server this prints “.ssh/authorized_keys .ssh/authorized_keys2”, “authorizedkeyscommand none” and “trustedusercakeys none”. If AuthorizedKeysCommand is set, keys also come from a program (often a cloud agent or a directory service), and the files are only part of the answer. If TrustedUserCAKeys is set, anyone with a certificate signed by that CA can log in without a line in any file. If you want only the first file, set AuthorizedKeysFile .ssh/authorized_keys in a drop-in; the SSH hardening guide explains how drop-ins are read. SSH hardening: how sshd reads its config
List every key on the server
ssh-keygen -l prints the fingerprint of every key in a file, one line each, and ignores comment lines and key options. That fingerprint is the same SHA256 string sshd writes to the log when the key is used, which is what makes the next step possible.
sudo ssh-keygen -lf /root/.ssh/authorized_keys 256 SHA256:FdCznNceNcN09wFK6NmsH9j6x7sR3Jawo+xTQucmEk8 alice@laptop (ED25519) 3072 SHA256:7wgJz011Qk6oUE84Qhj7mCC2U3zMFikDrHULEZTRME4 old-deploy (RSA) Check every account, not just yours and root. Service accounts and old users with a home directory are where forgotten keys live:
getent passwd | cut -d: -f1,6 | while IFS=: read -r user home; do ls "$home"/.ssh/authorized_keys* 2>/dev/null done Don’t trust the comment at the end of each line. The man page says plainly that it “is not used for anything”: anyone who can write to the file can label a key alice@laptop. The fingerprint is the identity. The comment is a hint.
On cloud servers, expect a key in root’s file that you didn’t add. cloud-init writes the provider’s key to both the default user and root, and by default prefixes root’s copy with a command that prints “Please login as the user … rather than the user root” and exits. That key can’t open a shell as root, but it’s still worth knowing why it’s there. Look at the options at the start of the line to tell: cloud-init: disable_root_opts
sudo cut -c1-120 /root/.ssh/authorized_keys See when each key last logged in
With the default LogLevel INFO, every successful key login writes a line like this, with the key’s type and fingerprint at the end:
sshd-session[48213]: Accepted publickey for deploy from 203.0.113.7 port 51022 ssh2: ED25519 SHA256:FdCznNceNcN09wFK6NmsH9j6x7sR3Jawo+xTQucmEk8 So you can match every key in every file against the journal. This script prints each key with the last login the journal has for it. It reads only, and changes nothing:
#!/bin/sh # ssh-key-audit.sh: every authorized key on this server and its last login. Run as root. logins=$(journalctl _COMM=sshd _COMM=sshd-session --no-pager -o short-iso -g 'Accepted publickey') getent passwd | while IFS=: read -r user _ _ _ _ home _; do for f in "$home/.ssh/authorized_keys" "$home/.ssh/authorized_keys2"; do [ -f "$f" ] || continue ssh-keygen -lf "$f" 2>/dev/null | while read -r bits fp rest; do last=$(printf '%s\n' "$logins" | grep -F " for $user from " | grep -F "$fp" | tail -n 1 | cut -d' ' -f1) echo "$user $fp $rest last login: ${last:-not in journal}" done done done sudo sh ssh-key-audit.sh root SHA256:Qm4tR8… admin@desk (ED25519) last login: 2026-11-02T09:14:51+00:00 deploy SHA256:FdCznN… alice@laptop (ED25519) last login: 2026-11-27T16:03:22+00:00 deploy SHA256:7wgJz0… old-deploy (RSA) last login: not in journal The journal filter matches the program name. Since OpenSSH 9.8 the login lines come from sshd-session rather than sshd, so the script asks for both. If your sshd_config lists keys in a different file, change the inner loop to match what sshd -T printed. OpenSSH 9.8 release notes
“Not in journal” means not used within the journal’s history, which may be short. Check how far back it goes before you treat a key as unused. If the first line is from last week, or --list-boots shows only the current boot, your journal isn’t kept long enough to judge, and the answer has to come from asking people.
sudo journalctl -o short-iso | head -n 1 sudo journalctl --list-boots | head -n 3 Remove the keys nobody uses
For each key with no recent login, find its owner. If nobody claims it, remove it. Keep a copy first, so a key that turns out to belong to a nightly job can go back in a minute:
sudo cp -p /home/deploy/.ssh/authorized_keys /root/authorized_keys.deploy.$(date +%F) sudo grep -vF "<a unique piece of that key>" /home/deploy/.ssh/authorized_keys | sudo tee /home/deploy/.ssh/authorized_keys.new >/dev/null sudo ssh-keygen -lf /home/deploy/.ssh/authorized_keys.new # check the right line is gone sudo mv /home/deploy/.ssh/authorized_keys.new /home/deploy/.ssh/authorized_keys sudo chown deploy: /home/deploy/.ssh/authorized_keys && sudo chmod 600 /home/deploy/.ssh/authorized_keys Copy the piece from the middle or end of the base64 key, not the start: every RSA key of the same size begins with the same characters, and so do all Ed25519 keys. Don’t match on the comment either. Then confirm the file still works for the keys you kept by logging in from a second terminal before you close the first.
Removing a key stops new logins only. sshd checks authorized_keys when someone authenticates, so a session that is already open stays open. When you remove someone’s access because they left, end their sessions too:
loginctl list-sessions sudo loginctl terminate-session <ID> If that person had sudo, the key you removed may not be their only way in. They could have added a key to another account, a cron job or a systemd unit. Run the audit across every account again, and look at recent changes in those places. The auditd guide has rules that record who changes root’s keys, cron and systemd units; add a line like the one below for each account whose keys matter. auditd rules that matter
-a always,exit -F arch=b64 -F dir=/home/deploy/.ssh -F perm=wa -k user_keys Restrict the keys you keep
A line in authorized_keys can start with options that limit what the key may do. These four cover most needs: sshd(8): key options
| Option | What it does | Use it for |
|---|---|---|
| restrict | Turns off port, agent and X11 forwarding, PTY allocation and ~/.ssh/rc | Any key that doesn’t need an interactive shell |
| from="203.0.113.0/24" | Accepts the key only from matching addresses or host names | CI runners and office or VPN ranges |
| command="/usr/local/bin/backup" | Runs this command whatever the client asked for | Backup, deploy and monitoring keys |
| expiry-time="20270301" | Refuses the key after this date | Contractors and temporary access |
Options go before the key type, separated by commas, with no spaces outside quotes. A backup key that may only run one script, only from one network, until March:
restrict,from="203.0.113.0/24",command="/usr/local/bin/backup",expiry-time="20270301" ssh-ed25519 AAAAC3Nza… backup@nas To check, try the key from an address outside the range, or ask it for a shell. The first should be refused and the second should run only the forced command. The command the client asked for is available to your script in SSH_ORIGINAL_COMMAND, if it needs to check it.
File permissions matter too. With StrictModes yes, the default, sshd ignores an authorized_keys file when the file, ~/.ssh or the home directory can be written by other users. A key that suddenly stops working after a permissions change is often this.
sudo stat -c "%a %U %n" /home/deploy /home/deploy/.ssh /home/deploy/.ssh/authorized_keys # expect: home not group/world-writable, 700 on .ssh, 600 on authorized_keys, all owned by the user When there are too many keys to track
Past a handful of people and servers, copying public keys around stops scaling. The usual next step is SSH certificates: you trust one CA key on each server with TrustedUserCAKeys, and sign each person’s key with an expiry date of days or weeks. Access then runs out by itself instead of waiting for someone to edit a file on every server. ssh-keygen does the signing; there’s nothing extra to install. ssh-keygen(1): CERTIFICATES
Whichever way you manage keys, watch the logins themselves. A stolen key that’s still on the list looks exactly like its owner. Vigilhold, our host agent for Linux servers, watches SSH along with processes, network connections and file changes on every server from one console, so SSH logins are watched on every server even when nobody is reading the journal. See Vigilhold
While you’re in sshd_config, paste it into our free checker for a plain-English report on root login, passwords and weak ciphers. It runs in your browser, and nothing you paste leaves the page. SSH config checker
The short version
| Step | Check it worked |
|---|---|
| Know which files sshd reads | sshd -T shows authorizedkeysfile, and no command or CA you didn’t expect |
| List every key on every account | ssh-keygen -lf runs on each file the loop found |
| Match keys to logins | Every key has a recent login or a named owner |
| Remove unused keys | The fingerprint is gone and you can still log in with the keys you kept |
| End old sessions | loginctl list-sessions shows no sessions for removed users |
| Restrict automation keys | The key is refused from outside its range and runs only its command |
A key nobody can name is a login nobody is watching. Find it, then remove it.
Originally published on the Al's-Ops blog.
