使用fail2ban保护服务器
首先,服务器最终极的保护方式是禁止账号密码登录,只允许密钥登录。
但是比较麻烦的是,并不是什么时候都拥有密钥的。
对于我来说,比较重要的机器我会禁止账号密码登录,相对而言不太重要的机器我只会改一个ssh port,一般来说这足够了,不过为了安全起见,准备上fail2ban,超过n次密码错误直接禁用ip。
安装fail2ban
apt install fail2banfail2ban默认只对22端口有效。因为我改过端口,所以需要调整一下配置文件。
cd /etc/fail2ban
# 创建配置文件
touch jail.localjail.local写入如下内容
[sshd]
backend=systemd
enabled = true
port = your_real_ssh_port重启fail2ban
systemctl restart fail2ban查看fail2ban对端口是否生效
iptables -L -v -n如何解封ip?
fail2ban-client -i
#查看sshd状态
status sshd
#解封ip
set sshd unbanip [ip]启动失败报错Failed during configuration: Have not found any log file for <jail-name> jail
解决方案:在jail.conf加上backend = systemd
参考连接
https://serverfault.com/questions/285256/how-to-unban-an-ip-properly-with-fail2ban
https://serverfault.com/questions/382858/in-fail2ban-how-to-change-the-ssh-port-number
https://github.com/fail2ban/fail2ban/issues/2756
https://github.com/fail2ban/fail2ban/issues/3292
