如何在CentOS 8上使用Fail2Ban保护SSH
ssh日志文件显示太多密码失败。
如何在CentOS 8 Linux服务器上使用fail2ban保护ssh?
如何在CentOS 8上安装Fail2Ban?
通常,Internet上的所有人都可以看到SSH TCP端口22。
结果,许多僵尸程序和攻击者会尝试使用随机密码/用户登录您的服务器。
OpenSSH服务器和客户端本身是相当安全的,但是像其他所有东西一样,可以通过蛮力攻击来破解它。
这是来自我的服务器的示例消息:您可以使用Fail2ban安全应用程序保护ssh和其他服务,该应用程序在几次ssh登录尝试失败后都禁止IP地址。
本教程将学习如何在CentOS 8 Linux服务器上安装和配置Fail2ban。
如何在CentOS 8上安装Fail2Ban
设置和配置Fail2ban以保护服务器安全的过程如下:
- 使用ssh登录到CentOS 8服务器
- 在CentOS 8上启用并安装EPEL存储库,运行:
sudo yum install epel-release
- 安装Fail2Ban,运行:
sudo yum install fail2ban
- 配置Fail2ban
- 启用并启动Fail2ban服务:
sudo systemctl enable fail2ban && sudo systemctl start fail2ban
让我们详细了解所有命令和选项。
其中可以找到失败的ssh登录尝试
使用grep命令/egrep命令或cat命令/tail命令/less命令/more命令查看/var/log/secure
:
tail -f /var/log/secure grep 'sshd.*Failed password for' /var/log/secure
输出示例:
Sep 26 10:08:13 localhost sshd[16031]: Failed password for root from 49.88.112.90 port 15595 ssh2 Sep 26 10:13:19 localhost sshd[16039]: Failed password for root from 222.186.52.89 port 58696 ssh2 Sep 26 10:13:22 localhost sshd[16039]: Failed password for root from 222.186.52.89 port 58696 ssh2 Sep 26 10:13:26 localhost sshd[16039]: Failed password for root from 222.186.52.89 port 58696 ssh2 Sep 26 10:16:16 localhost sshd[17218]: Failed password for root from 49.88.112.80 port 41089 ssh2 Sep 26 10:16:19 localhost sshd[17218]: Failed password for root from 49.88.112.80 port 41089 ssh2 Sep 26 10:19:14 localhost sshd[17226]: Failed password for root from 153.36.236.35 port 44787 ssh2 Sep 26 10:19:17 localhost sshd[17226]: Failed password for root from 153.36.236.35 port 44787 ssh2 Sep 26 10:19:19 localhost sshd[17226]: Failed password for root from 153.36.236.35 port 44787 ssh2 Sep 26 10:22:06 localhost sshd[17260]: Failed password for root from 222.186.30.165 port 22558 ssh2 Sep 26 10:22:08 localhost sshd[17260]: Failed password for root from 222.186.30.165 port 22558 ssh2
在CentOS 8上使用Fail2Ban保护SSH
首先在CentOS 8上启用并安装EPEL Repo,运行:
sudo yum update sudo yum install epel-release sudo yum update
在CentOS 8上安装Fail2ban
由于您启用了ELEP repo,我们可以使用yum命令如下安装Fail2ban:
sudo yum install fail2ban
现在是时候在引导时使用systemctl命令启用fail2ban保护服务了,运行:
sudo systemctl enable fail2ban
输出示例:
Created symlink /etc/systemd/system/multi-user.target.wants/fail2ban.service ? /usr/lib/systemd/system/fail2ban.service.
配置Fail2ban设置
/etc/fail2ban/jail.local文件将覆盖/etc/fail2ban/jail.conf文件中设置的默认值。
因此,使用文本编辑器(例如vi/vim或nano/emacs)创建或编辑jail.local文件:
sudo vi /etc/fail2ban/jail.local
更新/追加如下:
[DEFAULT] # Ban IP/hosts for 24 hour ( 24h*3600s = 86400s): bantime = 86400 # An ip address/host is banned if it has generated "maxretry" during the last "findtime" seconds. findtime = 600 maxretry = 3 # "ignoreip" can be a list of IP addresses, CIDR masks or DNS hosts. Fail2ban # will not ban a host which matches an address in this list. Several addresses # can be defined using space (and/or comma) separator. For example, add your # static IP address that you always use for login such as 103.1.2.3 #ignoreip = 127.0.0.1/8 ::1 103.1.2.3 # Call iptables to ban IP address banaction = iptables-multiport # Enable sshd protection [sshd] enabled = true
保存并退出文件。
接下来启动服务,运行:
sudo systemctl start fail2ban sudo systemctl status fail2ban
如何启动/停止/重启fail2ban服务?
语法如下:
sudo systemctl start fail2ban sudo systemctl stop fail2ban sudo systemctl restart fail2ban sudo systemctl status fail2ban
查找失败和禁止的IP地址的状态
运行以下两个命令:
sudo fail2ban-client status sudo fail2ban-client status sshd
读取包含密码故障报告并显示当前状态的日志文件
Fail2ban过滤器
将CD放入/etc/fail2ban/filter.d,您可以查看所有过滤器:
cd /etc/fail2ban/filter.d ls
例如,显示opensh的Fail2Ban过滤器,运行cat命令:
sudo cat /etc/fail2ban/filter.d/sshd.conf
或者
sudo vi /etc/fail2ban/filter.d/sshd.conf
不要编辑此文件。
为了进行定制,请创建一个名为/etc/fail2ban/jail.d/sshd.conf.local的文件。
获取有关禁止的IP地址和日志文件的更多信息
执行以下命令:
tail -f /var/log/fail2ban.log grep IP-address /var/log/fail2ban.log sudo iptables -L -n -v sudo iptables -L f2b-sshd -n -v sudo iptables -S | f2b-sshd