How SSH Works: Three Layers, and the Settings That Actually Matter
Transport, authentication and connection layers explained, from host-key trust to channels and tunnels, plus the sshd settings worth changing today.
On this page
Most people meet SSH as a password prompt and a scary warning once a year. Underneath is a clean three-layer design, specified in four RFCs, and every common SSH problem maps neatly onto one layer.
The three layers
+-----------------------------------------------+
| CONNECTION channels: shell, exec, sftp, | RFC 4254
| port forwarding, agent |
+-----------------------------------------------+
| AUTHENTICATION publickey, password, ... | RFC 4252
+-----------------------------------------------+
| TRANSPORT key exchange, host auth, | RFC 4253
| encryption, integrity |
+-----------------------------------------------+
| TCP (port 22) |
+-----------------------------------------------+
SSH-2 is the version that was standardised by the IETF, and the only one in use.
Layer 1: transport, building a trustworthy pipe
When the TCP connection opens, both sides announce their supported algorithms and agree on one set: key exchange, host key type, cipher, message authentication. Then:
- Key exchange
Client and server run a Diffie-Hellman style exchange so both derive the same session keys, without those keys ever crossing the wire. An eavesdropper who records the whole conversation cannot reconstruct them.
- Server authentication
The server proves its identity by signing the exchange with its host key. The client compares that key against
known_hosts. On first contact it asks you to trust the fingerprint, and remembers it. - Encryption and integrity on
From here on, every packet is encrypted and carries an integrity check, so tampering is detected and content is private.
This is why the host-key warning matters. The whole scheme prevents man-in-the-middle attacks by pinning the server’s key on first use. If it changes unexpectedly, the protocol is telling you the thing you are talking to is not the thing you trusted before.
“REMOTE HOST IDENTIFICATION HAS CHANGED” is either a rebuilt server or an interception. Confirm the new fingerprint out of band (cloud console, a colleague, the provisioning log) before you delete the old known_hosts line. Habitually deleting it cancels the protection.
Layer 2: authentication, proving the client
Only after the encrypted pipe exists does the client identify itself, so credentials are never sent in the clear. Methods include:
| Method | How it works | Notes |
|---|---|---|
| Public key | Client signs a challenge with its private key. The server checks it against authorized_keys | Strongest common choice. Nothing reusable crosses the network |
| Password | Sent over the encrypted channel | Guessable and reusable. Often the target of brute-force scans |
| Keyboard-interactive | Server asks prompts, often for one-time codes | Basis for two-factor setups |
| Certificates | A trusted authority signs user or host keys | Scales better than copying keys around |
Public-key authentication in practice:
ssh-keygen -t ed25519 -C "laptop" # creates the key pair, set a passphrase
ssh-copy-id user@server # installs the public half
ssh user@server # signs in with the private half
Layer 3: connection, many things over one pipe
Once authenticated, the connection layer multiplexes channels over the single encrypted session. Each channel is independent, with its own flow control.
ssh -L 5432:db.internal:5432 bastion makes a private database appear on your localhost, carried inside the SSH session.ssh -R 8080:localhost:3000 server lets the remote host connect back to something on your machine.Diagnosing by layer
| Symptom | Layer | Likely cause |
|---|---|---|
| ”no matching key exchange method” | Transport | Client and server share no common algorithm, often a very old or hardened host |
| Host key changed warning | Transport | Rebuilt server or interception |
| ”Permission denied (publickey)“ | Authentication | Wrong key offered, bad permissions on ~/.ssh, key not in authorized_keys |
| Connects, then forwarded port fails | Connection | Forwarding disabled in server config or target unreachable from the server |
| Hangs before any prompt | Below SSH | Firewall or DNS, not the protocol |
Run ssh -v (more v’s for more detail) and read which stage it stops at.
The server settings worth changing
# /etc/ssh/sshd_config
PasswordAuthentication no # keys only, once you have confirmed key login works
PermitRootLogin no # log in as a user, escalate with sudo
PubkeyAuthentication yes
AllowUsers deploy alice # explicit allowlist
Reload the daemon after editing, and keep one session open while you test a second, so a typo does not lock you out.
Reaching internal hosts safely
Jump through a hardened bastion with ProxyJump rather than forwarding your agent into a shared machine.
ssh -J bastion.example.com app-server.internal
The jump host only relays encrypted traffic, and your key never lands on it. Agent forwarding, by contrast, lets anyone with root on the remote machine use your agent for as long as you are connected.
Do
Use ed25519 keys with a passphrase and an agent, disable password logins, restrict users, verify host fingerprints out of band, and use ProxyJump for internal hops.
Don't
Copy a private key to servers, forward your agent everywhere, or silence the host-key warning by deleting known_hosts lines on reflex.
Takeaway
SSH works because of its layering: the transport layer creates a private, tamper-evident pipe and pins the server’s identity, the authentication layer proves the client, and the connection layer carries many independent channels over that one pipe. Learn the layers and the errors stop being mysterious, and the hardening list stays short.
Frequently asked questions
What are the three layers of the SSH protocol?
The transport layer negotiates algorithms, exchanges keys, authenticates the server and encrypts everything. The user authentication layer proves who the client is. The connection layer then multiplexes the one encrypted session into many logical channels: a shell, file transfers and forwarded ports.
What does the known_hosts warning mean?
The server presented a host key that differs from the one you saved on first connect. It may be a reinstalled server, or someone intercepting the connection. Verify the new fingerprint through another channel before removing the old entry, never by habit.
Is public-key authentication better than a password?
Yes, generally. The private key never leaves your machine, nothing reusable crosses the network, and it cannot be guessed by brute force the way a password can. Protect the private key with a passphrase and an agent, and disable password login on servers that do not need it.
What is the risk of SSH agent forwarding?
While you are connected, anyone with root on the remote host can ask your forwarded agent to sign authentication requests as you, reaching any server your key can. Prefer ProxyJump through a bastion host, and forward the agent only to machines you fully trust.
/* Comments */
Comments are offline right now — we reconnect automatically, nothing is lost.