iac.htora.dev · security templates

Home/Templates/Lock SSH down to keys only

Lock SSH down to keys only

Turn off password logins and root logins, and check the config before restarting so you cannot lock yourself out.

Ansible

Why bother

Anything with SSH open to the internet gets password guesses within minutes of coming online, continuously, forever. Keys remove the whole category. The risk is in the change itself: a typo in the config plus a restart leaves you locked out of a machine you now cannot reach. These templates check the file is valid and check you have a working key before they touch anything.

How you know it worked

From another machine, try to log in with a password. It should be refused before it even asks.

How it works

1Ansible connectsover SSH to Linux, over WinRM to Windows2Safety checksa key exists and the new config is valid3Settings writtena drop-in on Linux, the whole file on Windows4sshd reloaded or restartedthen read back to prove passwords are off
Step by step, from the thing that happens to the result you want.

Set it up

Pick your platform, then open the level you want. Each level is complete on its own. Read the comments in the files as you go. Anything with a real consequence is explained on the line where it happens.

Ansible. A drop-in sshd config, validated before reload

What you need first

  • An inventory, and an account you can become root with.
  • A working SSH key for that account on every host. If one is missing, the templates stop before changing anything.
  • A way back in that is not SSH, the first time you run this on a host you cannot walk up to.

What it creates

  • /etc/ssh/sshd_config.d/00-hardening.conf
  • An Include line in the main config, if the image does not already have one

The code

Quick startReadyone file, 107 lines
One playbook. Key check, drop-in file, safe reload.
Run it
  1. ansible all -i inventory.ini -m ping
    Proves Ansible can reach every machine before anything changes.
  2. ansible-playbook -i inventory.ini playbook.yml --check --diff
    Shows each change it would make, line by line. Changes nothing.
  3. ansible-playbook -i inventory.ini playbook.yml
    Makes the change.
  4. ansible-playbook -i inventory.ini playbook.yml
    Run it once more. The recap should end with changed=0.

No inventory yet? A one-machine inventory.ini:

[all]
web-01 ansible_host=10.0.0.11 ansible_user=ubuntu
Download playbook.yml
Checks

Syntax passedAnsible lint passed

Last run 2026-10-02.

outcomes/ssh-hardening/linux/t0
playbook.yml
Open playbook.yml as plain text
StandardReadysix files, 195 lines
A role. Adds cipher and key exchange lists, AllowGroups, forwarding switches, and a read-back of sshd -T that fails if passwords are still accepted.
Run it
  1. ansible all -i inventory.ini -m ping
    Proves Ansible can reach every machine before anything changes.
  2. ansible-playbook -i inventory.ini site.yml --check --diff
    Shows each change it would make, line by line. Changes nothing.
  3. ansible-playbook -i inventory.ini site.yml
    Makes the change.
  4. ansible-playbook -i inventory.ini site.yml
    Run it once more. The recap should end with changed=0.

No inventory yet? A one-machine inventory.ini:

[all]
web-01 ansible_host=10.0.0.11 ansible_user=ubuntu
Download all six files as a zip
Checks

Syntax passedAnsible lint passed

Last run 2026-10-02.

outcomes/ssh-hardening/linux/t1
roles/ssh_hardening/defaults/main.yml
Open main.yml as plain text
roles/ssh_hardening/handlers/main.yml
Open main.yml as plain text
roles/ssh_hardening/meta/main.yml
Open main.yml as plain text
roles/ssh_hardening/tasks/main.yml
Open main.yml as plain text
roles/ssh_hardening/templates/hardening.conf.j2
Open hardening.conf.j2 as plain text
site.yml
Open site.yml as plain text
HardenedReadythree files, 165 lines
Standard's roles, unchanged, with your settings in settings.yml. Rules refuse settings that allow passwords or root login, skip the key check, allow agent forwarding, add weak ciphers, MACs, or key exchange, or let another file override the settings. A daily check-mode run reports drift, and ansible-lint runs on the production profile.
Run it
  1. ansible all -i inventory.ini -m ping
    Proves Ansible can reach every machine before anything changes.
  2. ansible-playbook -i inventory.ini site.yml --check --diff
    Shows each change it would make, line by line. Changes nothing.
  3. ansible-playbook -i inventory.ini site.yml
    Makes the change.
  4. ansible-playbook -i inventory.ini site.yml
    Run it once more. The recap should end with changed=0.

No inventory yet? A one-machine inventory.ini:

[all]
web-01 ansible_host=10.0.0.11 ansible_user=ubuntu
Download all three files as a zip
Checks

Syntax passedAnsible lint passed

Last run 2026-10-02.

outcomes/ssh-hardening/linux/t2
guard.yml
Open guard.yml as plain text
settings.yml
Open settings.yml as plain text
site.yml
Open site.yml as plain text
What catches people

sshd keeps the first value it reads for each setting, and it reads drop-in files in name order. Many cloud images ship 50-cloud-init.conf with passwords switched on, so a file named 99-hardening.conf loses to it and the run still reports success. These templates use 00- so they are read first.

Older images have no Include line in sshd_config, so a drop-in file is read by nobody at all. Both templates add the line, and the Standard role reads back sshd -T afterwards and fails if passwords still work.

How to undo it

Delete the drop-in file and reload sshd. The templates reload the service, so an existing session stays open even when the new config is wrong. That open session is usually the difference between a quick fix and a trip to the data centre.

What it costs

None.

Registry 0.23.0. Built 2026-11-17.

Made by Habibullah Tora. Source on GitHub. Code under the MIT licence, writing under CC BY 4.0.