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.

alice's laptop and db.example.com, time going down. Transport layer, in clear text: both sides send their SSH-2.0 version strings and KEXINIT; the laptop sends KEX_ECDH_INIT with Q_C; the server answers KEX_ECDH_REPLY with its host key K_S, Q_S and a signature of H; the laptop finds K_S in known_hosts, checks the signature and computes K; both send NEWKEYS. User authentication, encrypted: USERAUTH_REQUEST for alice with her public key and no signature; USERAUTH_PK_OK; USERAUTH_REQUEST again with the signature; USERAUTH_SUCCESS. Connection: CHANNEL_OPEN and CHANNEL_REQUEST exec uptime; the server sends the output in CHANNEL_DATA, exit-status 0, EOF and CLOSE.
Three protocols, one after the other, on one TCP connection; everything after NEWKEYS is encrypted.

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:

Three outcomes of the known_hosts lookup: same key, the connection goes on; no line, ssh shows the fingerprint and asks yes or no, then adds a line; a different key, ssh prints REMOTE HOST IDENTIFICATION HAS CHANGED and refuses
Same key: go on. No line: ask once and remember. Different key: refuse.

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)

A man in the middle between the laptop and the server can run its own key exchange with each side, but it must sign H with its own host key, which does not match the line in known_hosts, so ssh refuses
A man in the middle can do the key exchange, but it cannot sign with the real host key.

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):

  1. SSH_MSG_USERAUTH_REQUEST (50) with user alice, method publickey, the public key and no signature. It asks: would you accept this key? The server looks in ~alice/.ssh/authorized_keys and answers SSH_MSG_USERAUTH_PK_OK (60).
  2. 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 byWhat it does
CHANNEL_OPEN (90)clientopen a "session" channel: its number, window 2097152, max packet 32768
CHANNEL_OPEN_CONFIRMATION (91)serverthe server's number for the same channel
CHANNEL_REQUEST (98)either"exec" a command, "pty-req" a terminal, "shell", "exit-status"
CHANNEL_SUCCESS (99)eitherthe request worked
CHANNEL_DATA (94)eitherbytes: keystrokes one way, output the other
CHANNEL_EOF (96), CHANNEL_CLOSE (97)bothno 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

Open the Networking mind mapNetworkingLayers & links: frames on one local networkLayers & linksIP & routing: packets hop between networksIP & routingCore services: address, name and timeCore servicesTCP, UDP & sockets: end to end between two portsTCP, UDP & socketsHTTP & TLS: what a browser does per requestHTTP & TLSAPIs & push: how apps talk to serversAPIs & pushProxies & edge: boxes in the middle of the pathProxies & edgeContainers: virtual networks from Linux partsContainersReal-time media: voice and video over UDPReal-time mediaSecurity at the edge: filter traffic at the borderSecurity at the edgeHTTP connectionHTTP connectionDNS, TCP handshake, request, keep-aliveTLS 1.3 handshakeTLS 1.3 handshakekey share and certificate in 1 round tripSSH connectionhost key signs H; known_hosts checks itCertificate chainCertificate chainleaf, intermediate, then a trusted rootACME / Let's EncryptACME / Let's EncryptProve control of the name, then send a CSRHTTP/1.1 vs 2 vs 3HTTP/1.1 vs 2 vs 3h2 multiplexes on TCP; h3 runs on QUICQUIC handshakeQUIC handshaketransport and TLS in one round tripHTTP cachingHTTP cachingmax-age to reuse, ETag to revalidateTLS 1.2 vs TLS 1.3TLS 1.2 vs TLS 1.31.3: 1 RTT, ECDHE only, encrypted certOpen the Networking mind mapNetworkingwhat a browser does per requestHTTP & TLSHTTP connectionHTTP connectionDNS, TCP handshake, request, keep-aliveTLS 1.3 handshakeTLS 1.3 handshakekey share and certificate in 1 round tripSSH connectionhost key signs H; known_hosts checks itCertificate chainCertificate chainleaf, intermediate, then a trusted rootACME / Let's EncryptACME / Let's EncryptProve control of the name, then send a CSRHTTP/1.1 vs 2 vs 3HTTP/1.1 vs 2 vs 3h2 multiplexes on TCP; h3 runs on QUICQUIC handshakeQUIC handshaketransport and TLS in one round tripHTTP cachingHTTP cachingmax-age to reuse, ETag to revalidateTLS 1.2 vs TLS 1.3TLS 1.2 vs TLS 1.31.3: 1 RTT, ECDHE only, encrypted cert
This page on the Networking mind map · 83 pages in 10 branches · open it to see the whole map and test yourself in recall mode.