我的博客
HomePostsGitHubLinux安装fail2ban
环境
系统:Debian 12 x86_64 fail2ban版本:0.11.2
1. 安装软件
apt update
apt install fail2ban2. 启动fail2ban
systemctl start fail2ban开机自启
systemctl enable fail2ban查看运行状况
systemctl status fail2ban输出内容如下,表示启动成功
fail2ban.service - Fail2Ban Service
Loaded: loaded (/lib/systemd/system/fail2ban.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2023-03-07 14:47:28 CST; 4h 22min ago
Docs: man:fail2ban(1)
Main PID: 15545 (fail2ban-server)
Tasks: 5 (limit: 1929)
Memory: 15.5M
CPU: 3.348s
CGroup: /system.slice/fail2ban.service
└─15545 /usr/bin/python3 /usr/bin/fail2ban-server -xf start
Mar 07 14:47:28 ubuntu systemd[1]: Started Fail2Ban Service.
Mar 07 14:47:28 ubuntu fail2ban-server[15545]: Server ready3. 配置fail2ban
进入 /etc/fail2ban 目录,使用vim编辑 jail.local 文件,添加需要防止爆破的服务
比如,下面就是我自己的ssh服务的放爆破配置
[sshd]
enabled = true
filter = sshd
port = 22
maxretry = 5
findtime = 43200
bantime = 604800
action = %(action_mwl)s
logpath = /var/log/auth.log
enabled = true:启用此规则。filter = sshd:使用名为 sshd 的过滤器。port = 22:监控端口号为22。maxretry = 5:在 findtime 时间内最多允许失败5次。findtime = 43200:在12小时(43200秒)内查找失败登录尝试。bantime = 604800:禁止IP地址的时间为7天(604800秒)。action = %(action_mwl)s:执行操作为发送邮件并包含日志行和whois信息。logpath = /var/log/auth.log:日志文件路径为 /var/log/auth.log。
保存之后,运行命令重启 fail2ban
systemctl restart fail2ban通过 cat /var/log/fail2ban.log 文件,查看 fail2ban 的运行日志
4. 基础命令
查看 fail2ban 服务的状态。它将显示当前启用的监控规则列表以及其他统计信息
fail2ban-client status查看名为 sshd 的监控规则的状态。它将显示当前被禁止的IP地址列表以及其他统计信息
fail2ban-client status sshd解封名为 sshd 的监控规则中被禁止的IP地址 5.5.5.5
fail2ban-client set sshd unbanip 5.5.5.5更多命令,查看官方文档 Commands - Fail2ban
CC BY-NC 4.0 2025 © BLT.CC