Security Basics — SSH, TLS and Certificates›SSH · Cheat sheet & self-check

SSH · wrap-up

Cheat sheet & self-check

Every command from this section on one page.

04 · How SSH works

Host keys

ls /etc/ssh/ssh_host_*_key.pubThe server's public host keys
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pubFingerprint of the server's host key (run on the server)
ssh-keygen -F server.example.comIs this host already in my known_hosts?
ssh-keygen -R server.example.comRemove a host's old key from known_hosts (after verifying the change)
ssh-keyscan -t ed25519 server.example.comFetch a host key (verify its fingerprint separately)

Seeing what happens

ssh -v user@serverVerbose: key exchange, host key, which auth methods are tried
ssh -o PreferredAuthentications=publickey user@serverTry key authentication only
ssh -Q kexKey exchange algorithms your client supports

05 · SSH keys and client configuration

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_ed25519Add or change a key's passphrase
ssh-keygen -lf ~/.ssh/id_ed25519.pubShow a key's fingerprint
ssh-copy-id -i ~/.ssh/id_ed25519.pub asha@serverAppend your public key to the server's authorized_keys
ssh-keygen -t ed25519-skKey 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_ed25519Load a key; enter the passphrase once
ssh-add -t 8h ~/.ssh/id_ed25519Load it for 8 hours only
ssh-add -lList loaded keys

Permissions (server side)

chmod 700 ~/.sshDirectory: owner only
chmod 600 ~/.ssh/authorized_keysFile: owner read/write only
sudo journalctl -u ssh -n 50See why sshd refused a key (e.g. bad ownership or modes)

06 · Hardening an SSH server

Test and apply safely

sudo sshd -tCheck 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 sshApply; existing sessions stay connected
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no user@hostConfirm password login is refused

fail2ban and firewall

sudo apt install fail2banBan IPs that keep failing to log in
sudo fail2ban-client status sshdCurrently banned IPs
sudo fail2ban-client set sshd unbanip 198.51.100.7Unban an address
sudo ufw allow from 203.0.113.0/24 to any port 22 proto tcpAllow SSH only from your network
sudo ufw limit 22/tcpRate-limit new SSH connections

SSH certificates

ssh-keygen -t ed25519 -f user_caCreate a user certificate authority key
ssh-keygen -s user_ca -I asha -n asha -V +8h id_ed25519.pubSign a user key: valid for user asha, 8 hours
TrustedUserCAKeys /etc/ssh/user_ca.pubsshd_config: trust keys signed by this CA