Sub topic 8
Logging in with Your Key and Verifying the Host
Assume you can use the command line, have already created an SSH key pair, and have installed your public key in the server account's ~/.ssh/authorized_keys file. You also need the server's expected host-key fingerprint from a trusted source, such as your administrator or cloud provider.
The first key-based login
Specify the private key with -i when you connect:
ssh -i ~/.ssh/id_ed25519 [email protected]If the private key has a passphrase, SSH asks for it. The passphrase unlocks the private key on your computer. It is not sent to the server.
During the first connection, SSH displays the server's host key fingerprint:
The authenticity of host 'server.example.com' can't be established.
ED25519 key fingerprint is SHA256:example-fingerprint-here.
Are you sure you want to continue connecting (yes/no/[fingerprint])?A host key is the server's cryptographic identity. It lets your SSH client detect whether it is connecting to the same server on later connections.
Compare the displayed fingerprint with a trusted fingerprint. Type yes only when they match. SSH then stores the host key in ~/.ssh/known_hosts.
Why host verification matters
Public key authentication answers, “Can this client prove it has the private key for Alice?” Host verification answers, “Is this really Alice's intended server?”
Without host verification, an attacker could place a server between you and the real server. The attacker might collect passwords or relay connections. This attack is called a man-in-the-middle attack.
The normal trust sequence is:
- Obtain the expected fingerprint through a separate trusted channel.
- Connect to the server.
- Compare the fingerprint shown by SSH.
- Accept the key only if the values match.
- Let SSH remember the accepted key in
~/.ssh/known_hosts.
Handling a changed host key
If a known host's key changes, SSH refuses the connection:
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!This can indicate a legitimate rebuild, a changed IP address, or a possible attack. Do not immediately remove the old key. Confirm the change with the administrator first.
After confirmation, remove the outdated entry by hostname:
ssh-keygen -R server.example.comThen connect again and verify the new fingerprint. If you connect by IP address, remove the IP entry instead:
ssh-keygen -R 203.0.113.20| Situation | Safe action |
|---|---|
| First connection | Verify the fingerprint, then accept it |
| Host was rebuilt | Confirm the new fingerprint, remove the old entry, and reconnect |
| Unexpected change | Stop and investigate before accepting anything |
Confirming which key SSH uses
When several keys exist, SSH might offer the wrong key or too many keys. Use verbose mode:
ssh -v -i ~/.ssh/id_ed25519 [email protected]Look for messages such as Offering public key and Server accepts key. To force SSH to use only the specified identity, add this option:
ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519 [email protected]A successful login should open the remote shell without asking for the account password. A passphrase prompt is expected when the private key is protected.
Exercise
SSH reports that the host key for server.example.com has changed. What should you do before running ssh-keygen -R?
Show answer
Confirm the server rebuild, hostname change, or new fingerprint through a trusted channel. Remove the old entry only after the change is verified.