SkillPilotAI

SSH: keys, host keys and tunnels

SSH (Secure Shell, TCP 22) gives you an encrypted remote login, replacing plain-text protocols like Telnet. Beyond the shell it gives key-based authentication, host key checks that stop MITM attacks, and tunnels (-L, -R, -D, -J) that safely reach into private networks.

134.1 Why is it used?

  • With Telnet (TCP 23) the username and password (user: dev, password: lab-pass-134) cross the cafe Wi-Fi in plain text; any snooper can read them.
  • SSH encrypts everything: the snooper sees only random bytes like 8f 2a c1 9e ...
  • Key auth removes passwords (and password-guessing bots) from logins.
  • Tunnels let you reach internal dashboards, databases and servers through one bastion host.

134.2 How the SSH handshake works

SSH handshake in five steps
  1. 1.Version exchange: both send SSH-2.0-OpenSSH_9.7 on TCP 22

  2. 2.Key exchange: kex sntrup761x25519-sha512, both send public values

  3. 3.Bastion sends its host key (ssh-ed25519) + signature; both derive the same shared secret (never sent)

  4. 4.Host key check: laptop matches the fingerprint against ~/.ssh/known_hosts

  5. 5.User auth inside the encryption (password or publickey)

  6. 6.Encrypted channel ready: commands and output are random bytes on the wire

Terminal
ssh -v dev@bastion true
Output
debug1: Local version string SSH-2.0-OpenSSH_9.7debug1: Remote protocol version 2.0,  remote software version OpenSSH_9.7debug1: kex: algorithm: sntrup761x25519-sha512@openssh.comdebug1: kex: host key algorithm: ssh-ed25519debug1: Server host key: ssh-ed25519  SHA256:313g3kSmXXDLuyqg6pfvCzTDrzzCdYYrpNdgG9Ulc10debug1: Host 'bastion' is known and matches the ED25519 host key.debug1: SSH2_MSG_NEWKEYS receiveddebug1: Authentications that can continue:  publickey,password,keyboard-interactivedebug1: Next authentication method: passwordAuthenticated to bastion ([192.168.154.20]:22) using "password".
  • The kex is a hybrid post-quantum key exchange; after NEWKEYS everything is encrypted.

134.3 First connect and known_hosts

Terminal
ssh dev@bastion 'hostname; whoami'cat ~/.ssh/known_hosts# on bastion (admin):ssh-keygen -lf \  /etc/ssh/ssh_host_ed25519_key.pub
Output
The authenticity of host 'bastion (192.168.154.20)'  can't be established.ED25519 key fingerprint is  SHA256:313g3kSmXXDLuyqg6pfvCzTDrzzCdYYrpNdgG9Ulc10.Are you sure you want to continue connecting  (yes/no/[fingerprint])? yesWarning: Permanently added 'bastion' (ED25519)  to the list of known hosts.dev@bastion's password: ****bastiondev [bastion]$ ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub256 SHA256:313g3kSmXXDLuyqg6pfvCzTDrzzCdYYrpNdgG9Ulc10  root@bastion (ED25519)
  • The fingerprint shown on first connect must match the one the admin sees on the server (or the cloud console).
  • The first connect is the risky one: a MITM could get its key into known_hosts if you type yes blindly.

134.4 Comparison: password auth vs key auth

PointPassword authKey auth
What you sendThe password (inside encryption)Only a signature over the server's challenge
RiskBrute force by bots day and nightPrivate key theft (protect with a passphrase)
Where things livePassword on server (hashed)Private key only on laptop, public key in ~/.ssh/authorized_keys
SharingNeverPublic key safe to share, private key never
Terminal
ssh-keygen -t ed25519 -N lab-key-134 \  -C laptop@n134 -f ~/.ssh/id_ed25519ls -l ~/.sshssh-copy-id -i ~/.ssh/id_ed25519.pub \  dev@bastionssh -v dev@bastion hostname
Output
Generating public/private ed25519 key pair.Your identification has been saved in /root/.ssh/id_ed25519Your public key has been saved in /root/.ssh/id_ed25519.pubThe key fingerprint is:SHA256:He8dcFeLnSGH8XEyhFXwLdIyF5nonOj7M9t349oUmkA laptop@n134 -rw-------    1 root     root           444 Nov  9 03:50 id_ed25519-rw-r--r--    1 root     root            93 Nov  9 03:50 id_ed25519.pub Number of key(s) added: 1 debug1: Offering public key: /root/.ssh/id_ed25519 ED25519  SHA256:He8dcFeLnSGH8XEyhFXwLdIyF5nonOj7M9t349oUmkAdebug1: Server accepts key: /root/.ssh/id_ed25519 ED25519Enter passphrase for key '/root/.ssh/id_ed25519': ****Authenticated to bastion ([192.168.154.20]:22) using "publickey".bastion
  • -N on the command line is only for the lab; in real life type the passphrase at the prompt.
  • The private key file is -rw------- (owner only); ssh-copy-id logs in once with the password and appends the public key to authorized_keys.

134.5 ssh-agent: unlock once

Terminal
ssh-agent -a /tmp/agent.sockexport SSH_AUTH_SOCK=/tmp/agent.sockssh-add ~/.ssh/id_ed25519ssh-add -lssh dev@bastion \  'echo logged in as $(whoami) on $(hostname)'
Output
Enter passphrase for /root/.ssh/id_ed25519: ****Identity added: /root/.ssh/id_ed25519 (laptop@n134)256 SHA256:He8dcFeLnSGH8XEyhFXwLdIyF5nonOj7M9t349oUmkA  laptop@n134 (ED25519)logged in as dev on bastion

134.6 Host key changed: MITM caught

known_hosts mismatch stops a MITM
  1. 1.known_hosts on the laptop: bastion -> SHA256:313g3kSm...

  2. 2.Attacker on the cafe Wi-Fi pretends to be bastion

  3. 3.Laptop connects; fake bastion sends host key SHA256:DY2e...

  4. 4.Fingerprint does not match known_hosts

  5. 5.SSH stops the connection: Host key verification failed

Terminal
# on bastion: new host keys (like a reinstall)rm -f /etc/ssh/ssh_host_*; ssh-keygen -A# on laptop:ssh dev@bastion hostnamessh-keygen -R bastionssh dev@bastion hostname
Output
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!The fingerprint for the ED25519 key sent by the remote host isSHA256:DY2ea9Fe+CB0ZBeDim43Jk9EXBjO6op+gyuJRGkNwxA.Offending ECDSA key in /root/.ssh/known_hosts:3Host key verification failed. $ ssh-keygen -R bastion# Host bastion found: line 1/root/.ssh/known_hosts updated.
  • Use ssh-keygen -R only when you know the server was really reinstalled, and say yes only after verifying the new fingerprint.

134.7 Tunnels: -L, -R, -D and -J

Local forward -L: laptop:8080 -> web.internal:8000
  1. 1.Direct curl to 192.168.154.140:8000 times out (private network)

  2. 2.ssh -f -N -L 8080:192.168.154.140:8000 dev@bastion

  3. 3.Laptop listens on 127.0.0.1:8080

  4. 4.Browser request goes through the SSH tunnel to bastion:22

  5. 5.Bastion (192.168.154.130 side) connects to web.internal:8000

  6. 6.Web log shows client 192.168.154.130: it sees the bastion, not the laptop

FlagListens onTraffic goes toUse case
-L 8080:host:8000Client (laptop)A destination on the server sideInternal dashboard or DB behind a bastion
-R 9000:localhost:3000Server (bastion), localhost by defaultAn app on the client sideShow a laptop dev app to the bastion
-D 1080Client, as a SOCKS proxyAny destination named per requestReach every site in the private network
-J dev@bastion-A second SSH session through the bastionShell on an internal server, key never copied to bastion
Terminal
ssh -f -N -L 8080:192.168.154.140:8000 dev@bastioncurl -sS http://127.0.0.1:8080/ ssh -f -N -R 9000:localhost:3000 dev@bastion# on bastion:curl -sS http://127.0.0.1:9000/ ssh -f -N -D 1080 dev@bastioncurl -sS -m 5 --socks5 127.0.0.1:1080 \  http://web.internal:8000/curl -sS -m 5 --socks5-hostname \  127.0.0.1:1080 http://web.internal:8000/ ssh -J dev@bastion dev@web.internal \  'hostname; ip -4 -o addr show eth0'
Output
$ curl -sS http://127.0.0.1:8080/internal dashboard: only inside n134-int[web]$ cat /tmp/web.log192.168.154.130 - - [09/Nov/2026 03:50:53] "GET / HTTP/1.1" 200 - [bastion]$ ss -ltn | grep 9000LISTEN 0      128        127.0.0.1:9000       0.0.0.0:*[bastion]$ curl -sS http://127.0.0.1:9000/hello from laptop dev app :3000 $ curl -sS -m 5 --socks5 127.0.0.1:1080 http://web.internal:8000/curl: (97) Could not resolve host: web.internal$ curl -sS -m 5 --socks5-hostname 127.0.0.1:1080 http://web.internal:8000/internal dashboard: only inside n134-int web2: eth0    inet 192.168.154.140/25 brd 192.168.154.255 scope global eth0
  • -f -N: go to background, run no remote command (tunnel only).
  • --socks5 resolves the name on the laptop (fails for internal names); --socks5-hostname lets the bastion resolve it.

134.8 Common mistakes

  • Typing yes on the first connect without checking the fingerprint.
  • Thinking the private key goes to the server at login: only a signature is sent; never share the private key by email or in a Git repo.
  • Running ssh-keygen -R automatically every time the warning appears: it may really be a MITM.
  • Using --socks5 instead of --socks5-hostname for internal names.
  • Copying your private key onto the bastion instead of using -J or the agent.

134.9 Interview tips

  • Handshake order: version -> key exchange -> host key check -> user auth -> encrypted channel.
  • -L opens a port on the client to a server-side destination; -R opens a port on the server back to the client; -D is a SOCKS proxy.
  • Key auth: server sends a challenge, client signs with the private key, server verifies with authorized_keys.
  • known_hosts protects against MITM; the remote host identification has changed warning means stop and verify.