What SSH is
SSH (Secure Shell) gives you an encrypted, authenticated connection to another machine over an untrusted network. It replaced telnet and rsh, which sent everything, passwords included, as plain text. It runs over TCP port 22 and does much more than a shell:
| Remote shell | ssh thirana@kade-api |
| Run one command | ssh kade-api 'journalctl -u kade-api -n 50' |
| Copy files | scp, sftp, rsync -e ssh |
| Git | git@github.com:kade/kade-api.git |
| Tunnels | Carry other TCP connections through the encrypted link (section 8) |
Three layers inside SSH
| # | Layer | Job | Compare with |
|---|---|---|---|
| 1 | Transport | Key exchange, server authentication (host key), encryption and integrity | What TLS does for HTTPS (chapter 25) |
| 2 | User authentication | Proves who you are: public key, password, or others | HTTPS usually leaves this to the app (login, tokens) |
| 3 | Connection | Many channels over one connection: a shell, a file copy, several tunnels at once | HTTP/2 streams (chapter 24) |
Connecting, step by step
| 1 | After the TCP handshake, both sides send a version line in plain text, e.g. SSH-2.0-OpenSSH_9.6. |
| 2 | KEXINIT: each lists the algorithms it supports (key exchange, host key types, ciphers, MACs); the first match on both lists wins. |
| 3 | Key exchange: an elliptic-curve Diffie-Hellman (usually curve25519), exactly the idea from chapter 25. Both sides get a shared secret and a session ID. |
| 4 | The server signs the exchange with its host key. The client checks the signature and whether it knows this host key (section 4). Everything from here on is encrypted. |
| 5 | User authentication: the client proves it holds your private key (section 5). |
| 6 | The client opens a session channel and asks for a shell (or a command, or a tunnel). |
Host keys and "trust on first use"
HTTPS proves the server's identity with a certificate signed by a CA (chapter 25). Plain SSH has no CA. Instead, on the first connection it shows you the server's host key fingerprint and asks:
The authenticity of host 'kade-api (10.10.1.10)' can't be established. ED25519 key fingerprint is SHA256:q3Hk2Y0lK8vJb9nX7yWm1cR4tA5sD6fG8hJ0kL2mN4o. Are you sure you want to continue connecting (yes/no/[fingerprint])?
Answer yes, and the key is saved in ~/.ssh/known_hosts. Every later connection must present the same key. This model is called trust on first use (TOFU): the first connection is the weak moment, so verify the fingerprint through another channel if it matters.
Proving who you are: keys
Passwords work but can be guessed and phished, and bots try them on every server with port 22 open. Public-key authentication is the standard: you keep a private key, and the server holds your public key in ~/.ssh/authorized_keys.
- The private key never leaves your laptop. You prove you hold it by signing this connection's unique session ID. A recorded signature is useless for any other connection.
- Key type: use
ed25519: short, fast and safe. RSA needs at least 3072 bits. - Protect the private key with a passphrase, and let
ssh-agenthold it unlocked for the day. Hardware keys (YubiKey, withed25519-sk) keep it on a device that cannot be copied. - Agent forwarding (
ssh -A) lets a remote machine use your agent. Anyone with root there can use your keys while you are connected. Prefer ProxyJump (section 9).
Managing keys and config
ssh-keygen -t ed25519 -C "thirana@kade.lk" # creates ~/.ssh/id_ed25519 (private) and .pub (public)chmod 600 ~/.ssh/id_ed25519 # SSH refuses private keys others can readA ~/.ssh/config file saves typing and keeps settings in one place:
Host kade-api
HostName 34.87.120.15
User thirana
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 30 # keepalive: survive NAT and firewall idle timeouts (chapter 21)
Host kade-db
HostName 10.10.2.5 # private IP: reachable only through kade-api
User thirana
ProxyJump kade-api # section 9
One key per person, never shared; remove a person's public key when they leave. The ControlMaster option can reuse one connection for many ssh commands (like HTTP keep-alive), which makes repeated commands instant.
Channels: many things over one connection
After login, everything runs as a channel inside the one encrypted TCP connection: your shell is one channel, a file copy another, and each tunnel connection another. This is how one SSH login can carry a shell and three tunnels at once.
Tunnels: port forwarding
A tunnel takes a TCP connection on one side and carries it, inside SSH, to the other side. There are three kinds:
| # | Kind | Flag | Direction | Kadé example |
|---|---|---|---|---|
| 1 | Local | -L | A port on your laptop leads to something the server can reach | Open kade-db's PostgreSQL (private, 10.10.2.5:5432) as localhost:5433 on your laptop |
| 2 | Remote | -R | A port on the server leads back to something your laptop can reach | Show a teammate the Node app running on your laptop's port 3000 (the reverse tunnel from chapter 10) |
| 3 | Dynamic | -D | A SOCKS proxy on your laptop; each app connection goes out from the server | Browse the office's internal admin pages from home, as if you were in the office |
Pick a kind and fill in the ports. The builder writes the command and shows the path:
ssh -N -L 5433:10.10.2.5:5432 kade-api- Your appconnects to localhost:5433
- SSH (encrypted)laptop → kade-api:22
- kade-apiopens TCP to 10.10.2.5:5432
- 10.10.2.5port 5432
Anything that connects to localhost:5433 on your laptop reaches 10.10.2.5:5432, which only kade-api can see. The part from kade-api to 10.10.2.5 is a normal connection inside the VPC, not SSH-encrypted.
Jump hosts and bastions
A bastion (jump host) is one hardened machine you SSH into first, to reach others that have no public access. ProxyJump (ssh -J) does it cleanly: your laptop opens an SSH connection through the bastion to the target, so the end-to-end encryption and your private key stay on your laptop:
ssh -J thirana@kade-api thirana@10.10.2.5 # or just: ssh kade-db, with the config aboveThe bastion only sees an encrypted stream. This is why ProxyJump is safer than logging in to the bastion and running ssh again from there.
SSH in GCP: OS Login and IAP
Opening port 22 to 0.0.0.0/0 invites constant login attempts from bots. GCP offers a better way in:
Wide by design - scroll it sideways.
| IAP TCP forwarding | Your SSH travels inside HTTPS to Google's Identity-Aware Proxy, which checks who you are and whether IAM allows you, then connects to the VM's internal IP. The VM needs no external IP, and port 22 is open only to IAP's address range. |
| OS Login | Linux accounts and SSH access come from IAM instead of keys scattered in VM metadata. Remove someone from the IAM role and their access ends everywhere. It can require 2-step verification. |
| Tunnels too | The same IAP path can forward any TCP port, e.g. kade-db's PostgreSQL to your laptop, with no SSH involved at all (commands below). |
gcloud compute ssh kade-db --zone=asia-southeast1-b --tunnel-through-iapgcloud compute start-iap-tunnel kade-db 5432 --local-host-port=localhost:5433 --zone=asia-southeast1-bpsql -h localhost -p 5433 -U kade # in another terminalHardening SSH
| # | Setting or habit | Why |
|---|---|---|
| 1 | PasswordAuthentication no | Keys only: nothing for bots to guess |
| 2 | PermitRootLogin no | Log in as yourself, then use sudo: every action is tied to a person |
| 3 | No public port 22 (use IAP or a VPN) | The best attack surface is none |
| 4 | If 22 must be public: allow only known source IPs | e.g. only the office's 198.51.100.20/32 (a /32, chapter 3) |
| 5 | Passphrase-protected or hardware keys; OS Login with 2-step verification | A stolen laptop file is not enough |
| 6 | Keep OpenSSH updated; review who has access regularly | Old keys of people who left are a common hole |
Troubleshooting
ssh -v kade-api prints every step of section 3; -vvv prints even more. Common errors:
| # | Error | Meaning | Check |
|---|---|---|---|
| 1 | Connection timed out | SYNs dropped (chapter 16) | Firewall rule for 22; for IAP, the 35.235.240.0/20 rule |
| 2 | Connection refused | RST: nothing listening on 22 | Is sshd running? |
| 3 | Permission denied (publickey) | The server did not accept any key offered | Right user? Public key in authorized_keys? File permissions on the server (~/.ssh 700, authorized_keys 600)? OS Login role? |
| 4 | Host key verification failed | Host key changed (section 4) | Rebuilt VM, or an attack. Confirm, then ssh-keygen -R |
| 5 | Too many authentication failures | Your agent offered too many keys before the right one | IdentitiesOnly yes + IdentityFile in the config |
| 6 | client_loop: send disconnect: Broken pipe after idle | A NAT or firewall forgot the idle connection (chapter 21) | ServerAliveInterval 30 |
Summary
| # | Idea | In one line | New terms |
|---|---|---|---|
| 1-2 | SSH | Encrypted remote access on TCP 22; transport, user auth, connection layers | SSH, channel |
| 3 | Connecting | Version, KEXINIT, ECDH key exchange, host key signature, user auth, channel | KEXINIT, session ID |
| 4 | Host keys | Trust on first use; known_hosts; a changed key means attack or rebuild | host key, TOFU, known_hosts |
| 5-6 | User keys | Sign the session ID with your private key; ed25519; agent; config | authorized_keys, ssh-agent |
| 8 | Tunnels | -L local, -R remote, -D SOCKS | port forwarding, SOCKS |
| 9 | Jump hosts | ProxyJump through a bastion, end-to-end encrypted | bastion, ProxyJump |
| 10 | GCP | IAP TCP forwarding and OS Login: no public 22, IAM-based access | IAP, OS Login |
| 11-12 | Practice | Keys only, no root, no public port; ssh -v and the common errors |
Try it yourself
Every step of a real connection
- What to look for:
kex: algorithm: curve25519-sha256,Server host key: ssh-ed25519,Authenticated to github.com. - From section: 3. Connecting, step by step
ssh -v git@github.comA server's host key fingerprint
- What to look for: Compare it with the one GitHub publishes in its docs.
- From section: 4. Host keys and "trust on first use"
ssh-keyscan -t ed25519 github.com | ssh-keygen -lf -A new key, and the keys in your agent
- Steps:
ssh-keygen -t ed25519, thenssh-add -l - What to look for: The fingerprint of the key you just made.
- From section: 6. Managing keys and config
ssh-keygen -t ed25519ssh-add -lA local tunnel (-N: no shell)
- What to look for: Then
psql -h localhost -p 5433reaches the private database. - From section: 8. Tunnels: port forwarding
ssh -N -L 5433:10.10.2.5:5432 kade-apiA SOCKS proxy
- What to look for: Set the browser's SOCKS proxy to localhost:1080; check your IP at ifconfig.me.
- From section: 8. Tunnels: port forwarding
ssh -N -D 1080 kade-apiSSH with no external IP
- What to look for: Works with only the 35.235.240.0/20 firewall rule.
- From section: 10. SSH in GCP: OS Login and IAP
gcloud compute ssh kade-db --tunnel-through-iap --zone=asia-southeast1-bThe SSH server's effective settings
- What to look for:
passwordauthentication no,permitrootlogin no. - Runs on: on the server
- From section: 12. Troubleshooting
sudo sshd -T | grep -Ei 'passwordauth|permitroot'