A Practical Guide to configure common hardening settings on Linux servers using Ansible
8 min read
Server security is paramount in today’s interconnected world. A single vulnerability can expose your entire infrastructure to malicious actors. Manual hardening across multiple servers is time-consuming, error-prone, and inconsistent. This is where Ansible shines. Ansible’s automation capabilities allow us to implement robust security measures efficiently and reliably, ensuring consistent security posture across our entire fleet.
This blog post demonstrates how to leverage Ansible to harden Linux servers, focusing on key security best practices. We’ll build upon a previous example, enhancing it with crucial security features. The example includes hardening Apache web servers, but the principles apply broadly to other server types.
The Ansible Playbook:
This Ansible playbook will implement the following security measures:
Disable Password Authentication (SSH): Eliminates the risk of brute-force attacks targeting password-based SSH logins.
Create and Secure an Admin User: Establishes a dedicated administrative account with strong authentication using SSH keys.
Configure sudo Permissions: Grants the admin user appropriate sudo privileges.
Install and Configure Fail2ban: Implements a security tool to detect and ban malicious IP addresses attempting unauthorized logins.
Limit SSH Authentication Attempts: Reduces the window of opportunity for brute-force attacks by setting a maximum number of authentication retries.
Prepare authorized_keys: Create a file named authorized_keys containing the public key for the adminuser account.
Inventory File: Configure your Ansible inventory file to point to your target servers.
Caution: The sudoers file is critical. Errors can lock you out. Review this section carefully.
Running the Playbook:
Execute the playbook using: ansible-playbook hardening.yml
Beyond the Basics:
This playbook provides a solid foundation. Further enhancements could include:
Regular Security Updates: Integrate Ansible with your update management system.
Security Auditing: Implement regular security audits using Ansible to monitor system configurations.
Vulnerability Scanning: Integrate Ansible with vulnerability scanning tools.
Intrusion Detection Systems (IDS): Configure and manage IDS using Ansible.
While not necessarily considered as hardening settings, it is common and desirable to use Ansible to configure system updates, logrotate, log shipping, time synchronization and basic system tuning. The playbook below performs those tasks:
By automating server hardening with Ansible, you gain significant advantages in efficiency, consistency, and overall security posture. This approach allows you to manage security effectively, even across large server deployments. Remember to always test your playbooks thoroughly in a non-production environment before deploying them to production servers.
Comments are disabled until you accept functional cookies.
Comments are disabled until you accept functional cookies.