Skip to content

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.

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.

Terminal window
ssh-keygen -t ed25519 -C "alex@laptop"
  • ed25519 is what you want. Short, fast, secure. Only use -t rsa -b 4096 if something ancient refuses ed25519.
  • -C is a comment so you can tell keys apart in authorized_keys later. Use something like name@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\.

The easy way:

Terminal window
ssh-copy-id user@server

The manual way (and the Windows way, since there’s no ssh-copy-id there):

Terminal window
cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
Terminal window
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:

Terminal window
ssh user@server

No password prompt (maybe a passphrase prompt for your key)? It’s working.

Only after key login works. In a new session, while the old one stays open.

/etc/ssh/sshd_config.d/10-hardening.conf:

PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
Terminal window
sudo sshd -t # syntax check. Silence = good.
sudo systemctl reload ssh # 'sshd' on RHEL-family

Now, from a third terminal, connect again. Still works? Then you can close the old session.

~/.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 yes

Now it’s just ssh web1, scp file web1:/tmp/, ssh db1. Those aliases also work in rsync, git, VS Code Remote, everything.

Terminal window
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ssh-add -l # what's loaded

On macOS the Keychain handles this. On Windows, enable the service once (admin PowerShell):

Terminal window
Get-Service ssh-agent | Set-Service -StartupType Automatic -PassThru | Start-Service
Terminal window
ssh-add $env:USERPROFILE\.ssh\id_ed25519

The classic. Work through it in this order:

  1. Debug from the client: ssh -v user@server. Look for Offering public key and what the server says back. Is it even trying the key you think it’s trying?
  2. Right user? The key is in deploy’s authorized_keys, and you’re logging in as ubuntu.
  3. Permissions on the server. sshd silently ignores keys if these are too open:
    Terminal window
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
    chmod go-w ~ # home dir must not be group/world writable
  4. Ownership. If root created the file, the user can’t use it: sudo chown -R deploy:deploy /home/deploy/.ssh
  5. Server logs usually say exactly why: sudo journalctl -u ssh -n 50 (-u sshd on RHEL). Look for “bad ownership or modes.”
  6. Formatting. authorized_keys needs one key per line, all on one line. Copy-paste from a web page that wrapped it into three lines? That’s your problem.
  7. 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:

Terminal window
ssh-keygen -R server-hostname-or-ip

Don’t just delete your whole known_hosts. And if you didn’t expect a change, find out why before you type yes.

  • One key per device (laptop, desktop, CI). Lose the laptop, remove one line from authorized_keys, and everything else keeps working.
  • Audit authorized_keys now 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= and from= options in man sshd under AUTHORIZED_KEYS FILE FORMAT.