What is the difference between authorized_key and known_host file for SSH

Source: Internet
Author: User
Tags ssh server
Document directory
  • Server Authentication
  • User Authentication
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 claims 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~/.ssh/known_hostsFile 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. if you have the public key of the server you're about to connect to, you can add it~/.ssh/known_hostsOn the client manually.

By the way,known_hostsCan contain any type of public key supported by the SSH implementation, not just DSA (also RSA and ECDSA ).

Authenticating the server has to be done before you send any confidential data to it. In particle, 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 its identity and the server is verifying it. 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_keysOn 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 your ISES, when your machines share the same accounts. the server authenticates the client machine by the same mechanic that is used the other way round, then relies on the client to authenticate the user.

Those two files are both used by SSH but for completely different purposes, which cocould easily explain your confusion.

Authorized keys

By default SSH uses user accounts and passwords that are managed by the Host OS. (Well, actually managed by PAM but that distinction probably isn't too useful here .) what this means is that when you attempt to connect to SSH with the username 'bob' and some password the SSH server program will ask the OS "I got this guy named 'bob' who' s telling me his password is 'wonka '. can I let him in? "If the answer is yes, then SSH allows you to authenticate and you go on your merry way.

In addition to passwords SSH will also let you use what's called public-key cryptography to identify you. the specific encryption algorithm can vary, but is usually RSA or DSA. in any case when you set up your keys, usingssh-keygenProgram, you create two files. one that is your private key and one that is your public key. the names are fairly self-explanatory. by design the public key can be strewn about like dandelion seeds in the wind without compromising you. the private key shoshould always be kept in the strictest of confidence.

So what you do is place your public key inauthorized_keysFile. Then when you attempt to connect to SSH with username 'bob' and your private key it will ask the OS "I got this guy name 'bob', can be here? "If the answer is yes then SSH will inspect your private key and verify if the public key inauthorized_keysFile is its pair. If both answers are yes, then you are allowed in.

Known hosts

Much like howauthorized_keysFile is used to authenticate usersknown_hostsFile is used to authenticate servers. whenever SSH is configured on a new server it always generates a public and private key for the server, just like you did for your user. every time you connect to an SSH server it presents its private key in order to prove its identity. if you do not have its public key, then your computer will ask for it and add it intoknown_hostsFile. if you have the key, and it matches, then you connect straight away. if the keys do not match, then you get a big nasty warning. this is where things get interesting. the 3 situations that a key mismatch typically happens are:

  1. The key changed on the server. This cocould be from reinstalling the OS or on some oses the key gets recreated when updating ssh.
  2. The hostname or IP address you are connecting to used to belong to a different server. This cocould be address reassignment, DHCP, or something similar.
  3. Malicious man-in-the-middle is happening. This is the biggest thing that key checking is trying to protect you from.

In both cases,known_hostsAndauthorized_keys, The SSH program is using public key cryptography in order to identify either the client or the server.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.