SSH Keys: From Zero to Daily Use
Ed25519 SSH keys from zero to daily use: generate, ssh-agent, ssh-copy-id, ~/.ssh/config, and hardening.
What SSH keys actually are
When you log into a server with a password, you prove who you are by knowing a secret the server also knows. SSH keys flip this around: you generate a pair of mathematically linked keys. The private key stays on your computer and never leaves it; the public key goes onto every server you want to access. The server encrypts a challenge with your public key that only your private key can answer. Nothing secret ever travels over the network, and there is no shared password to phish, leak, or brute-force.
This is why keys are the standard for servers, Git hosting (GitHub, GitLab), and cloud VMs. Passwords still work, but keys are both more convenient (no typing) and more secure (immune to brute force).
Step 1 — Generate your first key
The modern recommendation is Ed25519: small, fast, and the default of ssh-keygen since OpenSSH 9.5 (October 2023). On older systems pass -t ed25519 explicitly. Only fall back to RSA (-t rsa -b 4096) for legacy servers that predate Ed25519 support.
ssh-keygen -t ed25519 -C "you@example.com"
Accept the default location (~/.ssh/id_ed25519) and set a passphrase when asked. A passphrase encrypts the private key on disk — if your laptop is stolen, the key alone is useless without it. (Typing it on every connection would be annoying; Step 2 fixes that.)
You now have two files:
~/.ssh/id_ed25519— private key. Never share it, never paste it into a chat or ticket.~/.ssh/id_ed25519.pub— public key. Safe to copy anywhere.
Lock down permissions (SSH refuses to use keys that are readable by others):
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
Step 2 — Use ssh-agent so you type the passphrase once
ssh-agent holds your decrypted key in memory for the session, so you enter the passphrase once and then connect freely.
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
On macOS, store the passphrase in the Keychain so it survives reboots:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
On Windows (PowerShell, with the built-in OpenSSH client), start the agent service once:
# Run PowerShell as Administrator, once:
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
# Then, as your normal user:
ssh-add $env:USERPROFILE\.ssh\id_ed25519
Verify the key is loaded with ssh-add -l — it should list your key's fingerprint.
Step 3 — Install the public key on a server
On Linux/macOS, one command copies your public key into the server's ~/.ssh/authorized_keys:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@your-server
Enter your password one last time. Then test:
ssh user@your-server
If no password prompt appears, key login works. On Windows (no ssh-copy-id), append the key manually:
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh user@your-server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Then make sure the server side has the right permissions (run once on the server): chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys.
Step 4 — Manage many hosts with ~/.ssh/config
Once you have more than a couple of servers, stop typing long commands. ~/.ssh/config gives every host a nickname:
Host vps
HostName 203.0.113.45
User deploy
IdentityFile ~/.ssh/id_ed25519
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
Host office
HostName 10.0.2.15
User admin
Port 2222
IdentityFile ~/.ssh/id_work
Now ssh vps is all you type. Set chmod 600 ~/.ssh/config. Other useful options: ServerAliveInterval 60 (keep idle connections alive), ForwardAgent yes (use with care — only for hosts you trust).
Step 5 — Lock down the server: disable password login
Once key login works, disable password authentication on the server — this single change defeats virtually all SSH brute-force bots. Edit /etc/ssh/sshd_config (as root):
PasswordAuthentication no
PermitRootLogin prohibit-password
PubkeyAuthentication yes
Apply and restart:
sudo sshd -t && sudo systemctl restart sshd
Critical safety rule: keep your current SSH session open while you test a *second* connection in another terminal. If the new connection fails, your old session is still alive to fix the config. Only close it once the new login succeeds.
Platform notes
- Linux: everything above works out of the box; OpenSSH is preinstalled on virtually all distributions.
- macOS: OpenSSH ships with the system; use
--apple-use-keychainwithssh-addfor passphrase persistence. - Windows 10/11: the OpenSSH *client* is built in (Settings → Apps → Optional features → OpenSSH Client; add it if missing). PowerShell understands
ssh,ssh-keygen,ssh-add, andscp. Onlyssh-copy-idis absent — use the manual pipe shown in Step 3.
FAQ
Should I use one key everywhere or a key per server?
One key per *identity* is the practical sweet spot: e.g. one personal key, one work key. A key per server is overkill for most people; a single key for everything means one compromise hits all servers.
I lost my private key / my laptop was stolen. What now?
The private key can't be recovered — that's the point. Remove the corresponding public key from every server's ~/.ssh/authorized_keys (and from GitHub/GitLab settings), generate a fresh pair, and reinstall. This is why the passphrase matters: it buys you time.
"Permission denied (publickey)" — how do I debug?
Run ssh -v user@host and read the output: it shows which keys were offered and why they were rejected. The usual culprits are wrong file permissions (Step 1's chmod), the public key missing from authorized_keys, or sshd_config still requiring something else.
Is it safe to use a key without a passphrase?
Only for automation (CI jobs, backups) where no human can type it — and then restrict what that key can do on the server (e.g. command= or from= prefixes in authorized_keys). For interactive use, always set a passphrase and let ssh-agent handle the typing.