Ssh – What are the steps that occur during a ssh connection establishment on a machine that does use password based authentication

authenticationssh

I want to know what happens behind the scenes in establishing a ssh connection, i mean till we land on the shell in a Linux from a Linux machine. I know that password is not sent in plain text in a ssh connection. So what do they do to encrypt the connection. Here i am assuming that i have not generated my public/private keys. I use password for authentication. So i think there cant be a exchange of keys before securing the connection. I searched on google and in serverfault but no hits. The closest i got to in serverfault was this post What exactly does ssh send when performing key negotiation? .
The above post assumes the user is connecting via pubic key exchange. But i am looking for a case of securing the connection without the public/private keys. How does it encrypt the connection even before i enter the password. What does it use to secure the connection so that password is not send plain text ?

Best Answer

When you install SSH and start it for the first time, it will generate public and private keys for the machine, which are the same for every user. Upon connection, both machines will exchange their public key parts (in plain text) and use them to encrypt the communication between the hosts. After the key exchange, the communication will switch to encrypted mode.

In every users ~/.ssh/known_hosts file the keys will then be stored and compared against the one send upon the next connection. This is the reason you will get an error if you try to connect to a machine you have recently reinstalled: While doing so you have generated a new machine key pair, and this doesn't match the cached version, which also could mean an attempted attack.