Reference · Linux servers
Unknown SSH keys: list every key that can sign in, match fingerprints and remove the rest
Find every key that can sign in over SSH: AuthorizedKeysFile and AuthorizedKeysCommand in sshd -T, fingerprints from ssh-keygen -lf, key options, Accepted publickey lines in the journal and cloud-init's keys, then remove unknown keys safely.
| Target artifact | The keys that let someone sign in over SSH without a password: each account's ~/.ssh/authorized_keys and ~/.ssh/authorized_keys2, root's included; any file or program named by AuthorizedKeysFile or AuthorizedKeysCommand; and certificate authorities trusted through TrustedUserCAKeys or a cert-authority line. |
|---|---|
| Default behaviour | OpenSSH reads .ssh/authorized_keys and .ssh/authorized_keys2 in each home folder; AuthorizedKeysCommand and TrustedUserCAKeys are none. A listed key lets its holder sign in unless options on its line limit it. On cloud images, cloud-init adds the provider's keys once per instance. |
| Safe state | Every key's fingerprint matches a key you hold; no authorized_keys2 files; AuthorizedKeysCommand and TrustedUserCAKeys are none or yours; every Accepted publickey line in the journal shows a fingerprint you know. |
| Check on Linux | sudo sshd -T | grep -Ei '^(authorizedkeysfile|authorizedkeyscommand|trustedusercakeys) ' |
| Check on Linux | sudo ssh-keygen -lf /home/USER/.ssh/authorized_keys for each account, and for /root/.ssh/authorized_keys |
| Check on Linux | sudo journalctl -u ssh --since -30d --no-pager | grep 'Accepted publickey' |
| Check against your own keys | ssh-keygen -lf ~/.ssh/id_ed25519.pub or ssh-add -l on each computer you sign in from |
| Fix on Linux | save a copy of the file, delete the key's line, sign in from a second session, then end sessions still open with that key |
Where sshd looks for keys
sudo sshd -T | grep -Ei '^(authorizedkeysfile|authorizedkeyscommand|authorizedkeyscommanduser|trustedusercakeys|revokedkeys) 'sshd -T line | Meaning |
|---|---|
authorizedkeysfile .ssh/authorized_keys .ssh/authorized_keys2 | The default. A path without a leading / is relative to each home folder; %h is the home folder and %u the user name |
authorizedkeyscommand none | The default. Otherwise a program prints keys at each sign-in, run as authorizedkeyscommanduser, when no file held a matching key |
trustedusercakeys none | The default. Otherwise a file of certificate authorities: a certificate one of them signed signs in as any account it names |
revokedkeys none | No file of refused keys |
- Ubuntu 24.04's
sshd_configsays "Expect .ssh/authorized_keys2 to be disregarded by default in future", but OpenSSH 9.6 still reads it. OpenSSH 10.0, released on 9 April 2025, accepts wildcard patterns inAuthorizedKeysFile. - A file in
/etc/ssh/sshd_config.d/overrides the main file (SSH password sign-in explains the order), and aMatchblock can set other files for one account:sudo sshd -T -C user=USER,host=HOST,addr=ADDRESSshows what applies to it.
Options on sshd's command line
sshd -T reads only the configuration files: in a test, a daemon started with -o "AuthorizedKeysCommand ..." used that command while a hand-run sshd -T printed none. Check what Ubuntu's service passes:
systemctl cat ssh.service
cat /etc/default/ssh
ps -o pid,args -C sshdsystemctl cat prints the unit and each drop-in under its path; the unit runs /usr/sbin/sshd -D $SSHD_OPTS. ps shows the options the listener started with. A legitimate case: AWS says EC2 Instance Connect is pre-installed on Ubuntu 20.04 and later images; its package adds /lib/systemd/system/ssh.service.d/ec2-instance-connect.conf, which gives sshd an AuthorizedKeysCommand that reads keys from the instance metadata.
List every key and its fingerprint
getent passwd | 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; doneThe loop covers every account, service accounts included; add any other file sshd -T names. Each key prints as size, SHA256: fingerprint, comment and type, such as 256 SHA256:PB4QIIkr40EDnwx50MDAI/63XZkH4YpZPAl849UD2Uo admin-laptop-2026 (ED25519). The comment is free text and proves nothing: match the fingerprint against the keys you hold, on each computer you sign in from.
ssh-keygen -lf ~/.ssh/id_ed25519.pub
ssh-add -lssh-add -l lists your SSH agent's keys the same way. In Windows PowerShell: ssh-keygen -lf $env:USERPROFILE\.ssh\id_ed25519.pub.
Read the options in front of a key
ssh-keygen -lf does not show the options at the start of a line, and lists a cert-authority line like any other key. Print the lines that start with something other than a key type:
sudo grep -nvE '^(ssh-|ecdsa-|sk-|#|$)' /home/USER/.ssh/authorized_keys| Option | Effect |
|---|---|
command="..." | Runs that command whatever the client asks; in a test, a client that asked for id got the forced command's output. The manual's example forces a backup command, dump /home |
from="..." | Accepts the key only from matching addresses; a refused attempt logs Authentication tried for USER with correct key but not from a permitted host |
cert-authority | The key is a certificate authority: any certificate it signed that names this account signs in |
restrict | Turns off forwarding, the terminal and ~/.ssh/rc; options after it turn single features back on |
Which key signed in
sudo journalctl -u ssh --since -30d --no-pager | grep 'Accepted publickey'Each sign-in with a key logs Accepted publickey for USER from ADDRESS port PORT ssh2: ED25519 SHA256:FINGERPRINT.
- The fingerprint is the value
ssh-keygen -lfprints for that key. A certificate shows asED25519-CERT SHA256:..., then itsID, serial number andCAfingerprint. - With
LogLevel VERBOSEin/etc/ssh/sshd_config.d/00-hardening.conf, sshd also logsAccepted key ED25519 SHA256:... found at /home/USER/.ssh/authorized_keys:3, the file and line, and aFailed publickeyline for each key refused. The default,INFO, logs neither. The manual warns that the DEBUG levels violate users' privacy. - From OpenSSH 9.8, released on 1 July 2024, each connection runs in a separate program,
sshd-session; filtering on the message text finds the lines either way.
Keys that cloud-init adds
In the cloud-init 26.2 package for Ubuntu 24.04, the SSH module adds the provider's public keys for the instance, and any ssh_authorized_keys in the user data, to the default user's file, ubuntu on Ubuntu images. With disable_root: true, Ubuntu's default, it adds the same keys to root's file behind a forced command:
no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"ubuntu\" rather than the user \"root\".';echo;sleep 10;exit 142" ssh-ed25519 AAAA...Keys listed under a user in the user data go to that user, and ssh_import_id fetches keys from GitHub (gh:NAME) or Launchpad (lp:NAME). These modules run once per instance, not at every boot; the user data is kept in /var/lib/cloud/instance/user-data.txt. MITRE ATT&CK notes that attackers can add keys through a provider's interface too, such as Google Cloud's add-metadata command.
Remove an unknown key
Stop Keep a way back in
Find your own key's fingerprint in the file first, keep your session open until a new one signs in, and make sure you can reach the server's console in your hosting provider's web panel.
- Save a copy as evidence, outside the account's home folder, and note the file's times:
sudo install -d -m 700 /root/ssh-evidence
sudo cp -a /home/USER/.ssh/authorized_keys /root/ssh-evidence/USER-authorized_keys
sudo stat /home/USER/.ssh/authorized_keystouch can set the modification time back, but not the change time, Change in stat. MITRE records an intrusion, campaign C0064 (December 2025 to February 2026), that added a key to root's file through a crontab script, then restored the script's modification time.
- Delete the key's line with
sudo nano /home/USER/.ssh/authorized_keys. sshd reads the file at each sign-in: in a test, the removed key was refused at once, with no restart. - Check that
~/.sshand the file still belong to the account:sudo ls -la /home/USER/.ssh. UnderStrictModes yes, the default, sshd ignores a key file that other users can write to. - Sign in from a second terminal with your own key.
- End sessions still open with the removed key: in a test, one stayed connected.
sslists each SSH connection, its address and thesshdprocesses serving it: in a test there were two for each connection, one owned by root and one by the signed-in user, and killing the root one ended the session.ps -o user= -p PIDprints a process's owner. Use your own SSH port.
sudo ss -tnp state established '( sport = :22 )'
sudo kill PIDRefuse a key on the whole server
Put the public key line in /etc/ssh/revoked_keys, add RevokedKeys /etc/ssh/revoked_keys to /etc/ssh/sshd_config.d/00-hardening.conf, then run sudo sshd -t and sudo systemctl restart ssh. In a test, the key was then refused, logged as revoked by file, even though it was still in authorized_keys. If the file is missing or unreadable, sshd refuses every key, as its manual warns: create the file first.
An unknown key means someone could write to that account's files, or had root: look for scheduled jobs and services they left, and search the journal for the key's fingerprint.
Related guides and lookups
Sources
- Ubuntu manual: sshd, authorized_keys file format, 24.04
- Ubuntu manual: sshd_config, 24.04
- Ubuntu manual: ssh-keygen, 24.04
- OpenSSH: release notes, 9.8 and 10.0
- systemd manual: journalctl
- cloud-init 26.2: module reference, SSH
- AWS: install EC2 Instance Connect
- MITRE ATT&CK: T1098.004, SSH authorized keys
- MITRE ATT&CK: campaign C0064