$ ssh [email protected] # RFC 4251–4254

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

SSH public-key login
LAPTOP ~/.ssh/id_ed25519 SERVER sshd, authorized_keys TCP :22 + version exchange KEX init: client key share KEX reply: key share, host key, sig check known_hosts session keys publickey: ed25519 pub + signature pub in authorized_keys? sig valid? auth success channel: shell / exec / port-forward
NO.STEPFROMTOTYPEINFO
101laptopserverBANNERSSH-2.0-OpenSSH_10.0
201laptopserverKEXalgorithm negotiation (mlkem768x25519-sha256) · client ephemeral key share
302serverlaptopKEX REPLYserver key share · host key ssh-ed25519 SHA256:3f9a… · signature over exchange hash → both derive session keys (NEWKEYS)
403laptopserverUSERAUTHpublickey, user=deploy, ssh-ed25519 AAAAC3…, signature
504serverlaptopSUCCESSSSH_MSG_USERAUTH_SUCCESS
605laptopserverCHANNELsession → exec "pg_dump analytics"
  1. 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.
  2. After TCP and a version banner, the client starts the key exchange by sending its ephemeral key share.
  3. 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.
  4. The client proves your identity: it signs the unique session ID with the private key and sends the signature plus the public key.
  5. The server checks that the public key is authorized and that the signature verifies. No password crossed the network.
  6. 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 bastion

Then 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 no and PermitRootLogin no in sshd_config.
  • ForwardAgent yes to untrusted hosts. Root on that host can use your agent to log in elsewhere as you. Prefer ProxyJump.
  • 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

See also: HTTPS, Firewalls, Linux.