CentOS7安装异常登录封ip

封禁异常ip

总会被攻击


Last failed login: Thu Mar 20 02:39:10 CST 2025 from 196.251.80.174 on ssh:notty
There were 209 failed login attempts since the last successful login.
Last login: Tue Mar 18 14:13:39 2025 from 120.11.213.184

为防止未知 IP 的攻击登录尝试,并且自动封锁登录失败超过一定次数的 IP,你可以借助 fail2ban 工具来实现。fail2ban 能够监控系统日志,一旦检测到登录失败次数超出预设阈值,就会自动封锁对应的 IP。

yum install fail2ban -y
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
vim /etc/fail2ban/jail.local



[ssh]
enabled = true
port    = ssh
filter  = sshd
logpath = /var/log/secure
maxretry = 3
findtime = 600
bantime  = 3600

systemctl start fail2ban
systemctl enable fail2ban

fail2ban-client status ssh

以上配置参数的含义如下:

  • enabled = true:开启 fail2ban 对 SSH 服务的监控。
  • port = ssh:监控 SSH 端口。
  • filter = sshd:采用 sshd 过滤器来解析 SSH 日志。
  • logpath = /var/log/secure:指定 SSH 日志文件的路径。
  • maxretry = 3:在 findtime 时间内,允许的最大失败登录次数为 3 次。
  • findtime = 600:在 600 秒(即 10 分钟)内计算失败登录次数。
  • bantime = 3600:被封锁的 IP 封禁时长为 3600 秒(即 1 小时)。
posted @ 2025-11-06 15:52  迷路的哨兵甲  阅读(8)  评论(0)    收藏  举报