Lesson 05 of 16 · SSH
SSH keys and client configuration
Create an SSH key the right way (Ed25519 with a passphrase), load it into the agent so you type the passphrase once, install it on a server, and fix the permission problems that make key login silently fall back to passwords. Then a tidy ~/.ssh/config with per-host settings and jump hosts, why agent forwarding is risky, and hardware-backed keys.
Why keys beat passwords
| Password | SSH key |
|---|---|
| Can be guessed by bots | Cannot be guessed (a 256-bit key) |
| Often reused across sites | Unique to you |
| Sent to the server (encrypted) | Never sent; only a signature is |
| Typed every time | Typed once per session with an agent |
Create a key
$ ssh-keygen -t ed25519 -C "asha@laptop"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/asha/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/asha/.ssh/id_ed25519
Your public key has been saved in /home/asha/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:uZ8OQVUxzgKrN+HnW3hYOPqq9EKYNSkcvJ0icdQMnqo asha@laptop
- Ed25519: modern, small, fast; the right default. Use RSA (3072 bits or more) only for very old systems.
- Passphrase: encrypts the private key file on your disk. If your laptop or backup is stolen, the key is useless without it.
id_ed25519is the private key (never share).id_ed25519.pubis the public key (one line, safe to share).
The private key is your house key; the public key is a description of the lock that fits it. You give the description to every house you're allowed into (servers), and you keep the key on you. The passphrase is a little safe the key lives in, so a pickpocket can't use it.
Type the passphrase once: the agent
ssh-agent holds decrypted keys in memory for your session:
$ ssh-add ~/.ssh/id_ed25519 # passphrase asked once
$ ssh-add -l # list loaded keys
256 SHA256:uZ8O…Mnqo asha@laptop (ED25519)
On macOS add --apple-use-keychain to store the passphrase in the Keychain. Use ssh-add -t 8h to unload keys automatically after a working day.
Install the public key on a server
$ ssh-copy-id -i ~/.ssh/id_ed25519.pub asha@web1.example.com
This appends your public key to ~/.ssh/authorized_keys on the server. Without ssh-copy-id, paste the one line of id_ed25519.pub into that file yourself.
The permission trap. sshd ignores keys if other users could tamper with them, and silently falls back to password login:
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/authorized_keys
$ chmod go-w ~ # home directory not writable by others
$ ls -ld ~ ~/.ssh ~/.ssh/authorized_keys # owned by the user, not root
The server log says why: sudo journalctl -u ssh -n 50 shows lines such as Authentication refused: bad ownership or modes for directory /home/asha/.ssh.
Restrict a key in authorized_keys
Each line can carry options in front of the key. Useful for automation keys:
from="10.0.5.0/24",no-port-forwarding,no-agent-forwarding,command="/usr/local/bin/backup-only" ssh-ed25519 AAAAC3Nz… backup@ci
This key only works from that network and can only run the backup script.
A tidy ~/.ssh/config
Host bastion
HostName bastion.example.com
User asha
IdentityFile ~/.ssh/id_ed25519
Host web-*
User deploy
ProxyJump bastion
IdentityFile ~/.ssh/id_ed25519
Host web-1
HostName 10.0.1.11
Host *
IdentitiesOnly yes
ServerAliveInterval 30
Now ssh web-1 connects through the bastion automatically. ProxyJump (ssh -J bastion web-1) passes the connection through the jump host while your keys stay on your laptop. IdentitiesOnly yes stops the client offering every key in your agent to every server.
Avoid agent forwarding
ForwardAgent yes (or ssh -A) lets the remote server use your local agent. Anyone with root on that server can then use your keys to log in elsewhere as you while you are connected. Use ProxyJump instead. If you must forward, do it only to servers you fully trust.
Hardware-backed keys
With a FIDO2 security key (YubiKey and similar), ssh-keygen -t ed25519-sk creates a key whose private part stays on the token. Logging in needs the token plugged in and touched, so a stolen laptop is not enough.
Recap
- Use Ed25519 keys with a passphrase, and an agent so you type it once.
- The public key goes into the server's
authorized_keys; permissions must be strict or sshd ignores it. - Restrict automation keys with
from=andcommand=. - Use
~/.ssh/configand ProxyJump; avoid agent forwarding; consider hardware keys.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.