0
点赞
收藏
分享

微信扫一扫

centos服务器登录失败次数设定

SPEIKE 2024-11-08 阅读 11

实现的效果

一台centos服务,如果被别人暴力或者登录次数超过多少次,就拒绝或者在规定时间内拒绝ip登录。这里使用的是fail2ban

安装fail2ban

sudo yum install epel-release -y  # 先安装 EPEL 源
sudo yum install fail2ban -y

配置fail2ban

# 复制默认配置文件生成一个新配置
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
# 编辑 /etc/fail2ban/jail.local 文件
sudo vi /etc/fail2ban/jail.local

# 增加sshd模块信息
[sshd]
enabled = true                # 启用 SSH 规则
port = ssh                    # 监控 SSH 服务端口
filter = sshd                 # 使用默认的 SSH 过滤器
logpath = /var/log/secure     # 日志路径
maxretry = 5                  # 最大尝试次数,默认设置为5
bantime = 3600                # 封禁时间,单位为秒(这里设置为1小时,可根据需求调整)
findtime = 600                # 时间窗口,在findtime秒内尝试次数达到maxretry即封禁

# 启动并启用 Fail2ban 服务
sudo systemctl start fail2ban
sudo systemctl enable fail2ban

# 检查 Fail2ban 状态
sudo fail2ban-client status

# 要查看 SSH 封禁的 IP 列表(如果没有封禁的会报 Sorry but the jail 'sshd' does not exist)
sudo fail2ban-client status sshd

# 如果需要手动解封某个 IP,可以运行以下命令
sudo fail2ban-client set sshd unbanip <IP地址>


举报

相关推荐

0 条评论