The idea: three protocols on one TCP connection
When you type ssh alice@db.example.com, your laptop opens one TCP connection to port 22. On that connection SSH runs three protocols, one after the other. The transport layer (RFC 4253) agrees on keys and proves the server is the right one. User authentication (RFC 4252) logs you in, inside the encrypted tunnel. The connection protocol (RFC 4254) carries your command or your shell in channels.
The canvas follows the messages. The client is on the left, the server on the right, and each message flies between them. Grey messages are clear text. Green messages are encrypted. A row that turns orange has just changed.
Version strings and KEXINIT
First each side sends one line of text, its identification string, for example SSH-2.0-OpenSSH_9.6. Then each side sends SSH_MSG_KEXINIT (message number 20). It lists, in order of preference, the key exchange methods, host key types, ciphers, MACs and compression it supports. Both sides apply the same rule: for each kind, take the first entry on the client's list that the server also has. Nobody has to answer; both reach the same choice. On this page they agree on curve25519-sha256, ssh-ed25519 and chacha20-poly1305@openssh.com.
Curve25519: a secret that never crosses the wire
The key exchange is Diffie-Hellman on Curve25519 (RFC 8731). The client picks a random secret c and sends only its public value Q_C in SSH_MSG_KEX_ECDH_INIT (30). The server picks its own secret s, computes K = X25519(s, Q_C) and sends Q_S back in SSH_MSG_KEX_ECDH_REPLY (31). The client computes X25519(c, Q_S) and gets the same K. Someone who records both public values still cannot compute K. Both secrets are new for every connection, so a stolen host key later does not unlock old sessions. The math with small numbers is on the Diffie-Hellman page. (Demo: first connect)
The host key signs H
Diffie-Hellman alone does not say who is on the other end. So the server also computes the exchange hash H: a SHA-256 hash over both version strings, both KEXINITs, its host public key K_S, Q_C, Q_S and K. It signs H with its host private key (/etc/ssh/ssh_host_ed25519_key) and puts K_S and the signature in the reply. The client computes the same H and checks the signature. The H of the first key exchange is kept as the session id.
known_hosts: trust on first use
A valid signature only proves that the server owns K_S. The client must still decide whether K_S is the right key for db.example.com. It looks the name up in ~/.ssh/known_hosts:
The first time, ssh shows the key's fingerprint and asks. This is trust on first use: the first answer matters most, so compare the fingerprint with one from the admin. With the default StrictHostKeyChecking=ask, a changed key is refused before NEWKEYS. Your name, key and password are never sent. (Demo: host key changed)
NEWKEYS and the session keys
Both sides derive their keys from K and H (RFC 4253 §7.2): key C = HASH(K ‖ H ‖ "C" ‖ session id) encrypts client → server, key D encrypts server → client (letters A, B and E, F give IVs and MAC keys for ciphers that need them). Then each side sends SSH_MSG_NEWKEYS (21). It means: every packet I send after this one uses the new keys. From then on an observer sees only packet sizes and timing.
ssh-userauth with a public key
The client asks for the ssh-userauth service. Public key login takes two rounds (Demo: log in with a public key):
SSH_MSG_USERAUTH_REQUEST(50) with useralice, methodpublickey, the public key and no signature. It asks: would you accept this key? The server looks in~alice/.ssh/authorized_keysand answersSSH_MSG_USERAUTH_PK_OK(60).- The client signs the session id plus the request with its private key and sends the request again with the signature. The server checks it with the key from authorized_keys and answers
SSH_MSG_USERAUTH_SUCCESS(52).
The private key never leaves the laptop. The signature covers the session id, so it is useless on any other connection. If the key is not in authorized_keys, the answer is SSH_MSG_USERAUTH_FAILURE (51) with the methods that may still work, and ssh prints Permission denied (publickey). (Demo: key not in authorized_keys)
Channels: exec and shell
Everything after login runs in channels. Each side numbers its end; many channels (a shell, port forwards) share one connection. Each has a window: how many bytes the other side may send before it is told it can send more.
| Message (number) | Sent by | What it does |
|---|---|---|
| CHANNEL_OPEN (90) | client | open a "session" channel: its number, window 2097152, max packet 32768 |
| CHANNEL_OPEN_CONFIRMATION (91) | server | the server's number for the same channel |
| CHANNEL_REQUEST (98) | either | "exec" a command, "pty-req" a terminal, "shell", "exit-status" |
| CHANNEL_SUCCESS (99) | either | the request worked |
| CHANNEL_DATA (94) | either | bytes: keystrokes one way, output the other |
| CHANNEL_EOF (96), CHANNEL_CLOSE (97) | both | no more data; the channel is gone when both sent CLOSE |
ssh host uptime uses "exec": the server runs the command, sends its output, its exit status and closes the channel, and ssh exits with the same status (Demo: run a command). A plain ssh host asks for a "pty-req" and then a "shell" (Demo: interactive shell). How keystrokes then reach the shell through the pseudo-terminal is on the TTY, PTY and Job Control page.
See also the TCP three-way handshake that comes before all of this, and an HTTPS connection, where TLS solves the same problem with certificates instead of known_hosts.
What the page leaves out
The TCP segments themselves, the packet format (length, padding, sequence numbers), real key and hash values, the first none auth request that OpenSSH sends to learn the allowed methods, password and keyboard-interactive login, ssh-agent and agent forwarding, host certificates and SSHFP DNS records, rekeying after a time or data limit, WINDOW_ADJUST, port forwarding channels, and the post-quantum hybrid key exchanges (mlkem768x25519-sha256, sntrup761x25519-sha512) that new OpenSSH versions prefer; they follow the same message flow.
On the mind map
References
RFC 4253: The SSH Transport Layer Protocol (version exchange, KEXINIT, NEWKEYS, key derivation)
RFC 4252: The SSH Authentication Protocol (publickey, PK_OK)
RFC 4254: The SSH Connection Protocol (channels, exec, pty-req, shell)
RFC 8731: SSH Key Exchange Method Using Curve25519 and Curve448