zl程序教程

您现在的位置是:首页 >  系统

当前栏目

CentOS7部署fail2ban

centos7部署 Fail2Ban
2023-06-13 09:14:08 时间

修改ssh日志

ssh日志默认存放在/var/log/secure文件中,修改默认存放的位置

vim /etc/ssh/sshd_config
	SyslogFacility local1

vim /etc/rsyslog.conf
	local1.*	/var/log/sshd.log

systemctl restart rsyslog
systemctl restart sshd

安装fail2ban

yum install epel-release
yum install fail2ban

修改fail2ban配置

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

vim /etc/fail2ban/jail.local
	[DEFAULT]
	ignoreip = 127.0.0.1	# 忽略的IP列表,不受限制
	bantime = 10m		# 屏蔽时间
	findtime = 10m		# 在该时间段内超过尝试次数会被ban掉
	maxretry = 3		# 尝试次数
	backend = auto
	banaction = iptables-multiport	# 如果防火墙使用的是iptables,则使用默认配置,无须修改;如果使用的是firewalld防火墙,则需要修改为firewallcmd-ipset

vim /etc/fail2ban/jail.d/sshd.local
	[sshd]
	enabled = true		# 是否激活此项(true | false)
	port = ssh
	logpath = /var/log/sshd.log
	maxretry = 3
	bantime = 3600
	action = iptables-multiport	# 如果防火墙使用的是iptables,则使用默认配置,无须修改;如果使用的是firewalld防火墙,则需要修改为firewallcmd-ipset

启动服务

systemctl start iptables
systemctl start fail2ban

常用命令

fail2ban-client status				# 查看fail2ban状态
fail2ban-client status sshd			# 查看ban掉的IP列表
fail2ban-client set sshd addignoreip 1.1.1.1	# 添加白名单
fail2ban-client set sshd delignoreip 1.1.1.1	# 删除白名单
fail2ban-client set sshd unbanip 1.1.1.1	# 取消被ban的IP