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.
From another machine, try to log in with a password. It should be refused before it even asks.
How it works
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 startReady
ansible all -i inventory.ini -m ping
Proves Ansible can reach every machine before anything changes.ansible-playbook -i inventory.ini playbook.yml --check --diff
Shows each change it would make, line by line. Changes nothing.ansible-playbook -i inventory.ini playbook.yml
Makes the change.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
Syntax passedAnsible lint passed
Last run 2026-10-02.
Open playbook.yml as plain text
StandardReady
ansible all -i inventory.ini -m ping
Proves Ansible can reach every machine before anything changes.ansible-playbook -i inventory.ini site.yml --check --diff
Shows each change it would make, line by line. Changes nothing.ansible-playbook -i inventory.ini site.yml
Makes the change.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
Syntax passedAnsible lint passed
Last run 2026-10-02.
Open main.yml as plain text
Open main.yml as plain text
Open main.yml as plain text
Open main.yml as plain text
Open hardening.conf.j2 as plain text
Open site.yml as plain text
HardenedReady
ansible all -i inventory.ini -m ping
Proves Ansible can reach every machine before anything changes.ansible-playbook -i inventory.ini site.yml --check --diff
Shows each change it would make, line by line. Changes nothing.ansible-playbook -i inventory.ini site.yml
Makes the change.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
Syntax passedAnsible lint passed
Last run 2026-10-02.
Open guard.yml as plain text
Open settings.yml as plain text
Open site.yml as plain text
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.
Ansible. OpenSSH Server's sshd_config, tested before it goes live
What you need first
- Windows Server 2019 or later, reachable by Ansible over WinRM.
- The ansible.windows collection, at the version in requirements.yml.
- Each administrator's public key in C:\ProgramData\ssh\administrators_authorized_keys. If the file is missing or empty, the templates stop before changing anything.
What it creates
- The OpenSSH Server Windows capability, if it is not installed
- C:\ProgramData\ssh\sshd_config, written whole, with password logins off
- Permissions on administrators_authorized_keys limited to Administrators and SYSTEM
- PowerShell as the default shell, and the inbound SSH rule narrowed to your networks (Standard)
The code
Quick startReady
require_admin_key: trueplaybook.yml, line 30
ansible-galaxy collection install ansible.windows:==3.3.0
Installs the Windows modules, at the version the checks ran against.ansible all -i inventory.ini -m ansible.windows.win_ping
Proves Ansible can reach every machine before anything changes.ansible-playbook -i inventory.ini playbook.yml --check --diff
Shows each change it would make, line by line. Changes nothing.ansible-playbook -i inventory.ini playbook.yml
Makes the change.ansible-playbook -i inventory.ini playbook.yml
Run it once more. The recap should end with changed=0.
No inventory yet? An inventory.ini for machines in an Active Directory domain. Kerberos needs pywinrm with its Kerberos extra on the machine running Ansible.
[windows] app-01.corp.example.com [windows:vars] ansible_connection=winrm ansible_winrm_transport=kerberos ansible_port=5985
Syntax failedAnsible lint failed
Last run 2026-10-02.
Open playbook.yml as plain text
StandardReady
ansible-galaxy collection install -r requirements.yml
Installs the Windows modules, at the version the checks ran against.ansible all -i inventory.ini -m ansible.windows.win_ping
Proves Ansible can reach every machine before anything changes.ansible-playbook -i inventory.ini site.yml --check --diff
Shows each change it would make, line by line. Changes nothing.ansible-playbook -i inventory.ini site.yml
Makes the change.ansible-playbook -i inventory.ini site.yml
Run it once more. The recap should end with changed=0.
No inventory yet? An inventory.ini for machines in an Active Directory domain. Kerberos needs pywinrm with its Kerberos extra on the machine running Ansible.
[windows] app-01.corp.example.com [windows:vars] ansible_connection=winrm ansible_winrm_transport=kerberos ansible_port=5985
Syntax failedAnsible lint failed
Last run 2026-10-02.
Open requirements.yml as plain text
Open main.yml as plain text
Open main.yml as plain text
Open main.yml as plain text
Open main.yml as plain text
Open sshd_config.j2 as plain text
Open site.yml as plain text
HardenedReady
ansible-galaxy collection install ansible.windows:==3.3.0
Installs the Windows modules, at the version the checks ran against.ansible all -i inventory.ini -m ansible.windows.win_ping
Proves Ansible can reach every machine before anything changes.ansible-playbook -i inventory.ini site.yml --check --diff
Shows each change it would make, line by line. Changes nothing.ansible-playbook -i inventory.ini site.yml
Makes the change.ansible-playbook -i inventory.ini site.yml
Run it once more. The recap should end with changed=0.
No inventory yet? An inventory.ini for machines in an Active Directory domain. Kerberos needs pywinrm with its Kerberos extra on the machine running Ansible.
[windows] app-01.corp.example.com [windows:vars] ansible_connection=winrm ansible_winrm_transport=kerberos ansible_port=5985
Syntax failedAnsible lint passed
Last run 2026-10-02.
Open guard.yml as plain text
Open settings.yml as plain text
Open site.yml as plain text
How to check it worked
From another machine, ssh -o PubkeyAuthentication=no admin@server is refused without a password prompt, and a login with the administrator's key works.
The stock sshd_config sends every member of Administrators to C:\ProgramData\ssh\administrators_authorized_keys and ignores the key in their own profile. A key placed in the usual per-user file does nothing for an administrator, and with passwords off that is a lockout.
sshd also ignores administrators_authorized_keys unless only Administrators and SYSTEM can reach it, and it says so only in its own log. Both templates set those permissions before turning passwords off.
Ansible reaches Windows over WinRM, so a broken SSH configuration cannot lock Ansible out. It can still lock people out, which is why the templates test the new file with sshd -t before copying it over the live one.
How to undo it
Put password logins back by setting PasswordAuthentication yes in the settings and running again, or edit sshd_config over WinRM or RDP and restart the sshd service. Remove the capability to take SSH off the server entirely.
What it costs
None.