SSH
SSH (Secure Shell) gives you an encrypted, authenticated connection to a remote machine over an untrusted network, usually on TCP port 22. You use it to get a shell on a server, run a remote command, copy files (scp, sftp, rsync), and tunnel other traffic, such as a PostgreSQL connection to a database that only accepts traffic from inside a private network. OpenSSH is the implementation on almost every Linux and macOS machine, and it ships with Windows.
Logging in with a key
| NO. | STEP | FROM | TO | TYPE | INFO |
|---|---|---|---|---|---|
| 1 | 01 | laptop | server | BANNER | SSH-2.0-OpenSSH_10.0 |
| 2 | 01 | laptop | server | KEX | algorithm negotiation (mlkem768x25519-sha256) · client ephemeral key share |
| 3 | 02 | server | laptop | KEX REPLY | server key share · host key ssh-ed25519 SHA256:3f9a… · signature over exchange hash → both derive session keys (NEWKEYS) |
| 4 | 03 | laptop | server | USERAUTH | publickey, user=deploy, ssh-ed25519 AAAAC3…, signature |
| 5 | 04 | server | laptop | SUCCESS | SSH_MSG_USERAUTH_SUCCESS |
| 6 | 05 | laptop | server | CHANNEL | session → exec "pg_dump analytics" |
- You generated a key pair with
ssh-keygen -t ed25519. The public key sits in the server's~/.ssh/authorized_keys. The private key never leaves the laptop. - After TCP and a version banner, the client starts the key exchange by sending its ephemeral key share.
- The server replies with its key share, its host key and a signature over the exchange, proving its identity first. The client compares the host key with
known_hosts(a mismatch means a possible man-in-the-middle). Both sides derive session keys; from here on, everything is encrypted. - The client proves your identity: it signs the unique session ID with the private key and sends the signature plus the public key.
- The server checks that the public key is authorized and that the signature verifies. No password crossed the network.
- The authenticated connection now carries channels: an interactive shell, a remote command, an scp/sftp transfer or a tunnel.
In the real protocol the client may first ask “would you accept this key?” without a signature, and sign only if the server says yes. The idea is the same: proof of the private key without revealing it.
Keys and files
| File | Where | Holds |
|---|---|---|
~/.ssh/id_ed25519 |
Your laptop | Private key. Protect it with a passphrase. Never copy it to servers. |
~/.ssh/id_ed25519.pub |
Your laptop | Public key: safe to share |
~/.ssh/authorized_keys |
Server, per user | Public keys allowed to log in as that user |
~/.ssh/known_hosts |
Your laptop | Host keys of servers you trust |
~/.ssh/config |
Your laptop | Aliases, users, jump hosts, ports |
Use ed25519 keys. RSA is fine at 3072 bits or more; DSA is gone from OpenSSH. Since OpenSSH 10.0 (2025) the default key exchange is the hybrid post-quantum mlkem768x25519-sha256.
Tunnels: reaching a private database
A database in a private subnet has no public IP. A bastion (jump host) does. SSH can forward a local port through it:
# local 15432 → bastion → db.internal:5432
ssh -N -L 15432:db.internal:5432 [email protected]
psql "host=localhost port=15432 dbname=analytics user=analyst"Or put it in ~/.ssh/config once:
Host bastion
HostName bastion.shop.com
User me
Host db1
HostName 10.0.1.20
User deploy
ProxyJump bastionThen ssh db1 hops through the bastion automatically.
Common mistakes
- Answering “yes” to a changed host key without checking. The warning exists to catch man-in-the-middle attacks. Confirm the fingerprint out of band.
- Sharing one private key across a team. You lose the ability to revoke one person. One key per person, or short-lived SSH certificates signed by a CA.
- Leaving password login on. Set
PasswordAuthentication noandPermitRootLogin noinsshd_config. ForwardAgent yesto untrusted hosts. Root on that host can use your agent to log in elsewhere as you. PreferProxyJump.- Long-lived open port 22 to the internet. Restrict it with a firewall, a VPN, or a cloud session manager (AWS SSM, GCP IAP) instead of a bastion.
Learning Resources
- OpenSSH manual pages:
ssh,ssh_config,sshd_config,ssh-keygen - SSH Mastery, 2nd edition: Michael W. Lucas, practical and current
- SSH, The Secure Shell: The Definitive Guide: deep protocol background
- RFC 4253: SSH Transport Layer Protocol
- SSH Tips and Tricks: Carlos Becker
- SSH Tutorial: Crash Course