--- # ssh-hardening / linux / t0 "Quick start" # # Turns off password logins and root logins on every host in your inventory. # # Run: # ansible-playbook -i inventory.ini playbook.yml --check # see what would change # ansible-playbook -i inventory.ini playbook.yml # do it # # Verify: # ssh -o PubkeyAuthentication=no you@host # Expect: Permission denied (publickey). It should refuse before asking for a password. # # Two safety steps are built in, because the usual way this goes wrong is being # locked out of a machine you can no longer reach: # 1. It refuses to run if the account you are connecting as has no SSH key. # 2. It checks the config file is valid before reloading the service. # # If your keys come from somewhere other than authorized_keys, such as SSH # certificates or an AuthorizedKeysCommand, skip the first check: # ansible-playbook -i inventory.ini playbook.yml -e skip_key_check=true - name: Lock SSH down to keys only hosts: all become: true gather_facts: true vars: # The only thing you might want to change. Raise it if you use a bastion # that retries, lower it to be stricter. max_auth_tries: 4 pre_tasks: # become: false and a ~ path, so this looks in the home directory of the # account Ansible logs in as. Facts are gathered as root, so a path built # from them would check root's keys instead. - name: Look for an authorized key on the account we connected as ansible.builtin.stat: path: "~/.ssh/authorized_keys" become: false register: authorized_keys when: not (skip_key_check | default(false) | bool) - name: Stop if there is no key, because turning off passwords would lock us out ansible.builtin.assert: that: - authorized_keys.stat.exists - authorized_keys.stat.size > 0 fail_msg: >- {{ inventory_hostname }} has no authorized_keys for {{ ansible_user | default('the account Ansible connects as') }}. Install a key first. Turning off password logins now would lock you out. success_msg: "Key found, safe to continue." when: not (skip_key_check | default(false) | bool) tasks: - name: Make sure the drop-in directory exists ansible.builtin.file: path: /etc/ssh/sshd_config.d state: directory owner: root group: root mode: "0755" # Some older images have no Include line, so a drop-in file would be read by # nobody and the playbook would report success while changing nothing. - name: Make sure the main config reads the drop-in directory ansible.builtin.lineinfile: path: /etc/ssh/sshd_config line: "Include /etc/ssh/sshd_config.d/*.conf" insertbefore: BOF state: present validate: /usr/sbin/sshd -t -f %s notify: Reload sshd # sshd keeps the FIRST value it reads for each setting, and reads drop-ins # in name order. A 00- prefix makes these win over files the distribution # ships, such as 50-cloud-init.conf, which often turns passwords back on. - name: Write the hardening settings ansible.builtin.copy: dest: /etc/ssh/sshd_config.d/00-hardening.conf owner: root group: root mode: "0600" validate: /usr/sbin/sshd -t -f %s content: | # Managed by Ansible. Local edits are overwritten on the next run. PasswordAuthentication no KbdInteractiveAuthentication no PermitEmptyPasswords no PermitRootLogin no PubkeyAuthentication yes MaxAuthTries {{ max_auth_tries }} LoginGraceTime 30 X11Forwarding no AllowAgentForwarding no ClientAliveInterval 300 ClientAliveCountMax 2 notify: Reload sshd handlers: # A reload keeps existing sessions alive, where a restart ends them. So if # the new config is wrong you are still connected and can put it back. - name: Reload sshd ansible.builtin.service: name: "{{ 'sshd' if ansible_facts['os_family'] == 'RedHat' else 'ssh' }}" state: reloaded