How-to

How SSH Works, One Packet at a Time

An SSH login looks instant, yet several distinct stages happen first. Knowing them makes error messages readable and settings like KexAlgorithms less mysterious.

What happens when I type ssh and press Enter?

Your client resolves the host name, opens a TCP connection to port 22 and then runs through three protocol layers defined in the SSH-2 standards. The transport layer sets up encryption and proves the server's identity. The authentication layer proves yours. The connection layer then carries shells, commands, file transfers and forwarded ports as separate channels inside the one encrypted pipe. Each layer depends on the one before it, so an error message tells you at once which stage failed.

Stage 1: How do both sides agree on what to use?

First, each side sends a plain-text banner such as SSH-2.0-OpenSSH_10.0. Then both send a KEXINIT message listing the algorithms they support, in order of preference, for four jobs: key exchange, host key signatures, encryption and message authentication. The first entry on the client's list that the server also supports wins in each category. You can watch it happen:

ssh -vv ops@203.0.113.25 2>&1 | grep -E 'kex: algorithm|host key algorithm|cipher:' | head -4

When this stage fails you see "no matching key exchange method found" or a similar line, which means the two lists share nothing, usually because one side is very old.

Stage 2: How are the session keys created?

Through a key exchange that lets both sides compute the same secret without ever sending it. Classic SSH uses elliptic curve Diffie-Hellman over Curve25519: each side makes a temporary key pair, swaps the public halves and combines them with its own private half to reach the same shared value. Recent OpenSSH goes further. Release 9.0 made the hybrid sntrup761x25519-sha512 method the default, and release 10.0 switched to mlkem768x25519-sha256, which pairs Curve25519 with ML-KEM, a post-quantum algorithm standardized by NIST. A recording of today's session should stay unreadable even to a future quantum computer.

ImageOpenSSHDefault key exchange
Debian 1310.0mlkem768x25519-sha256
Ubuntu 24.049.6sntrup761x25519-sha512
Debian 129.2sntrup761x25519-sha512
Rocky Linux 9, AlmaLinux 98.7curve25519-sha256, per system crypto policy

From the shared secret, both sides derive separate keys for each direction of traffic. The temporary key pairs are thrown away, which gives forward secrecy: stealing the server's long-term host key later does not decrypt old sessions.

Stage 3: How do I know I reached the real server?

The server signs the exchange with its long-term host key, usually ed25519. Your client checks that signature against the key stored in ~/.ssh/known_hosts. On the first visit there is nothing stored, so the client shows the fingerprint and asks you to confirm. Later, a mismatch produces the big "REMOTE HOST IDENTIFICATION HAS CHANGED" warning. On our machines, a reinstall from the panel creates new host keys, so that warning is expected after a reinstall and suspicious at any other time.

Stage 4: Which encryption protects the data?

Symmetric ciphers, which are fast. OpenSSH prefers chacha20-poly1305@openssh.com and the AES-GCM family, both of which encrypt and authenticate in one step. Every Intel, AMD and Ampere processor we sell accelerates AES in hardware, and ChaCha20 is fast in software, so SSH encryption never limits a normal session or file copy. Keys are renewed during long sessions after a set amount of data or time.

Stage 5: How does login work?

Now encrypted, the client asks to authenticate as a user. The server answers with the methods it accepts, such as publickey and password. With public key login, the client proves it holds the private key by signing a value tied to this unique session; the private key never leaves your computer and the signature cannot be replayed against another session. With password login, the password travels inside the encrypted tunnel and the server checks it against the system's password hash. The error "Permission denied (publickey)" comes from this stage; Fix "Permission denied (publickey)" explains how to fix it.

Stage 6: What are channels?

After login, everything runs in channels: numbered streams multiplexed over the single connection. Your interactive shell is one session channel. A port forward with -L opens a direct-tcpip channel for each connection through it. SFTP is a session channel running the sftp subsystem. Because channels share one connection, features like ControlMaster can reuse a logged-in connection for new sessions in milliseconds; The SSH config file shows how.

Why does this matter on a dedicated server?

Because nearly every fault you will meet maps to one stage. Refused or timed-out connections are TCP problems before stage 1. Algorithm mismatches are stage 1. Host key warnings are stage 3. Permission errors are stage 5. Port forwards that open but do not pass data are stage 6, usually because the target service is not listening.

Keep Reading

What is SSH?

The protocol behind every remote login to a Linux box: what it protects, where it came from and what you use it for daily.

Open

SSH vs SSL/TLS

Both encrypt traffic, but SSH runs your admin sessions while TLS protects websites and APIs; here is where each fits.

Open

SSH keys

How SSH key pairs replace passwords, how to make an ed25519 key and how to have it placed on your machine at install time.

Open

The SSH config file

Turn long ssh command lines into short aliases with ~/.ssh/config: hosts, ports, keys, jump hosts, keepalives and connection sharing.

Open

FAQ

Quick Answers

No. The password travels only after the encrypted channel is set up, and with key login no password is sent at all.

The server's long-term key pair. It signs each key exchange so your client can confirm it is talking to the same server as before.

Recent OpenSSH releases use hybrid post-quantum key exchange by default, mlkem768x25519-sha256 in OpenSSH 10.0, to protect recorded sessions.

A reinstall creates new host keys. Check the new fingerprint, then remove the old entry with ssh-keygen -R.

Yes. SSH multiplexes channels, and ControlMaster lets new sessions reuse an existing connection.