Sub topic 8
Logging in with Your Key and Verifying the Host
This chapter covers the first login with your new key. It assumes you can use the ssh command with a password and have already generated a key pair and installed the public key in ~/.ssh/authorized_keys on the server. It also assumes you are comfortable with Unix file permissions. The chapter has two goals. The first is to complete a key-based login and confirm that the key, not your password, did the work. The second is to understand and verify the host key prompt, which is the one moment in SSH where you can be tricked.
Two separate checks happen on every connection
An SSH connection verifies identity in both directions, and the two checks use different keys.
- Host authentication. Your client checks that the server is the machine you meant to reach. The server proves this with its host key, a key pair stored on the server (usually in
/etc/ssh/). - User authentication. The server checks that you are who you claim to be. You prove this with your user key, the private key you generated.
Host authentication happens first. If it fails or you ignore a warning, nothing about your key matters, because you might be handing a login attempt to an impostor.
The diagram shows the order of events in a typical first connection.
Making the first login
Run ssh with the user and host. If your key is at the default path ~/.ssh/id_ed25519, the client tries it automatically.
ssh [email protected]On the first connection to a host, the client has no record of it and prints a prompt like this:
The authenticity of host 'server.example.com (203.0.113.10)' can't be established.
ED25519 key fingerprint is SHA256:Q3k0Vf1m0cT8m0m1QmJ0r7n1cV9mB2m3pQxYz4aBcDe.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?The client is saying that it cannot tell whether this key belongs to the real server. It shows a fingerprint, a short hash of the host's public key. A fingerprint is easier to compare than the whole key.
Verifying the fingerprint
Typing yes without checking is the common habit, and it is the weak point. An attacker who can intercept your connection (a man-in-the-middle) can present their own host key. If you accept it, your client stores the attacker's key as trusted.
To verify, get the real fingerprint through a channel you already trust, separate from the network path you are connecting over. Options include:
- The server's console, such as a cloud provider's web console or a physical or virtual terminal.
- A previously established SSH session to the same server.
- Your provider's documentation or dashboard, if it publishes fingerprints.
- Your administrator, by a channel other than the connection itself.
On the server, print the fingerprint of each host key:
for f in /etc/ssh/ssh_host_*_key.pub; do ssh-keygen -lf "$f"; doneEach line shows the key size, the fingerprint, a comment, and the key type. Compare the ED25519 line to what your client displayed. Match the SHA256: string character by character, or at least the start and end. If they match, type yes.
Recent OpenSSH versions also accept the fingerprint itself at the prompt, as the [fingerprint] option shows. Pasting the fingerprint you obtained out of band means the client compares it for you.
The client picks the key type to display based on the algorithms it and the server share, so you may see ECDSA or RSA instead of Ed25519. Compare against the line of the same type.
What happens after you answer yes
The client appends the host's key to ~/.ssh/known_hosts and prints a notice:
Warning: Permanently added 'server.example.com' (ED25519) to the list of known hosts.Despite the word "Warning", this is normal. On later connections the client compares the key the server presents with the saved one and connects silently if they match.
You can inspect the file. By default, host names are hashed (HashKnownHosts yes on many distributions), so entries look like random strings. You can still search for a host:
ssh-keygen -F server.example.comYou can also fetch a host's key ahead of time with ssh-keyscan. This is useful for scripts, but it fetches whatever key the network gives you, so it has the same trust problem as typing yes. Only use it if you then compare the printed fingerprints with a trusted source:
ssh-keyscan -t ed25519 server.example.com > /tmp/server.key
ssh-keygen -lf /tmp/server.keyCompleting the user authentication
After the host is trusted, the server tests your key. If your private key has a passphrase, the client asks for it (or uses a key already loaded in ssh-agent, covered in the next chapter):
Enter passphrase for key '/home/alice/.ssh/id_ed25519':This passphrase unlocks the private key on your own machine. It is never sent to the server. If authentication succeeds you get a shell prompt on the server with no account password requested.
Confirming that the key did the work
A login that succeeds is not proof that the key was used. If key authentication fails silently, the client falls back to a password prompt, and you may type a password without noticing the difference. To test the key alone, forbid every other method:
ssh -o PreferredAuthentications=publickey -o PasswordAuthentication=no [email protected]If the key is not accepted, this command fails with Permission denied (publickey) instead of falling back to a password. That makes it a reliable test.
For more detail, add -v. The lines to look for are:
debug1: Offering public key: /home/alice/.ssh/id_ed25519 ED25519 SHA256:...
debug1: Server accepts key: /home/alice/.ssh/id_ed25519 ED25519 SHA256:...
Authenticated to server.example.com ([203.0.113.10]:22) using "publickey".Offering public key means the client tried your key. Server accepts key means the server found the matching public key in authorized_keys. The final line names the method that succeeded. If you see using "password", key authentication did not work.
On the server you can also check the authentication log. The path depends on the distribution. Debian and Ubuntu use /var/log/auth.log. Many other systems use the journal:
sudo journalctl -u ssh -n 20A successful key login shows Accepted publickey for alice from ..., and a password login shows Accepted password for alice. Use -u sshd instead of -u ssh on systems where the service has that name.
When the host key changes
If you connect to a known host and the key differs from the saved one, the client stops with this message:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@The client refuses to continue. There are two broad explanations.
- A legitimate change. The server was reinstalled, its host keys were regenerated, or a new machine reuses the old name or IP address. This is common with cloud instances that are rebuilt.
- An attack. Someone is intercepting the connection.
Do not just remove the entry. First find out why the key changed, by asking the administrator or checking the fingerprint on the console as described above. Once you have confirmed that the change is legitimate, remove the old entry and reconnect:
ssh-keygen -R server.example.com
ssh [email protected]ssh-keygen -R deletes the matching entries from known_hosts and saves the old file as known_hosts.old. The next connection shows the first-time prompt again, and you verify the new fingerprint as before.
Common first-login outcomes
| What you see | Meaning | Next step |
|---|---|---|
| Shell prompt, no password asked | Key accepted | Done. Confirm with `-v` if unsure |
| Passphrase prompt, then shell | Key accepted, key is encrypted | Done. Use `ssh-agent` to avoid repeats |
| Password prompt | Key not accepted, fallback to password | Run the `-v` command above |
| `Permission denied (publickey)` | Key rejected and no fallback allowed | Check `authorized_keys` content and permissions |
| `Host key verification failed` | Key mismatch or you answered `no` | Verify the fingerprint, then fix `known_hosts` |
| `REMOTE HOST IDENTIFICATION HAS CHANGED` | Saved key differs from presented key | Investigate before removing the entry |
Keep a second session open
Whenever you change authentication settings, keep one working login open in a separate terminal until a fresh connection succeeds. An existing session stays alive even when a new login would fail, so it gives you a way back in to fix mistakes. The later chapter on disabling password logins relies on this habit, so adopt it now.
Exercise
You connect to a new server for the first time and the client shows an ED25519 fingerprint. You then log in with your key. Describe how you would (1) confirm the fingerprint is genuine before typing yes, and (2) prove afterward that the login used your key and not a password. Give the specific commands.
Show answer
(1) Get the fingerprint through a separate trusted channel, for example the provider's web console. On the server run for f in /etc/ssh/ssh_host_*_key.pub; do ssh-keygen -lf "$f"; done and compare the ED25519 line with the one the client displayed. Type yes only if they match. (2) Connect with ssh -o PreferredAuthentications=publickey -o PasswordAuthentication=no [email protected]. If it succeeds, the key worked, because password fallback is disabled. Adding -v shows Server accepts key and Authenticated ... using "publickey". Server logs would show Accepted publickey for alice.
Exercise
After a server is rebuilt, your client prints WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!. What should you do before running ssh-keygen -R, and why?
Show answer
Confirm that the change is legitimate, for example by asking the administrator or checking the new fingerprint on the server console. The warning can mean an attacker is intercepting the connection, and deleting the entry without checking would make your client trust the attacker's key. Once the new fingerprint is verified, run ssh-keygen -R server.example.com, reconnect, and verify the fingerprint at the first-time prompt.