Agent Wranglers

INF-48Required

Allow SSH with keys only

Turn off password logins and direct root logins over SSH. Give each person their own key, and remove it the day they leave.

How to do it

Put the settings in a file under /etc/ssh/sshd_config.d/ whose name sorts first, such as 10-hardening.conf. For most keywords the first value sshd reads wins. Cloud images often ship a later file, such as 50-cloud-init.conf, that turns passwords back on. Check that the main file's Include line comes before its own settings. Prefer short-lived SSH certificates from your identity provider over keys that never expire. Better still, use a tunnel that needs single sign-on, as in close admin ports. Remove keys on the deadline in Remove access within a day of leaving.

Examples

Plain text example

# /etc/ssh/sshd_config.d/10-hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
AuthenticationMethods publickey

Shell example

sshd -T | grep -E '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin) '
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no admin@shop.example.com   # Permission denied

How to check

sshd -T prints passwordauthentication no, kbdinteractiveauthentication no and permitrootlogin no. A login that offers only a password is refused. Run sshd -t before every reload, so a broken file never locks you out.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/infrastructure/servers/ssh-keys-only">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.