SSH · wrap-up
Cheat sheet & self-check
Every command from this section on one page.
Host keys
ls /etc/ssh/ssh_host_*_key.pub | The server's public host keys |
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub | Fingerprint of the server's host key (run on the server) |
ssh-keygen -F server.example.com | Is this host already in my known_hosts? |
ssh-keygen -R server.example.com | Remove a host's old key from known_hosts (after verifying the change) |
ssh-keyscan -t ed25519 server.example.com | Fetch a host key (verify its fingerprint separately) |
Seeing what happens
ssh -v user@server | Verbose: key exchange, host key, which auth methods are tried |
ssh -o PreferredAuthentications=publickey user@server | Try key authentication only |
ssh -Q kex | Key exchange algorithms your client supports |
Keys
ssh-keygen -t ed25519 -C 'asha@laptop' | New Ed25519 key pair with a comment; set a passphrase when asked |
ssh-keygen -p -f ~/.ssh/id_ed25519 | Add or change a key's passphrase |
ssh-keygen -lf ~/.ssh/id_ed25519.pub | Show a key's fingerprint |
ssh-copy-id -i ~/.ssh/id_ed25519.pub asha@server | Append your public key to the server's authorized_keys |
ssh-keygen -t ed25519-sk | Key backed by a FIDO2 hardware token (needs the token to log in) |
Agent
eval "$(ssh-agent -s)" | Start an agent in this shell (desktops usually start one for you) |
ssh-add ~/.ssh/id_ed25519 | Load a key; enter the passphrase once |
ssh-add -t 8h ~/.ssh/id_ed25519 | Load it for 8 hours only |
ssh-add -l | List loaded keys |
Permissions (server side)
chmod 700 ~/.ssh | Directory: owner only |
chmod 600 ~/.ssh/authorized_keys | File: owner read/write only |
sudo journalctl -u ssh -n 50 | See why sshd refused a key (e.g. bad ownership or modes) |
Test and apply safely
sudo sshd -t | Check the configuration for errors; prints nothing if OK |
sudo sshd -T | grep -Ei 'passwordauth|permitroot|kbdinteractive' | Show the settings sshd will actually use |
sudo systemctl reload ssh | Apply; existing sessions stay connected |
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no user@host | Confirm password login is refused |
fail2ban and firewall
sudo apt install fail2ban | Ban IPs that keep failing to log in |
sudo fail2ban-client status sshd | Currently banned IPs |
sudo fail2ban-client set sshd unbanip 198.51.100.7 | Unban an address |
sudo ufw allow from 203.0.113.0/24 to any port 22 proto tcp | Allow SSH only from your network |
sudo ufw limit 22/tcp | Rate-limit new SSH connections |
SSH certificates
ssh-keygen -t ed25519 -f user_ca | Create a user certificate authority key |
ssh-keygen -s user_ca -I asha -n asha -V +8h id_ed25519.pub | Sign a user key: valid for user asha, 8 hours |
TrustedUserCAKeys /etc/ssh/user_ca.pub | sshd_config: trust keys signed by this CA |