Lesson 04 of 16 · SSH
How SSH works
What happens when you type ssh user@server: the encrypted channel is set up with a key exchange, the server proves its identity with its host key (checked against known_hosts), and only then do you prove who you are with a key or password. What the famous 'host identification has changed' warning means and what to do about it.
What SSH gives you
SSH (Secure Shell) gives you a remote command line, file copy (scp, sftp) and tunnels, all over one encrypted, authenticated connection, normally on TCP port 22. It replaced Telnet and rlogin, which sent everything, passwords included, as plain text.
The connection, step by step
You: ssh asha@web1.example.com
Client Server (sshd)
│── TCP connect to port 22 ──────────────────────────────►│
│◄─────── versions: SSH-2.0-OpenSSH_9.x ──────────────────│
│── algorithm lists ◄──────────────────► algorithm lists ─│ agree on algorithms
│── key exchange (ECDHE / hybrid post-quantum) ───────────│ 1. shared secret → encryption on
│◄── server HOST KEY + signature over the exchange ───────│ 2. server proves identity
│ client checks host key against ~/.ssh/known_hosts │
│══════════════════ encrypted from here ══════════════════│
│── user authentication: public key (or password) ───────►│ 3. user proves identity
│◄── success → shell, command, file transfer, tunnel ─────│
Three things to notice:
- Encryption starts before you log in. Your password (if used) travels inside the encrypted channel.
- The server proves who it is first, with its host key.
- Then you prove who you are, with a key (lesson 5) or a password.
Modern OpenSSH uses hybrid post-quantum key exchange by default (for example sntrup761x25519-sha512 in OpenSSH 9.x, and mlkem768x25519-sha256 from OpenSSH 10), so traffic recorded today stays safe even against future quantum computers.
Before telling a stranger on the phone your secret password, you'd want to know it's really your bank. SSH does the same: first it makes the line private, then the server shows its ID card (host key), and only then do you show yours.
Host keys and known_hosts
Each server has its own host key pairs, created when SSH was installed:
$ ls /etc/ssh/ssh_host_*_key.pub
/etc/ssh/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_ed25519_key.pub /etc/ssh/ssh_host_rsa_key.pub
$ ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
256 SHA256:Xq3P9mWcT1vL0r8a2bF6kYdN4sHjE7uQzR5oVgC9iMw root@web1 (ED25519)
The first time you connect, your client has never seen this key:
The authenticity of host 'web1.example.com (203.0.113.10)' can't be established.
ED25519 key fingerprint is SHA256:Xq3P9mWcT1vL0r8a2bF6kYdN4sHjE7uQzR5oVgC9iMw.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
- The safe answer is to compare the fingerprint with one obtained another way: from the cloud console, the person who built the server, or your configuration system. You can even paste the expected fingerprint at the prompt.
- Answering
yessaves the key in~/.ssh/known_hosts. This model is called trust on first use (TOFU): the first connection is trusted, and every later one must match it.
"REMOTE HOST IDENTIFICATION HAS CHANGED"
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Offending ED25519 key in /home/asha/.ssh/known_hosts:14
The server's key no longer matches what you saved. Two possibilities:
| Harmless | Dangerous |
|---|---|
| Server rebuilt or re-imaged; IP reused by a new machine; keys rotated | Someone is intercepting the connection (man-in-the-middle) |
What to do: find out which. Check with whoever manages the server or look at the new fingerprint on the server's console. Only when you are sure, remove the old entry for that host and reconnect:
$ ssh-keygen -R web1.example.com
Never "fix" it by deleting the whole known_hosts file or by turning off StrictHostKeyChecking in scripts; that switches off the protection for every server.
How the user proves identity
| Method | How | Notes |
|---|---|---|
| Public key | Your client signs a challenge with your private key; the server checks it against ~/.ssh/authorized_keys |
Recommended; lesson 5 |
| Password | Sent inside the encrypted channel | Guessable; disable on public servers (lesson 6) |
| Keyboard-interactive | Prompts, often for one-time codes | Used for MFA |
| Certificates | A key signed by an SSH certificate authority | For larger fleets (lesson 6) |
Try it
Run ssh -v you@your-server and find these lines in the output: the agreed kex: algorithm, Server host key: ssh-ed25519 SHA256:…, Authentications that can continue: publickey,…, and Authenticated to … using "publickey".
Recap
- SSH sets up encryption first, then the server proves its identity with its host key, then you authenticate.
known_hostsremembers server keys (trust on first use); verify fingerprints the first time.- A changed host key warning means investigate, then remove only that entry with
ssh-keygen -R. - Prefer key-based authentication over passwords.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.