软件介绍

Fail2ban是一款轻量级的Linux安全工具,它可以自动检测到可疑IP多次尝试登录SSH,并将其加入黑名单以阻止恶意登录、密码暴力破解和其他恶意行为。保证您的系统安全,推荐每台机器都安装。

一键脚本

bash <(curl -L -s https://raw.githubusercontent.com/shawnwongx/Liunx-Scripts/main/fail2ban.sh)

查看运行状态

systemctl status fail2ban

查看被ban的IP

fail2ban-client status sshd

查看日志

cat /var/log/fail2ban.log

取消被拉黑的IP(IP_ADDRESS改成要取消的IP地址)

sudo fail2ban-client set sshd unbanip IP_ADDRESS

默认ssh端口是22,3次密码错误会被自动拉黑,拉黑时间是永久,如果你需要自定义的话,请使用下面的代码手动安装或者在一键脚本安装后自行编辑/etc/fail2ban/jail.local

手动安装

Debian/Ubuntu

sudo apt-get install -y fail2ban
sudo systemctl start fail2ban

Centos

sudo yum install -y fail2ban
sudo systemctl start fail2ban

创建本地配置文件

sudo touch /etc/fail2ban/jail.local

填入配置

port是ssh端口,maxretry是尝试次数,bantime是拉黑时间,单位是秒,比如要1小时填入3600,-1是永久,可以按自己的需求修改

echo '[sshd]
enabled = true
port = 22
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = -1' >> /etc/fail2ban/jail.local

启动服务

sudo systemctl restart fail2ban