ssh: authorized keys vs known hosts

published: November 16, 2025


In ssh, what is the difference between the known_hosts file and the authorized_keys file? This may seem a basic part of understanding ssh, but it recently tripped me up while automating the configuration for an ssh connection between servers which will then be used for further automation.

I created a new ssh key-pair on the client machine with the familiar command ssh-keygen -t ed25519 and copied the keytype and public key to the authorized_keys file in the home directory of my login user on the host.

[james@test ~]$ cat ~/.ssh/authorized_keys
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIK+8gTqN9R80CZ4AAb2NNyRVXWjsYsSijLyTaeMdauJ4

Now, if I just wanted to manually connect to the host, this would be enough. The first time we run ssh to connect to the new host we are met with this familiar message:

[james@beepboop ~]$ ssh test.mydomain.com
The authenticity of host 'test.mydomain.com (b490:8736:4e8c:4246::1)' can't be established.
ED25519 key fingerprint is SHA256:K6cZ6TBMZr09k4NuQqo7SfodWo5Wl6UHRs1Ja0p9kLU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

This is ssh giving us a chance to inspect the fingerprint of the public key that the host presented to us and verify that we really are talking to the correct host. If so, we type yes and ssh adds the public key data for the host to the known_hosts file:

Warning: Permanently added 'test.mydomain.com' (ED25519) to the list of known hosts.

known_hosts now looks like this:

[james@beepboop ~]$ cat ~/.ssh/known_hosts
test.mydomain.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAejS63Hz3qQzTSXysvtgtmcMvKW4fZJDd4aMcicWr4L

But we want to automate! To automate things, we don't want to have to verify the key and type yes. We want to take the public key from the host and put it in our known_hosts file before we use ssh to connect. If we do that ssh to the new host will just work.

This is where I got tripped up... Where does the host's public key reside? I had never thought about it before. We can already ssh to this host via a different client, so the host must already have a key - we don't need to create any new keys.

As usual, the answer could be found in the man pages. It turns out that sshd by default has host key-pairs in /etc/ssh and they have stems ssh_host_ecdsa_key, ssh_host_ed25519_key, and ssh_host_rsa_key. There are multiple keys, one for each host key algorithm. If we inspect the .pub public keys of these pairs we will see that they match what ssh added to our known_hosts file. We just need to take the contents of the public key from one of the pairs to create the automation and put it in known_hosts ourself.

recap

authorized keys

known hosts


The names themselves were a clue and now serve as a great reminder of their function. Shout-out to the ssh devs for good naming!

references

Check out the manual page man sshd for more info. It describes the authorized_keys file format and the known_hosts file format it detail.