SaferComputing.com

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.

Published
Updated
Last checked
Applies to
OpenSSH server on Ubuntu 22.04 LTS and 24.04 LTS, and other Linux systems using OpenSSH; tested with OpenSSH 9.6p1 on Ubuntu 24.04
Technical specification
Target artifactThe 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 behaviourOpenSSH 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 stateEvery 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 Linuxsudo sshd -T | grep -Ei '^(authorizedkeysfile|authorizedkeyscommand|trustedusercakeys) '
Check on Linuxsudo ssh-keygen -lf /home/USER/.ssh/authorized_keys for each account, and for /root/.ssh/authorized_keys
Check on Linuxsudo journalctl -u ssh --since -30d --no-pager | grep 'Accepted publickey'
Check against your own keysssh-keygen -lf ~/.ssh/id_ed25519.pub or ssh-add -l on each computer you sign in from
Fix on Linuxsave 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

Ubuntu, as a user with sudo
sudo sshd -T | grep -Ei '^(authorizedkeysfile|authorizedkeyscommand|authorizedkeyscommanduser|trustedusercakeys|revokedkeys) '
sshd -T lineMeaning
authorizedkeysfile .ssh/authorized_keys .ssh/authorized_keys2The default. A path without a leading / is relative to each home folder; %h is the home folder and %u the user name
authorizedkeyscommand noneThe default. Otherwise a program prints keys at each sign-in, run as authorizedkeyscommanduser, when no file held a matching key
trustedusercakeys noneThe default. Otherwise a file of certificate authorities: a certificate one of them signed signs in as any account it names
revokedkeys noneNo file of refused keys
  • Ubuntu 24.04's sshd_config says "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 in AuthorizedKeysFile.
  • A file in /etc/ssh/sshd_config.d/ overrides the main file (SSH password sign-in explains the order), and a Match block can set other files for one account: sudo sshd -T -C user=USER,host=HOST,addr=ADDRESS shows 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:

Ubuntu, as a user with sudo
systemctl cat ssh.service
cat /etc/default/ssh
ps -o pid,args -C sshd

systemctl 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

Ubuntu, as root (sudo -i first)
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; done

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

Your own computer: Linux or macOS
ssh-keygen -lf ~/.ssh/id_ed25519.pub
ssh-add -l

ssh-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:

Ubuntu, as a user with sudo
sudo grep -nvE '^(ssh-|ecdsa-|sk-|#|$)' /home/USER/.ssh/authorized_keys
OptionEffect
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-authorityThe key is a certificate authority: any certificate it signed that names this account signs in
restrictTurns off forwarding, the terminal and ~/.ssh/rc; options after it turn single features back on

Which key signed in

Ubuntu, as a user with sudo
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 -lf prints for that key. A certificate shows as ED25519-CERT SHA256:..., then its ID, serial number and CA fingerprint.
  • With LogLevel VERBOSE in /etc/ssh/sshd_config.d/00-hardening.conf, sshd also logs Accepted key ED25519 SHA256:... found at /home/USER/.ssh/authorized_keys:3, the file and line, and a Failed publickey line 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:

Line cloud-init writes in /root/.ssh/authorized_keys
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.

  1. Save a copy as evidence, outside the account's home folder, and note the file's times:
Ubuntu, as a user with sudo
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_keys

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

  1. 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.
  2. Check that ~/.ssh and the file still belong to the account: sudo ls -la /home/USER/.ssh. Under StrictModes yes, the default, sshd ignores a key file that other users can write to.
  3. Sign in from a second terminal with your own key.
  4. End sessions still open with the removed key: in a test, one stayed connected. ss lists each SSH connection, its address and the sshd processes 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 PID prints a process's owner. Use your own SSH port.
Ubuntu, as a user with sudo
sudo ss -tnp state established '( sport = :22 )'
sudo kill PID
Refuse 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.

Sources