查看是否被爆破
查看用密码登陆成功的IP地址及次数
grep "Accepted password for root" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -nr | more
查看用密码登陆失败的IP地址及次数
grep "Failed password for root" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -nr | more
更改默认端口
sed -i "s/Port .*/Port 你的端口/g" /etc/ssh/sshd_config
更改后重启ssh
/etc/init.d/sshd restart #centos系统
/etc/init.d/ssh restart #debian,ubuntu系统
安装Fail2ban自动配置防 SSH 爆破
原理
Fail2ban会通过检查日志来匹配错误信息,从而使用iptables来防止暴力破解。理论上只要是能够在服务器本地输出错误日志和访问日志的程序都可以使用Fail2ban来保驾护航。
功能
一键安装部署 Fail2ban,自动配置防 SSH 爆破。可自定义 ip 封禁时间,最高重试次数。
安装并运行
wget -N --no-check-certificate https://raw.githubusercontent.com/xyysjd/Fail2ban/master/fail2ban.sh && bash fail2ban.sh
卸载
wget -N --no-check-certificate https://raw.githubusercontent.com/xyysjd/Fail2ban/master/uninstall.sh &&
bash uninstall.sh
非22端口处理和使用ufw管理
1.打开 Fail2ban 配置文件:
vi /etc/fail2ban/jail.local
2.修改
banaction = ufw(添加这一行)
action = ufw[name=SSH, port=你自己修改的ssh端口, protocol=tcp]
然后重启
sudo systemctl restart fail2ban
功能
1.自助修改SSH端口
2.自定义最高封禁IP的时间(以小时为单位)
3.自定义SSH尝试连接次数
4.一键完成SSH防止暴力破解
查看被ban的IP列表
fail2ban-client status sshd
解封相关IP
fail2ban-client set sshd unbanip 要解封的ip
此时再查看banlist会发现IP已移除。
如果重启fail2ban后发现被移除的规则又回来了那么就
rm /var/lib/fail2ban/fail2ban.sqlite3
报错解决方案
使用以下命令卸载后重新安装
service fail2ban stop
rm -r /etc/fail2ban/
apt-get purge fail2ban
评论区