SSH Keys
Passwords over SSH are an invitation. Every public IP gets hammered by bots trying root:123456 around the clock. Check journalctl -u ssh on any box with port 22 open and watch them. Keys make all that noise irrelevant.
How it works, in one paragraph
Section titled “How it works, in one paragraph”You make a key pair. The private key stays on your machine and never leaves. The public key goes on every server you want to log into, in ~/.ssh/authorized_keys. When you connect, the server checks that you hold the private key matching one of the listed public keys. No password crosses the wire.
The private key is the one without .pub. If you ever paste the file without .pub into a chat, a ticket, or a repo, it’s burned. Make a new one.
Generate a key
Section titled “Generate a key”ssh-keygen -t ed25519 -C "alex@laptop"ed25519is what you want. Short, fast, secure. Only use-t rsa -b 4096if something ancient refuses ed25519.-Cis a comment so you can tell keys apart inauthorized_keyslater. Use something likename@machine. Future you will be grateful.- Set a passphrase. If your laptop gets stolen, the passphrase is the only thing between the thief and every server you have. The agent (below) means you only type it once per session.
You get:
~/.ssh/id_ed25519 # private. chmod 600. Guard it.~/.ssh/id_ed25519.pub # public. Hand it out freely.On Windows, the same command works in PowerShell (OpenSSH ships with Windows 10/11). Keys land in C:\Users\you\.ssh\.
Put the public key on the server
Section titled “Put the public key on the server”The easy way:
ssh-copy-id user@serverThe manual way (and the Windows way, since there’s no ssh-copy-id there):
cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"Note >> (append), not > (overwrite). > wipes everyone else’s keys, including possibly the only one that still works.
Test it:
ssh user@serverNo password prompt (maybe a passphrase prompt for your key)? It’s working.
Turn off password login
Section titled “Turn off password login”Only after key login works. In a new session, while the old one stays open.
/etc/ssh/sshd_config.d/10-hardening.conf:
PasswordAuthentication noKbdInteractiveAuthentication noPermitRootLogin nosudo sshd -t # syntax check. Silence = good.sudo systemctl reload ssh # 'sshd' on RHEL-familyNow, from a third terminal, connect again. Still works? Then you can close the old session.
The SSH config file: stop typing so much
Section titled “The SSH config file: stop typing so much”~/.ssh/config:
Host web1 HostName 203.0.113.10 User deploy IdentityFile ~/.ssh/id_ed25519
Host db1 HostName 10.0.0.5 User admin ProxyJump web1 # hop through web1 to reach a private host
Host * ServerAliveInterval 60 # stop idle sessions from dying behind NAT AddKeysToAgent yesNow it’s just ssh web1, scp file web1:/tmp/, ssh db1. Those aliases also work in rsync, git, VS Code Remote, everything.
The agent: type your passphrase once
Section titled “The agent: type your passphrase once”eval "$(ssh-agent -s)"ssh-add ~/.ssh/id_ed25519ssh-add -l # what's loadedOn macOS the Keychain handles this. On Windows, enable the service once (admin PowerShell):
Get-Service ssh-agent | Set-Service -StartupType Automatic -PassThru | Start-Servicessh-add $env:USERPROFILE\.ssh\id_ed25519“Permission denied (publickey)”
Section titled ““Permission denied (publickey)””The classic. Work through it in this order:
- Debug from the client:
ssh -v user@server. Look forOffering public keyand what the server says back. Is it even trying the key you think it’s trying? - Right user? The key is in
deploy’sauthorized_keys, and you’re logging in asubuntu. - Permissions on the server. sshd silently ignores keys if these are too open:
Terminal window chmod 700 ~/.sshchmod 600 ~/.ssh/authorized_keyschmod go-w ~ # home dir must not be group/world writable - Ownership. If root created the file, the user can’t use it:
sudo chown -R deploy:deploy /home/deploy/.ssh - Server logs usually say exactly why:
sudo journalctl -u ssh -n 50(-u sshdon RHEL). Look for “bad ownership or modes.” - Formatting.
authorized_keysneeds one key per line, all on one line. Copy-paste from a web page that wrapped it into three lines? That’s your problem. - SELinux (RHEL-family):
restorecon -Rv ~/.ssh
“WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!”
Section titled ““WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!””Either the server was reinstalled (likely), or someone is intercepting your connection (unlikely, but this message is the one time SSH is actually trying to save you). If you know it was reinstalled:
ssh-keygen -R server-hostname-or-ipDon’t just delete your whole known_hosts. And if you didn’t expect a change, find out why before you type yes.
Housekeeping
Section titled “Housekeeping”- One key per device (laptop, desktop, CI). Lose the laptop, remove one line from
authorized_keys, and everything else keeps working. - Audit
authorized_keysnow and then. You’ll find keys from people who left in 2019. Remove them. - Deploy keys for CI should be separate, restricted keys. Look up
command=andfrom=options inman sshdunder AUTHORIZED_KEYS FILE FORMAT.