起因:

  • 发现小鸡流量异常,还有个兄弟在某个帖子中说免费v4小鸡是ssh爆破重灾区后,检查了下自己的小鸡果然惨不忍睹,将所有独立v4小鸡都装上fail2ban后,遂有这篇交流学习帖子。

建议:

  • 不只装fail2ban,最好也要改了登陆端口,放弃密码登陆使用key密钥登陆。

免责:

本人小白一个,下列安装步骤仅供参考,如有不对之处请海涵。

1.安装fail2ban

apt updateapt install fail2ban

2.复制配置文件jail.local

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

3.写入配置文件。

nano /etc/fail2ban/jail.local

将[sshd]里的内容

[sshd]# To use more aggressive sshd modes set filter parameter "mode" in jail.local:# normal (default), ddos, extra or aggressive (combines all).# See "tests/files/logs/sshd" or "filter.d/sshd.conf" for usage example and details.#mode   = normalport    = sshlogpath = %(sshd_log)sbackend = %(sshd_backend)s
  • 替换为下列代码(我的比较极端,按需修改时间):

[sshd]enabled = truebackend = systemdport = sshfilter = sshdlogpath = sshdfindtime = 86400      ; 检测周期设置为24小时maxretry = 1         ; 首次封禁之前允许的最大尝试次数bantime = 86400       ; 初始封禁时间设置为24小时# 以下配置定义了增量封禁的规则bantime.increment = true     ; 启用增量封锁bantime.factor = 1           ; 封禁时间增加因子bantime.maxtime = 7776000    ; 封禁时间的最大值90天bantime.multipliers = 1 3 5 15 30 90    ; 禁时间的增加倍数

4.启动fail2ban

#启动fail2bansudo systemctl start fail2ban#设置开机启动sudo systemctl enable fail2ban

5.验证配置是否生效

#验证时间与你设置的是否相同fail2ban-client get sshd bantimefail2ban-client get sshd findtimefail2ban-client get sshd maxretry#如果不同,重新加载配置文件jail.localsudo systemctl reload fail2ban