VPS 服务器暴露在公网上,每天都会遭受大量扫描和攻击。本文介绍 8 个必做的安全加固措施。
1. 修改 SSH 默认端口
编辑 /etc/ssh/sshd_config:
Port 2222 # 改为非标准端口
PermitRootLogin no # 禁止 root 直接登录
PasswordAuthentication no # 禁用密码登录
重启 SSH:systemctl restart sshd
2. 配置防火墙
使用 UFW(Ubuntu/Debian):
ufw default deny incoming
ufw default allow outgoing
ufw allow 2222/tcp # SSH
ufw allow 80/tcp # HTTP
ufw allow 443/tcp # HTTPS
ufw enable
3. 安装 Fail2Ban
自动封禁暴力破解 IP:
apt install fail2ban -y
systemctl enable fail2ban
systemctl start fail2ban
配置 /etc/fail2ban/jail.local:
[sshd]
enabled = true
maxretry = 3
bantime = 3600
4. 启用自动安全更新
apt install unattended-upgrades -y
dpkg-reconfigure -plow unattended-upgrades
5. 使用密钥登录
生成 SSH 密钥对:
ssh-keygen -t ed25519 -C "your_email@example.com"
ssh-copy-id -p 2222 user@your-vps-ip
6. 配置 2FA 双因素认证
apt install libpam-google-authenticator -y
google-authenticator
在 /etc/pam.d/sshd 添加:
auth required pam_google_authenticator.so
7. 监控异常登录
查看登录日志:
last -a | head -20
journalctl -u ssh -n 50
8. 定期备份
使用 rsync 自动备份:
rsync -avz /var/www/ backup-server:/backups/www/
安全检查清单
- ✓ SSH 端口已修改
- ✓ 防火墙已配置
- ✓ Fail2Ban 已启用
- ✓ 自动更新已开启
- ✓ 密钥登录已配置
- ✓ 定期备份已设置
总结
安全加固是一个持续的过程,建议每月检查一次安全日志,及时发现异常。
Comments NOTHING