飞牛OS自动封禁IP配置,使用fail2ban防护sshd

0 阅读1分钟

飞牛使用fail2ban防护sshd

我的飞牛是公网IP,我最近打开飞牛日志发现SSH,半个月就被尝试登录了9800个账号密码。所以现在用fail2ban加一层防护,密码错误一次封禁1天!我自己是本地工具记住密码了,所以不怕被误伤。 在这里插入图片描述

命令如下

安装fail2ban

sudo apt install fail2ban -y

查看当前配置文件

cat /etc/fail2ban/jail.local 

更新的配置文件 (0容忍,错一次封1天,根据自己需求修改)

cat > /etc/fail2ban/jail.local << EOF
# 默认配置
[DEFAULT]
# 白名单ip网段,多个网段使用空格分隔
ignoreip = 127.0.0.1/8 ::1

loglevel=3
logencoding = auto

#配置ssh服务规则
[sshd]
# 启用
enabled  = true
# 过滤规则
filter = sshd
# 指定日志来源通过journalctl获取
backend = systemd

# 失败检测时间范围 单位:缺省时为s(秒)、m(分钟)、d(天)、w(周)、mo(月)、y(年)
findtime = 1d
# 最大失败次数
maxretry = 1

# 封锁时间 单位:缺省时为s(秒)、m(分钟)、d(天)、w(周)、mo(月)、y(年)、特殊值:-1(永久封禁)
bantime = 1d
EOF

重启服务

sudo systemctl restart fail2ban

设置开机自启

sudo systemctl enable fail2ban

查看服务状态

sudo systemctl status fail2ban

查看sshd封禁情况

sudo fail2ban-client status sshd

解锁误封ip

fail2ban-client set [规则名称] unbanip  [ip]
#如: sudo fail2ban-client set sshd unbanip 2.2.2.2