Linux 系统安全加固与漏洞管理实战
一、安全加固基础原则
- 专用服务用户(如 nginx、mysql、redis)
系统更新与补丁
# Debian
sudo apt update && sudo apt upgrade -y
sudo unattended-upgrades
# Red Hat
sudo dnf update --security
禁用不必要服务
systemctl disable --now <service>
ss -tuln | grep LISTEN
- 强密码 + PAM 配置(/etc/pam.d/)
二、SSH 安全强化
/etc/ssh/sshd_config:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
PermitEmptyPasswords no
MaxAuthTries 3
LoginGraceTime 30
AllowUsers ansible@trusted-ip
重启:systemctl restart sshd
Fail2Ban 防暴力破解:
sudo apt install fail2ban -y
# 配置 jail.local
三、防火墙与网络安全
UFW(Ubuntu):
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow from trusted-ip to any port 22
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
firewalld(Red Hat):
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
高级:iptables 自定义链 + ipset 黑名单。
四、SELinux / AppArmor
SELinux(Red Hat 系):
sestatus
setenforce 1
# 永久:/etc/selinux/config → SELINUX=enforcing
audit2allow 修复问题
AppArmor(Debian 系):
sudo aa-status
sudo aa-enforce /etc/apparmor.d/usr.sbin.nginx
五、文件系统与权限
重要目录:
chown -R root:root /etc
chmod 600 /etc/ssh/sshd_config
六、漏洞扫描与管理
工具:
Lynis:系统审计
sudo lynis audit system
OSSEC / Wazuh:HIDS(主机入侵检测)
定期流程:
七、证书与加密
Let's Encrypt(certbot):
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx
自动续期:cron + systemd timer
TLS 配置:Mozilla 推荐配置(Nginx/Apache)
八、入侵检测与响应
- OSSEC / Wazuh:日志分析 + 文件监控 + 主动响应
响应流程(IR):
九、容器与 K8s 安全
- Pod Security:restricted 策略
- RBAC + PodSecurityAdmission
- Runtime:gVisor / Kata Containers
- Secret 管理:外部 Vault / Sealed Secrets
十、企业级安全运维实践
- 自动化:Ansible 统一加固 Playbook
- 监控:SIEM(Elasticsearch + Kibana / Loki)
案例:生产环境安全加固项目
通过 Ansible 批量部署 Fail2Ban + Lynis + Wazuh + 严格防火墙 + SELinux,结合定期 Trivy 扫描,漏洞数量下降 90%,成功抵御多次扫描攻击。