SSHD Hardening role
Harden SSH daemon configuration by disabling password authentication and root login, enforcing key-based authentication only.
Usage
Configure the role in your inventory:
1 2 | |
Include the role in your playbook:
1 2 3 | |
The role provides this tag:
- ssh-hardening - Apply SSH security hardening
Docs
Apply SSH hardening to all hosts:
1 | |
Warning: Ensure you have SSH key-based access configured before running this role, as password authentication will be disabled!
What Gets Changed
The role modifies /etc/ssh/sshd_config with these security improvements:
-
Enable Public Key Authentication
1PubkeyAuthentication yes -
Disable Root Login
1PermitRootLogin no -
Disable Password Authentication
1PasswordAuthentication no
Security Benefits
- Key-Based Auth Only: Eliminates brute-force password attacks
- No Root Login: Prevents direct root access, forces use of sudo
- Attack Surface Reduction: Removes password-based authentication vector
Prerequisites
Before running this role, ensure:
-
SSH Keys are Installed: Copy your public key to target hosts
1ssh-copy-id user@host -
Test Key Access: Verify you can login without password
1ssh user@host -
Sudo Access: Ensure your user has sudo privileges for administrative tasks
Post-Deployment
After applying hardening: - Password login attempts will fail - Root cannot login directly (use sudo instead) - Only users with authorized SSH keys can connect
Testing
Test SSH access after hardening:
1 2 3 4 5 6 7 8 | |
Reverting Changes
To re-enable password authentication (not recommended):
1 2 | |
Handler
The role includes a handler that automatically restarts the SSH service when configuration changes are made, ensuring settings take effect immediately.
Best Practices
Additional SSH hardening recommendations (not included in this role): - Use non-standard SSH port - Configure fail2ban for additional protection - Use SSH key passphrases - Limit SSH access by IP (AllowUsers, AllowGroups) - Disable empty passwords - Set ClientAliveInterval and ClientAliveCountMax
