🔧 ??一、安裝 Fail2ban??
- ??啟用 EPEL 倉庫??
yum install epel-release -y
- ??安裝 Fail2ban??
yum install fail2ban -y
- ??啟動并設置開機自啟??
systemctl start fail2ban systemctl enable fail2ban
?? 注意:CentOS 7.9 默認 Python 版本為 2.7,Fail2ban 0.11+ 兼容此版本
5
。
?? ??二、配置核心防護規則??
??1. 創建自定義配置文件??
避免覆蓋默認配置,創建?jail.local
:
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local vi /etc/fail2ban/jail.local
??2. 配置 SSH 防護(重點)??
在?jail.local
?中添加以下內容
[sshd] enabled = true
port = ssh
filter = sshd
logpath = /var/log/secure # CentOS 認證日志路徑
maxretry = 5 # 5 次失敗嘗試后封禁
findtime = 600 # 10 分鐘內觸發規則
bantime = 86400 # 封禁 24 小時(單位:秒)
ignoreip = 127.0.0.1/8 192.168.1.0/24 # 信任 IP 白名單
??3. 可選:Nginx 防護??
針對 Web 暴力破解或掃描
[nginx-http-auth]
enabled = true
filter = nginx-http-auth
logpath = /var/log/nginx/error.log
maxretry = 3
🔒 ??三、服務管理與監控??
??命令?? | ??作用?? |
---|---|
systemctl restart fail2ban | 重啟服務使配置生效 |
fail2ban-client status sshd | 查看 SSH 防護狀態及被封禁 IP |
fail2ban-client set sshd unbanip <IP> | 手動解封指定 IP 1 3 |
tail -f /var/log/fail2ban.log | 實時監控封禁日志 9 |