TodayTools

Secure Shell

How SSH works

A safe terminal on a far-away computer

Donate
L7ApplicationPort 22TCP
What brings you here?

Watch it happen

How much detail

Logging in to a server with a key: version exchange, key exchange, then a public key login.

Daniel's laptop
Home router
Internet
build.example.com
Wi-FiInternetSetupRequestResponseEncrypted

6 steps, 18 packets

First up: TCP and versions. Press Play to watch it happen, one step at a time.

0 of 60/6

What is SSH?

SSH lets you log in to another computer and type commands as if you sat in front of it. Everything, including your password or key login and every keystroke, is encrypted. It also copies files (scp, SFTP) and forwards ports through the secure link.

SSH checks both sides. The server proves who it is with a host key that your computer remembers. You prove who you are, ideally with a key pair, where the private half never leaves your device. That is why it replaced old plain-text tools like Telnet.

Think of it like this. SSH is like a private phone line to a building's caretaker. When the call connects, you check the caretaker's voice matches the one you know (the host key). Then you show a pass only you could have made (your key signature). After that, everything you say is scrambled for anyone listening in.

Step by step

Normal

6 steps

Logging in to a server with a key: version exchange, key exchange, then a public key login.

  1. 1

    TCP and versions

    Port 22, then two text lines

    After the TCP handshake, each side sends one plain line such as SSH-2.0-OpenSSH_9.6. Both must speak SSH version 2.

  2. 2

    Algorithm lists

    KEXINIT from both sides

    Each side lists the key exchange, host key, cipher and MAC methods it supports. The first match on the client's list wins. Nothing is secret yet.

  3. 3

    Key exchange

    curve25519 and the host key

    The client sends a fresh Curve25519 public key. The server answers with its own, plus its host key and a signature. Both compute the same secret. The client checks the host key against known_hosts. Newer versions often pick a hybrid post-quantum method such as mlkem768x25519-sha256 instead, but the steps are the same.

  4. 4

    New keys

    Encryption turns on

    Each side sends NEWKEYS. Every packet after this is encrypted and carries an integrity tag.

  5. 5

    Public key login

    A signature, never the private key

    The client asks for the ssh-userauth service, then offers its public key. The server says that key is allowed, and the client proves it holds the private key by signing the session. The private key never leaves the laptop.

  6. 6

    Shell open

    A session channel starts

    The client opens a session channel and asks for a terminal and a shell. Every keystroke now travels as a small encrypted packet.

Host key changed

3 steps

The server shows a different host key than last time, so the client stops before logging in.

  1. 1

    Start as usual

    Versions and KEXINIT

    Nothing looks different at first. The version lines and algorithm lists go back and forth.

  2. 2

    Unexpected host key

    Fingerprint does not match known_hosts

    The key exchange reply carries a host key with a different fingerprint than the one saved last time. Either the server was rebuilt, or someone is sitting in the middle pretending to be it.

  3. 3

    Client refuses

    Warning and disconnect

    The client prints REMOTE HOST IDENTIFICATION HAS CHANGED and Host key verification failed, then closes. It never sends your key signature or password. Check the new fingerprint with the server's owner before you accept it.

Password guessing

4 steps

A bot guesses passwords, but the server only accepts keys and then blocks the address.

  1. 1

    A bot connects

    Bots scan port 22 all day

    Any server on the internet with port 22 open gets login attempts within minutes. They try common names like root or admin and common passwords.

  2. 2

    Password refused

    Only publickey is allowed

    The server is set to PasswordAuthentication no. Every password attempt fails at once, and the reply lists publickey as the only method that can continue.

  3. 3

    Too many tries

    Disconnect after MaxAuthTries

    After a few failures sshd drops the connection. A ban tool on the server, or the PerSourcePenalties setting in OpenSSH 9.8 and later, notices the repeat failures from one address.

  4. 4

    Address blocked

    New connections are dropped

    The firewall now silently drops new packets from that address for a while. The bot wastes its time, and the logs stay quiet.

Myths and mix-ups

  • Moving SSH to another port does not secure it; it only cuts down log noise from bots.
  • With key login, your private key is not sent to the server; only a signature is.
  • The host key warning is not a glitch to click past; it is the check that stops a fake server.
  • SSH is not only for terminals; it also copies files and tunnels other connections.

Try it yourself

Run these on your own computer to see SSH at work.

  • Watch the handshake

    ssh -v user@example.com

    Debug lines show the version exchange, the chosen algorithms, the host key and each login method tried.

  • Make a key pair

    ssh-keygen -t ed25519

    Creates a private key and a .pub file. Put the .pub contents in the server's ~/.ssh/authorized_keys.

  • See a server's host key

    ssh-keyscan -t ed25519 example.com | ssh-keygen -lf -

    Prints the fingerprint so you can compare it with what the owner gives you.

  • List supported key exchanges

    ssh -Q kex

    Shows the key exchange methods your SSH client knows.

SSH at a glance

Layer
Application (7)
Default port
22
Transport
TCP
Version
SSH-2 (SSH-1 is insecure and retired)
Key exchange
curve25519-sha256, newer hybrid post-quantum methods
Host keys
Ed25519, ECDSA or RSA (rsa-sha2)
Ciphers
chacha20-poly1305, AES-GCM, AES-CTR
Year
1995 (SSH-2 standardised in 2006)

Defined inRFC 4251 (architecture)RFC 4252 (authentication)RFC 4253 (transport)RFC 4254 (connection)RFC 8709 (Ed25519 keys)

Header fields

What SSH puts in its messages, with example values from the stories above.

SSH

Packet length
plain
Message
Version line (plain text)
Text
SSH-2.0-OpenSSH_9.6Both sides must say SSH-2.0. The old SSH-1 is insecure and refused.
Cookie
16 random bytes
kex_algorithms
curve25519-sha256, ...How the two sides will agree on a shared secret.
encryption
chacha20-poly1305@openssh.com, aes128-gcm@openssh.com, ...
server_host_key_algorithms
ssh-ed25519, ecdsa-sha2-nistp256, rsa-sha2-512
Client public key (Q_C)
32 bytesMade new for this session and thrown away after, so old sessions stay safe even if a key leaks later.
Host key (K_S)
ssh-ed25519, SHA256:Wq3cX5mL0rTn7aF2hJ8kP4vB9yE1dG6sU0iO3zR5xQcThe server's long-term identity. Your laptop remembers it in ~/.ssh/known_hosts after the first visit.
Server public key (Q_S)
32 bytes

SSH (encrypted)

Packet length
encryptedWith chacha20-poly1305 even the length is encrypted, so an onlooker learns very little.
Message
5 (SSH_MSG_SERVICE_REQUEST)
Service
ssh-userauth
MAC / auth tag
16 bytesAny change on the way breaks this tag and the connection is dropped.
User
daniel
Method
passwordEven this guess is encrypted on the wire, but the server can see it, which is why weak passwords are risky.
Key
ssh-ed25519 AAAAC3Nza...The server looks for this key in daniel's ~/.ssh/authorized_keys.
Signature
ssh-ed25519 over the session IDTied to this one session, so a recorded signature is useless anywhere else.
Channel type
session
Window size
2097152SSH has its own flow control per channel, so one busy channel cannot starve others.

Test yourself

Question 1 of 5Score 0

During public key login, what does the client send to the server?

Questions people ask

What port does SSH use?

TCP port 22 by default. A server can listen on another port; connect with ssh -p 2222 user@host.

Is SSH key login safer than a password?

Yes. A key cannot be guessed, is never sent to the server and cannot be reused by a fake site. Protect the private key with a passphrase.

What does Host key verification failed mean?

The server's host key does not match the one saved in your known_hosts file. Confirm the new fingerprint with the server's owner before removing the old line with ssh-keygen -R host.

What is the difference between SSH and SFTP?

SFTP is a file transfer protocol that runs inside an SSH connection. SSH gives you the secure link; SFTP is one of the things you can do over it.

Can someone see what I type over SSH?

No. After the key exchange everything is encrypted. An onlooker can see that you are connected and roughly how much data moves.

Donate