使用Ansible Galaxy实现安全加固与自动化部署

通过Ansible Galaxy获得安稳睡眠的安全方案

警告:本文所述技术可能已过时,但仍可作为学习现代工具和技术整合的参考。

作为"蓝队"成员的工作常常充满压力。在强健的Linux基础设施环境中,Ansible能极大提升效率。无论操作系统如何,我们都需要保护系统的远程访问安全。Ansible Galaxy提供了多种角色,可快速、有效地加固Linux服务器,且易于复现。

安全加固方案

保护远程访问时,基于密钥的SSH和双因素认证(2FA)能提供额外安全层。此外,防火墙是基本要求,配合fail2ban可防御通过iptables对开放服务的暴力破解。最后,将所有日志发送到Logstash,在Kibana中进行Geo-IP映射可视化,然后就能安心休息了。

实施步骤

  1. 检查Ansible版本
ansible@tw17ch01:/etc/ansible/roles/sshd$ dpkg -l |grep ansible
ii  ansible   2.1.0.0-1ppa~trusty        all        
  1. 安装配置SSH角色
ansible@tw17ch01:/etc/ansible/playbooks$ sudo ansible-galaxy install mattwillsher.sshd
  1. 修改SSH默认配置
    强烈建议修改默认端口、禁用root登录并设置PasswordAuthentication no

  2. 部署防火墙角色

ansible@tw17ch01:/etc/ansible/roles$ sudo ansible-galaxy install geerlingguy.firewall
  1. 配置fail2ban防护
ansible@tw17ch01:/etc/ansible/roles$ sudo ansible-galaxy install tersmitten.fail2ban
  1. 设置日志收集系统
ansible@tw17ch01:/etc/ansible/roles$ sudo ansible-galaxy install jpnewman.elk-filebeat

完整Playbook示例

- hosts: all
  become: yes
  roles:
    # 部署标准SSH配置
    - { role: sshd }
    
    # 安装iptables
    - { role: chains }
    
    # 添加filebeat
    - { role: logger }
    
    # 部署配置fail2ban
    - { role: banner }

验证配置

  1. SSH配置验证
ansible@tw17ch03:~$ cat /etc/ssh/sshd_config
Port 22444
PasswordAuthentication no
  1. 防火墙规则验证
ansible@tw17ch03:~$ sudo iptables -L
Chain INPUT (policy ACCEPT)
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:22444
  1. fail2ban配置验证
tendans@tw17ch03:~$ cat /etc/fail2ban/jail.local
[ssh]
enabled = true
port = 22444

通过这套方案,我们实现了服务器核心安全组件的自动化部署,包括SSH加固、防火墙配置、入侵防护和集中日志管理,大大提升了系统安全性和管理效率。
更多精彩内容 请关注我的个人公众号 公众号(办公AI智能小助手)
公众号二维码

posted @ 2025-08-06 18:01  qife  阅读(3)  评论(0)    收藏  举报