如何在CentOS/RHEL 7/6上安装Fail2Ban

时间:2019-05-29 14:47:31  来源:igfitidea点击:

Fail2ban扫描系统上创建的日志文件,并能够根据配置规则禁止恶意ip。
我们可以使用它来监视各种系统服务日志,如Apache, SSH 并封锁试图破坏系统安全的IP。

步骤1 -在CentOS上安装Fail2ban

首先,在CentSO系统上启用 epel-releaseyum存储库。
然后使用以下命令安装Fail2ban rpm包。

sudo yum install epel-release
sudo yum install fail2ban

步骤2 - Fail2ban默认配置

Fail2ban提供了自己的安全配置文件 /etc/fail2ban/jail.conf,但是我们需要创建这个文件的一个副本 jail.local

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo vi /etc/fail2ban/jail.local

创建禁止规则,编辑 jail.local,并在 [DEFAULT]部分中进行更改。

[DEFAULT]

# "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will use as always allowed,
# so add your system ip to protect your ip from banned.
ignoreip = 127.0.0.1/8 192.168.1.0/24 11.22.33.44

# "bantime" is the total number of seconds that a host is banned (3600sec = 1 Hour ).
bantime  = 3600

# A host is banned if it has generated "maxretry" during the last "findtime" seconds. as per below
# settings, 2 minutes
findtime  = 120

# "maxretry" is the number of failures before a host get banned.
maxretry = 3

步骤3 -保护SSH/SFTP

完成默认配置后,修改 [ssh-iptables]部分如下所示。

enabled  = true
filter   = sshd
action   = iptables[name=SSH, port=22, protocol=tcp]
           sendmail-whois[name=SSH, dest=root, Hyman@theitroad, sendername="Fail2Ban"]
logpath  = /var/log/secure
maxretry = 3

步骤4 -保护FTP

找到以下 [vsFTPd -iptables]部分的条目并进行如下更改。
如果不使用vsFTPd,可以跳过此部分。

[vsftpd-iptables]

enabled  = true
filter   = vsftpd
action   = iptables[name=VSFTPD, port=21, protocol=tcp]
           sendmail-whois[name=VSFTPD, Hyman@theitroad]
logpath  = /var/log/vsftpd.log
maxretry = 5
bantime  = 1800

第4步-重启Fail2ban服务

完成所有更改后,保存文件并使用以下命令重新启动Fail2ban服务。

sudo service fail2ban restart