Prerequisites
- Working SSH access
- A second session kept open during changes, so you don’t lock yourself out
01Switch to keys
Generate a key on your workstation, protected by a passphrase, then copy its public part to the server. Check that key login works before disabling passwords.
ssh-keygen -t ed25519
ssh-copy-id dorian@server02The settings
Rather than editing sshd_config, a file in sshd_config.d (included at the top of the main file on Debian and Ubuntu) keeps your changes separate.
By default, PasswordAuthentication is yes, PermitRootLogin is prohibit-password and MaxAuthTries is 6.
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
AllowUsers dorian
MaxAuthTries 303The trap: the first value wins
sshd keeps the first value it reads for each option, not the last. Files in sshd_config.d are read in alphabetical order, and cloud images often drop a 50-cloud-init.conf containing PasswordAuthentication yes.
A file named hardening.conf would be read after it (digits sort before letters) and its setting ignored. Hence the 00- prefix: it comes first. sshd -T shows the configuration actually applied.
sshd -T | grep -Ei 'passwordauthentication|permitrootlogin|maxauthtries'04Test, then reload
sshd -t validates the syntax without applying anything. Then reload the service (ssh on Debian and Ubuntu, sshd elsewhere) and test from a new session, keeping the old one open.
sshd -t && systemctl reload sshKey takeaways
- An ed25519 key, tested before turning passwords off.
- No root, a list of allowed users.
- First value read = value applied: prefix the file with 00-.
- sshd -T to check, sshd -t before every reload.