SSH key-based authentication: known_hosts vs authorized_keys

damluar

I read about setting up ssh keys in Linux and have some questions. Correct me if I'm wrong…

Let's say host tr-lgto wants to connect to host tr-mdm using ssh. If we want to be sure that it's the real tr-mdm, we generate a pair of keys on tr-mdm and we add the public key to known_hosts on tr-lgto. If tr-mdm wants to check that it's the real tr-lgto, then tr-lgto has to generate a keypair and add the public key to authorized_keys on tr-mdm.

Question 1: There is no user field in file known_hosts, just IP addresses and hostnames. tr-mdm might have a lot of users, each with their own .ssh folder. Should we add the public key to each of the known_hosts files?

Question 2: I found that ssh-keyscan -t rsa tr-mdm will return the public key of tr-mdm. How do I know what user this key belongs to? Moreover, the public key in /root/.ssh/ is different from what that command returns. How can this be?

Gilles 'SO- stop being evil'

You're mixing up the authentication of the server machine to the client machine, and the authentication of the user to the server machine.

Server authentication

One of the first things that happens when the SSH connection is being established is that the server sends its public key to the client, and proves (thanks to public-key cryptography) to the client that it knows the associated private key. This authenticates the server: if this part of the protocol is successful, the client knows that the server is who it pretends it is.

The client may check that the server is a known one, and not some rogue server trying to pass off as the right one. SSH provides only a simple mechanism to verify the server's legitimacy: it remembers servers you've already connected to, in the ~/.ssh/known_hosts file on the client machine (there's also a system-wide file /etc/ssh/known_hosts). The first time you connect to a server, you need to check by some other means that the public key presented by the server is really the public key of the server you wanted to connect to. If you have the public key of the server you're about to connect to, you can add it to ~/.ssh/known_hosts on the client manually.

Authenticating the server has to be done before you send any confidential data to it. In particular, if the user authentication involves a password, the password must not be sent to an unauthenticated server.

User authentication

The server only lets a remote user log in if that user can prove that they have the right to access that account. Depending on the server's configuration and the user's choice, the user may present one of several forms of credentials (the list below is not exhaustive).

  • The user may present the password for the account that he is trying to log into; the server then verifies that the password is correct.
  • The user may present a public key and prove that he possesses the private key associated with that public key. This is exactly the same method that is used to authenticate the server, but now the user is trying to prove their identity and the server is verifying them. The login attempt is accepted if the user proves that he knows the private key and the public key is in the account's authorization list (~/.ssh/authorized_keys on the server).
  • Another type of method involves delegating part of the work of authenticating the user to the client machine. This happens in controlled environments such as enterprises, when many machines share the same accounts. The server authenticates the client machine by the same mechanism that is used the other way round, then relies on the client to authenticate the user.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Allow password authentication in SSH only when there is no key in authorized_keys

From Dev

How to clear duplicated entries in SSH known_hosts and authorized_keys files?

From Dev

SSH add key to known_hosts from ssh-keyscan

From Dev

Add ssh key string to .ssh/authorized_keys file

From Dev

Public key authentication for LDAP users using local authorized_keys

From Dev

Can't ssh even with public key added to authorized_keys

From Dev

Add a public ssh key to the authorized_keys of a user

From Dev

how to remove passphrase from ssh / client has .ssh/known-hosts, server=authorized_keys

From Dev

Given keys in ~/.ssh/authorized_keys format, can you determine key strength easily?

From Dev

After changing permission of ~/.ssh/authorized_keys and the ~/.ssh directory I get a permission denied (public key)

From Java

How can I add a .pem private key fingerprint entry to known_hosts before connecting with ssh?

From Dev

Recovered .ssh/authorized_keys file, but still get Permission Denied (Public Key) message

From Java

Adding a public key to ~/.ssh/authorized_keys does not log me in automatically

From Dev

SSH Key-based authentication fails

From Dev

SSH, Key-based authentication and passwords

From Dev

ansible ssh prompt known_hosts issue

From Dev

known_hosts not updating, but ssh says it added

From Dev

Avoiding duplicate entries in ~/.ssh/known_hosts

From Dev

Differing SSH known_hosts formats

From Dev

Generate hashed name for SSH known_hosts

From Dev

Identify the entries within .ssh/known_hosts?

From Dev

MySQL\Workbench\ssh\known_hosts not found

From Dev

Add public key to known_hosts file

From Dev

Remove key from known_hosts

From Dev

Adding a Public Key to known_hosts File

From Dev

SSH client seems to be checking '/dev/null' instead of 'known_hosts' file when checking remote server's ECDSA key

From Dev

Salt and managing .ssh/authorized_keys

From Dev

how to add commands in .ssh/authorized_keys

From Dev

Redis writing to .ssh/authorized_keys

Related Related

  1. 1

    Allow password authentication in SSH only when there is no key in authorized_keys

  2. 2

    How to clear duplicated entries in SSH known_hosts and authorized_keys files?

  3. 3

    SSH add key to known_hosts from ssh-keyscan

  4. 4

    Add ssh key string to .ssh/authorized_keys file

  5. 5

    Public key authentication for LDAP users using local authorized_keys

  6. 6

    Can't ssh even with public key added to authorized_keys

  7. 7

    Add a public ssh key to the authorized_keys of a user

  8. 8

    how to remove passphrase from ssh / client has .ssh/known-hosts, server=authorized_keys

  9. 9

    Given keys in ~/.ssh/authorized_keys format, can you determine key strength easily?

  10. 10

    After changing permission of ~/.ssh/authorized_keys and the ~/.ssh directory I get a permission denied (public key)

  11. 11

    How can I add a .pem private key fingerprint entry to known_hosts before connecting with ssh?

  12. 12

    Recovered .ssh/authorized_keys file, but still get Permission Denied (Public Key) message

  13. 13

    Adding a public key to ~/.ssh/authorized_keys does not log me in automatically

  14. 14

    SSH Key-based authentication fails

  15. 15

    SSH, Key-based authentication and passwords

  16. 16

    ansible ssh prompt known_hosts issue

  17. 17

    known_hosts not updating, but ssh says it added

  18. 18

    Avoiding duplicate entries in ~/.ssh/known_hosts

  19. 19

    Differing SSH known_hosts formats

  20. 20

    Generate hashed name for SSH known_hosts

  21. 21

    Identify the entries within .ssh/known_hosts?

  22. 22

    MySQL\Workbench\ssh\known_hosts not found

  23. 23

    Add public key to known_hosts file

  24. 24

    Remove key from known_hosts

  25. 25

    Adding a Public Key to known_hosts File

  26. 26

    SSH client seems to be checking '/dev/null' instead of 'known_hosts' file when checking remote server's ECDSA key

  27. 27

    Salt and managing .ssh/authorized_keys

  28. 28

    how to add commands in .ssh/authorized_keys

  29. 29

    Redis writing to .ssh/authorized_keys

HotTag

Archive