Skip to main content
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—
Your laptopCloud network (VPC)ssh clientyoussh-agentholds unlocked keyid_ed25519private key · 600known_hoststrusted host keysBastion hostpublic · port 22Impostor hostdifferent host keyApp serverprivate · sshdauthorized_keysid_ed25519.pubsshd_configpassword login offSSM / IAPno open port 22ssh
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​

MessageCauseFix
Are you sure you want to continue connecting (yes/no...)?First connect, host key not in known_hostsCompare 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 middleVerify with the owner or console, then ssh-keygen -R host
WARNING: UNPROTECTED PRIVATE KEY FILE!Private key readable by otherschmod 600 ~/.ssh/id_ed25519
Permission denied (publickey)Wrong user, key not in authorized_keys, bad modes on the server, or wrong key offeredCheck ssh -v, the server log, and modes 700 / 600
Connection timed outFirewall or security group blocks port 22, or no route to a private IPCheck the rules, go through the bastion, or use SSM / IAP
Connection refusedHost is reachable but nothing listens on the portIs sshd running? Is it on another port?
Host key verification failed.You answered no, or batch mode refused an unknown hostAdd 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

  1. During key login, what does the client send to prove who you are?
  2. ssh stops with REMOTE HOST IDENTIFICATION HAS CHANGED. What is the right first move?
  3. Keys are set up, but login fails with Permission denied (publickey). The server log says the authorized_keys file has bad ownership or modes. Why?

Further reading: ssh(1) · sshd_config(5) · AWS Session Manager · IAP TCP forwarding · Cryptography a Developer Needs · How a Request Travels Inside a VPC