Debian 12 部署Fail2ban保護SSH

Environment

  • OS Version: Debian GNU/Linux 12 (bookworm)

  • Fail2ban-Server: 1.0.2

  • Python: 3.11.2

Install Fail2ban

apt install fail2ban

Configure Rule

nano /etc/fail2ban/jail.d/defaults-debian.conf

[DEFAULT]
# 用於指定忽略的ip地址, fail2ban 防御,以空格間隔。
ignoreip = 127.0.0.1/8
# ssh客戶端被禁止的時長(默認單位爲:秒)
bantime  = 86400
# 過濾的時長(秒)
findtime  = 600
# 匹配到的閥值(允許失敗的次數)
maxretry = 3
 
 
[ssh-iptables]
# 是否開啓
enabled  = true
# 過濾規則
port = 22
filter = sshd
# debian日志路徑
logpath = /var/log/auth.log
# centos7日志文件路徑
#logpath  = /var/log/secure
# 匹配到的閥值(允許失敗的次數)
maxretry = 4

Install Rsyslog

Debian 12 默認沒有安裝rsyslog,因fail2ban需要依賴rsyslog來做處理,所以我們這裏需要在debian12 中安裝rsyslog。

apt-get install rsyslog
systemctl restart fail2ban 
systemctl status fail2ban

Install Iptables

fail2ban也依賴iptable 做防火牆規則,所以iptables也是必不可少需要安裝的,如果已經安裝,可以跳過。

apt install iptables

Other Command

  1. 查看当前被禁止登陆的ip
root@mycloud:/# fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- Filter
|  |- Currently failed: 1
|  |- Total failed:     9
|  `- File list:        /var/log/auth.log
`- Actions
   |- Currently banned: 2
   |- Total banned:     2
   `- Banned IP list:   192.168.0.237 192.168.174.44
  1. 取消被禁止的ip
fail2ban-client set ssh-iptables unbanip ${IP_ADDR}
  1. 重啓
fail2ban-client reload