What is an SSH client's first-connection host-key fingerprint prompt asking you to confirm, and what does known_hosts protect against afterwards?
answer
- server proves identity, not you
- nothing to compare on first contact
- pinned key per host name
- changed key means refuse, not re-ask
basics
~20 sThe prompt asks whether the presented host key really belongs to that server. Once accepted, the key is pinned in known_hosts, and a later server presenting a different key, possibly a man-in-the-middle, is refused by default.
solid answer
~40 sIn SSH every server proves its identity by signing the key exchange with its **host key**. On first contact the client has nothing to compare against, so OpenSSH's default `StrictHostKeyChecking ask` shows the key's fingerprint and asks you to accept it: that is **trust on first use** (TOFU). Accepting writes a line to `~/.ssh/known_hosts`, and every later connection to that name is checked automatically; with the default setting a different key makes the client refuse to connect, because an interceptor would have to present a key of its own. TOFU therefore protects every connection *after* the first; the first is only safe if you compared the fingerprint through another channel. Even so it is far better than Telnet or rlogin, which sent everything, passwords included, in clear text.
go deeper
Recall that the prompt is about the server's identity, that accepting pins its key in known_hosts, and that a changed key makes the client refuse to connect by default.
Explain how the host-key signature over the handshake proves possession, how StrictHostKeyChecking values differ, and why accept-new keeps continuity where no does not.
Show judgement about a changed-key alert: list the legitimate causes, insist on out-of-band confirmation, and refuse to let scripts disable host checking.
Frame TOFU as a first-contact gap and argue when a fleet should replace it with host certificates or published fingerprints instead of training people to click yes.
## What a host key is An SSH server owns one or more long-lived key pairs called **host keys** (typically Ed25519, ECDSA and RSA). They have nothing to do with the key you may use to log in: a **user key** proves who *you* are to the server, a **host key** proves who the *server* is to you. During every connection the client and server run a fresh key exchange that produces the session keys. The server then signs the **exchange hash** of that handshake with its private host key and sends the public half along. The client checks two things: - the signature is valid, so whoever answered holds the private key for the public key it presented; - that public key is the one that *should* belong to the name the user typed. The first check is pure cryptography. The second is a trust question, and SSH's architecture document (RFC 4251, section 4.1) offers two answers: a **local database** mapping host names to keys, or a **certification authority** whose signature vouches for the mapping. OpenSSH's local database is `known_hosts`. ## What the first-connection prompt is asking On first contact the client has no entry for the host, so it cannot decide the second check by itself. With the default `StrictHostKeyChecking ask`, OpenSSH prints the key type and fingerprint and asks whether to continue. The question is really: *"Do you know that this fingerprint belongs to the machine you meant to reach?"* The honest way to answer is to compare it with a fingerprint obtained out of band, for example one the server's administrator published or one read on the server's own console: ```bash # on the server's console, not over the network you are trying to verify ssh-keygen -l -f /etc/ssh/ssh_host_ed25519_key.pub ``` RFC 4251 is candid about the alternative: accepting a key unchecked on first connection still protects against **passive** eavesdropping, but it is open to an **active man-in-the-middle** who answers that first connection with a key of its own. That is the price of trust on first use. ## What known_hosts does on every later connection Accepting the key appends a line to `~/.ssh/known_hosts` (system-wide entries live in `/etc/ssh/ssh_known_hosts`): host name patterns, key type and the base64 public key. From then on every connection to that name is compared automatically, whatever the setting, and the setting decides what happens on a miss: | `StrictHostKeyChecking` | Unknown host | Changed key | |---|---|---| | `yes` | refuse; add keys by hand | refuse | | `accept-new` | add automatically | refuse | | `ask` (default) | prompt, add if confirmed | refuse | | `no` / `off` | add automatically | connect anyway, with some restrictions | So the pinned key is what defends you. An attacker who later intercepts the connection, for instance through a spoofed DNS answer or a hijacked route, cannot produce a signature from the real host key and must present a different one, which the client reports as a changed host identification and refuses. ## What a changed-key warning does and does not mean A mismatch means *the key changed*, not *you are under attack*. Common legitimate causes: 1. the server was rebuilt or reinstalled and generated new host keys; 2. the name or address now points at a different machine; 3. an administrator rotated the host keys. The warning exists because, from the client's side, those cases look exactly like interception. The right reaction is to find out which it is, through a channel other than the suspicious connection, before trusting the new key. Removing the stale entry afterwards is a routine host operation covered with the material on operating OpenSSH. ## Where TOFU sits - **Telnet and rlogin**, SSH's clear-text forebears, offered no server authentication and no encryption at all; rlogin even let trusted hosts skip passwords. - **TOFU** adds encryption plus continuity: you are protected from the second connection onwards. - **Host certificates** (`@cert-authority` lines in `known_hosts`) and **SSHFP** DNS records (RFC 4255) replace the first-use guess with something verifiable, which matters once hosts are many or rebuilt often. The same check runs for every program that rides on SSH, including `sftp`, `scp` and automated clients, which is why disabling it in a script throws away the only server authentication the connection has.
- Why is `StrictHostKeyChecking no` in an automation script worse than `accept-new`?With `no`, ssh adds unknown keys automatically and also lets connections to hosts whose key has *changed* proceed, so a man-in-the-middle is not stopped. `accept-new` still adds unknown hosts automatically but refuses changed keys, keeping continuity. Better still is to pre-seed `known_hosts` with verified keys or trust a host CA, so the first connection is protected too.
- Does the host key encrypt the SSH session?No. The session keys come from the key exchange run fresh for each connection; the host key only signs the exchange hash to prove the server's identity. Someone who later steals a host key can impersonate the server from then on, but cannot use it to decrypt sessions recorded earlier.
- What does `CheckHostIP yes` add to the known_hosts check?It also records and checks the server's IP address in `known_hosts`, so a host key that suddenly differs for an address can reveal DNS spoofing. It is off by default in OpenSSH's client.
Saving a new contact the first time they ring: after that you notice when someone else calls claiming to be them, but you had no way to know the very first call was genuine unless you checked the number elsewhere.
- Typing yes at the fingerprint prompt is always safe; it is a formality.
- known_hosts stores the keys I use to log in to servers.
- A changed host key always means an attack is in progress.
- Trust on first use also protects the very first connection.
- The server's host key is what encrypts the session traffic.