SSH key-based authentication
ssh-keygen, ssh-copy-id, and why key auth is both more secure and more convenient than passwords. · 10 min
SSH key authentication uses a key pair: a private key that never leaves your machine and must be protected like a password, and a public key that's safe to share and gets placed on servers you want to access. When you connect, the server challenges your client to prove it holds the private key matching a public key it already trusts — this happens through public-key cryptography, not by ever transmitting the private key itself, which is exactly why it's more secure than password authentication: there's no password to intercept, phish, or brute-force over the network.
`ssh-keygen` generates a new key pair, by default `~/.ssh/id_ed25519` (private) and `~/.ssh/id_ed25519.pub` (public) — ed25519 is the modern, recommended algorithm; older RSA keys still work but need a larger bit size (4096) to be considered comparably strong today. Setting a passphrase on the private key during generation adds a second layer: even if the private key file is stolen, it's useless without that passphrase — skipping the passphrase for convenience is a real, common tradeoff worth thinking about rather than a purely technical decision.
`ssh-copy-id user@host` is the standard way to install your public key onto a server's `~/.ssh/authorized_keys` file for that user, after which key-based login works automatically. Once key-based login is confirmed working, disabling password authentication entirely in `sshd_config` (covered in the next lesson) removes password brute-forcing as an attack vector against SSH completely, not just partially.
| Command | Purpose | Example |
|---|---|---|
| ssh-keygen -t ed25519 | Generate a new SSH key pair using the modern ed25519 algorithm | — |
| ssh-copy-id user@host | Copy your public key to a server's authorized_keys for passwordless login | — |
| ssh user@host | Connect to a remote host | — |
| scp file user@host:/path | Copy a file to/from a remote host over SSH | — |
| sftp user@host | Open an interactive file-transfer session over SSH | — |
Full key setup, start to finish
# On your local machine
$ ssh-keygen -t ed25519 -C "your-email@example.com"
# Accept the default path, set a passphrase when prompted
$ ssh-copy-id deploy@203.0.113.10
# Enter the password ONE more time — for this copy operation only
$ ssh deploy@203.0.113.10
# Logs in with no password prompt — key auth confirmed workingCommon Mistakes
- ⚠ Copying the private key (id_ed25519, no .pub extension) to a server instead of the public key — the private key must never leave your own machine
- ⚠ Setting overly permissive permissions on ~/.ssh or the private key file — SSH will refuse to use a private key that's readable by anyone but its owner
- ⚠ Deleting or losing a private key with no backup and no other access method configured — plan key rotation and recovery before you need it, not after
Takeaway: The private key never leaves your machine and proves your identity cryptographically — that's what makes key auth immune to password interception, phishing, and brute-forcing in a way password auth fundamentally isn't.