Strovent SystemsSign in
Reference

Hashed known_hosts: find, remove or hash hosts with ssh-keygen

Hashed known_hosts entries hide the host name and cannot be decoded. Use ssh-keygen -F host to find one, -R host to delete it, and -H to hash an existing file.

A known_hosts line that starts with |1| stores the host name in hashed form:

|1|oZacANhDKSG1fsaTC7PjluQcaAo=|cQir0pF6QxG44QvEuHdCgqsRTgs= ssh-ed25519 AAAAC3Nza...

|1| marks the format. The next field is a random salt and the one after it is an HMAC-SHA1 of the host name made with that salt, both in base64. The key type and the public key follow as usual. The hash is one-way: nothing can turn it back into the name. What you can do is test a name you already suspect.

Why your file is hashed

The ssh client option HashKnownHosts yes hashes every name and address when it is added. OpenSSH's default is no, but the Debian and Ubuntu packages set HashKnownHosts yes in /etc/ssh/ssh_config. Hashed and plain lines can sit in the same file.

Find a host

ssh-keygen -F host.example.com

A match prints # Host host.example.com found: line 7 and the line itself. No output and exit status 1 mean there is no entry. Add -l to print the key fingerprint instead of the raw line, and -f file to search a file other than ~/.ssh/known_hosts.

Hosts on a port other than 22 are stored as [name]:port, and the hash covers the whole string. Search for them that way:

ssh-keygen -F '[host.example.com]:2222'

The plain name will not match a port-2222 entry.

List the hosts you know about

The file cannot list them, but you can test names from your shell history, your ~/.ssh/config or an inventory:

for h in host1.example.com host2.example.com 192.0.2.10; do
  ssh-keygen -F "$h" >/dev/null && echo "$h"
done

An IP address only has its own line if CheckHostIP was on when you connected. OpenSSH's default for it is no.

Remove a host

ssh-keygen -R host.example.com

This works on hashed lines, which sed or a text editor cannot find by name. It saves the previous file as ~/.ssh/known_hosts.old. Lines marked @cert-authority or @revoked are left alone. For when you should remove a key, see Read the SSH warning "REMOTE HOST IDENTIFICATION HAS CHANGED".

Hash an existing file

Turning on HashKnownHosts does not convert old lines. Do it once with:

ssh-keygen -H -f ~/.ssh/known_hosts

Lines that are already hashed are skipped, so it is safe on a mixed file. The original moves to known_hosts.old, and ssh-keygen warns known_hosts.old contains unhashed entries. Delete that file if the point was to hide the names.

Go back to readable names

Set HashKnownHosts no in ~/.ssh/config. New entries are then written in plain text. Old hashed lines stay hashed; remove each one with ssh-keygen -R and accept the key again on the next connection, checking the fingerprint as you would for a new host.

Hashed lines cannot use the wildcard and negation patterns that plain known_hosts lines allow.

Sources

Spotted a mistake? Email [email protected].