linux (ubuntu)系统防暴利破解配置
暴力破解(brute force),可以从以下几个方面进行安全加固:
✅ 一、禁用 root 远程登录(推荐)
编辑 SSH 配置文件,禁止 root 直接通过 SSH 登录:
sudo nano /etc/ssh/sshd_config
修改或添加以下内容:
PermitRootLogin no
然后重启 SSH 服务:
sudo systemctl restart ssh
💡 此方法建议在已经创建了另一个普通用户并设置了
sudo
权限的情况下进行。
✅ 二、配置 fail2ban 自动封禁暴力 IP
fail2ban
可以检测登录失败日志,自动封锁可疑 IP。
1. 安装 fail2ban:
sudo apt update
sudo apt install fail2ban -y
2. 自定义配置文件:
复制默认配置,避免直接修改原文件:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
编辑 jail.local
文件:
sudo nano /etc/fail2ban/jail.local
在 [sshd]
区块确保如下配置开启:
[sshd] enabled = true port = ssh filter = sshd logpath = /var/log/auth.log maxretry = 5 findtime = 6000 bantime = 36000
-
maxretry=5
:5 次失败后封禁; -
findtime=6000
:100 分钟内超过maxretry
次; -
bantime=36000
:封禁 10 小时。
然后启动并启用 fail2ban:
sudo systemctl enable --now fail2ban
查看状态:
sudo fail2ban-client status sshd
✅ 三、设置 SSH 使用密钥登录,禁用密码登录
1. 创建密钥(在客户端机器):
ssh-keygen -t rsa -b 4096
2. 上传公钥到服务器:
ssh-copy-id root@your_server_ip
3. 禁用密码登录:
编辑 SSH 配置:
sudo nano /etc/ssh/sshd_config
设置:
PasswordAuthentication no
重启 SSH:
sudo systemctl restart ssh
✅ 四、更换 SSH 默认端口
可降低被扫描频率:
sudo nano /etc/ssh/sshd_config
比如改为:
Port 2222
然后:
sudo ufw allow 2222/tcp
sudo systemctl restart ssh
✅ 五、使用防火墙(UFW)只允许特定 IP 访问 SSH
sudo ufw allow from 192.168.1.100 to any port 22 proto tcp
(只允许指定 IP 登录)