趁着阿里云活动,买了一台一年99的VPS,装了debian12
root@debian:~# neofetch
_,met$$$$$gg.
,g$$$$$$$$$$$$$$$P. --------------
,g$$P" """Y$$.". OS: Debian GNU/Linux 12 (bookworm) x86_64
,$$P' `$$$. Host: Alibaba Cloud ECS pc-i440fx-2.1
',$$P ,ggs. `$$b: Kernel: 6.1.0-13-amd64
`d$$' ,$P"' . $$$ Uptime: 6 hours, 59 mins
$$P d$' , $$P Packages: 640 (dpkg)
$$: $$. - ,d$$' Shell: bash 5.2.15
$$; Y$b._ _,d$P' Resolution: 1024x768
Y$$. `.`"Y$$$$P"' Terminal: /dev/pts/0
`$$b "-.__ CPU: Intel Xeon Platinum (2) @ 2.500GHz
`Y$$ GPU: 00:02.0 Cirrus Logic GD 5446
`Y$$. Memory: 181MiB / 1725MiB
`$$b.
`Y$$b.
`"Y$b._
`"""
安装fail2ban
apt install fail2ban
#看一下状态
systemctl status fail2ban
配置一下防止爆破ssh
nano /etc/fail2ban/jail.d/defaults-debian.conf
[DEFAULT]
# 用于指定哪些地址ip可以忽略 fail2ban 防御,以空格间隔。
ignoreip = 127.0.0.1/8
# ssh客户端主机被禁止的时长(默认单位为秒)
bantime = 86400
# 过滤的时长(秒)
findtime = 600
# 匹配到的阈值(允许失败次数)
maxretry = 4
[ssh-iptables]
# 是否开启
enabled = true
# 过滤规则
port = 22
filter = sshd
# debian日志文件的路径
logpath = /var/log/auth.log
# centos7日志文件的路径
#logpath = /var/log/secure
# 匹配到的阈值(次数)
maxretry = 4
启动发现fail2ban启动失败,原因是找不到日志文件,发现默认情况下,debian不再有/var/log/auth.log文件
,解决办法是安装rsyslog,安装后就有auth.log文件了。
apt-get install rsyslog
systemctl restart fail2ban
systemctl status fail2ban
这次fail2ban启动正常了,也开始工作了。但发现ban的ip尽然还能访问,查了一圈发现是没有iptables,fail2ban依赖于iptables实现ban ip,好吧,安装iptables:
apt install iptables
现在一切正常了。
常用命令:
#查看当前被禁止登陆的ip
fail2ban-client status ssh-iptables
[root@debian]# fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- Filter
| |- Currently failed: 0 # 当前失败次数
| |- Total failed: 3 # 总失败次数
| `- File list: /var/log/auth.log # 日志文件路径列表
`- Actions
|- Currently banned: 1 # 当前禁止访问的IP数量
|- Total banned: 1 # 禁止IP总数
#取消ban某个ip
fail2ban-client set ssh-iptables unbanip 42.34.45.78
#如果修改了配置需要重启生效:
fail2ban-client reload