Foundations: web, servers, containersLesson 8 of 13
How SSH Keys Get You Into a Server
Follow one login from your laptop to a server in a private subnet. See which key proves who you are, which key proves the server is real, how a bastion relays the connection, and what replaces port 22 in the cloud.
You provePassword?
Server proves—
Pathlaptop → server
Result—
RequestResponse / reportWatchAppears in a later step
What you will learn
- A key pair: the private key stays, the public key goes to authorized_keys
- Host key checks and known_hosts, and what the change warning means
- ssh-agent and ProxyJump through a bastion
- Turning off password login and fixing key file permissions
- Session Manager and IAP: access without an open port 22
SSH errors and what they mean
| Message | Cause | Fix |
|---|---|---|
Are you sure you want to continue connecting (yes/no...)? | First connect, host key not in known_hosts | Compare the fingerprint with one from a trusted source, then yes |
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! | Host key differs from known_hosts: rebuilt server, reused IP, or a man in the middle | Verify with the owner or console, then ssh-keygen -R host |
WARNING: UNPROTECTED PRIVATE KEY FILE! | Private key readable by others | chmod 600 ~/.ssh/id_ed25519 |
Permission denied (publickey) | Wrong user, key not in authorized_keys, bad modes on the server, or wrong key offered | Check ssh -v, the server log, and modes 700 / 600 |
Connection timed out | Firewall or security group blocks port 22, or no route to a private IP | Check the rules, go through the bastion, or use SSM / IAP |
Connection refused | Host is reachable but nothing listens on the port | Is sshd running? Is it on another port? |
Host key verification failed. | You answered no, or batch mode refused an unknown host | Add the verified key to known_hosts first |
Try it yourself
# Make a key pair (with a passphrase) and look at it
ssh-keygen -t ed25519 -C "you@laptop" -f ~/.ssh/id_ed25519
ssh-keygen -lf ~/.ssh/id_ed25519.pub # fingerprint of your key
ls -l ~/.ssh # private key should be -rw-------
# Load it into the agent once
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ssh-add -l
# Install the public key on a server (asks for the password one last time)
ssh-copy-id -i ~/.ssh/id_ed25519.pub dev@server.example.com
# Jump through a bastion, once or via ~/.ssh/config
ssh -J dev@bastion.example.com dev@10.0.2.15
cat >> ~/.ssh/config <<'EOF'
Host app-server
HostName 10.0.2.15
User dev
ProxyJump dev@bastion.example.com
EOF
ssh -v app-server # -v shows which key is offered and why it fails
# On the server: keys only
sudo sshd -t && sudo sshd -T | grep -Ei 'passwordauthentication|kbdinteractive|permitrootlogin'
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys
# Host key changed and verified as legitimate?
ssh-keygen -R server.example.com
# Cloud, no open port 22
aws ssm start-session --target i-0123456789abcdef0
gcloud compute ssh my-vm --zone us-central1-a --tunnel-through-iap
Check yourself
Further reading: ssh(1) · sshd_config(5) · AWS Session Manager · IAP TCP forwarding · Cryptography a Developer Needs · How a Request Travels Inside a VPC