为什么需要主动安全加固?
云服务器开机即暴露在公网,默认配置下SSH端口22每分钟可能遭受数百次暴力破解。统计显示,新部署的服务器平均在4分钟内就会收到第一次扫描探测。主动加固是必须而非可选项。
Step 1:禁用root直接登录,创建普通用户
# 创建新用户并加入sudo组
adduser deploy
usermod -aG sudo deploy
# 编辑SSH配置
vi /etc/ssh/sshd_config
# 设置:PermitRootLogin no
systemctl restart sshd
Step 2:SSH密钥登录,禁用密码认证
# 本地生成密钥对
ssh-keygen -t ed25519 -C "your@email.com"
# 将公钥上传到服务器
ssh-copy-id deploy@your-server-ip
# 禁用密码认证(sshd_config)
PasswordAuthentication no
PubkeyAuthentication yes
Step 3:修改SSH默认端口
将SSH端口从22修改为高位随机端口(如48522),可减少90%以上的自动化扫描流量:
# sshd_config
Port 48522
# 放行新端口后再重启
ufw allow 48522/tcp
systemctl restart sshd
Step 4:配置UFW防火墙
ufw default deny incoming
ufw default allow outgoing
ufw allow 48522/tcp # SSH
ufw allow 80/tcp # HTTP
ufw allow 443/tcp # HTTPS
ufw enable
ufw status verbose
Step 5:安装 Fail2Ban 防暴力破解
apt install fail2ban -y
# /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 48522
maxretry = 5
bantime = 3600
findtime = 600
systemctl enable fail2ban && systemctl start fail2ban
Step 6:自动安全更新
apt install unattended-upgrades -y
dpkg-reconfigure --priority=low unattended-upgrades
# 选择"Yes"启用自动安全更新
Step 7:关闭不必要的服务
# 查看开机自启服务
systemctl list-unit-files --type=service --state=enabled
# 禁用不需要的服务(示例)
systemctl disable bluetooth
systemctl disable avahi-daemon
Step 8:安装 rkhunter 检测 Rootkit
apt install rkhunter -y
rkhunter --update
rkhunter --check --sk
# 设置每日自动检测(crontab -e)
0 3 * * * /usr/bin/rkhunter --check --sk --report-warnings-only
Step 9:开启审计日志(auditd)
apt install auditd -y
systemctl enable auditd && systemctl start auditd
# 监控关键文件修改
auditctl -w /etc/passwd -p wa -k passwd_changes
auditctl -w /etc/sudoers -p wa -k sudoers_changes
Step 10:定期安全扫描
推荐工具:
- Lynis:开源系统安全审计工具,运行
lynis audit system获取安全评分和改进建议。 - ClamAV:开源防病毒,定期扫描上传目录。
- OpenVAS:漏洞扫描,发现未修补漏洞。
安全加固检查清单
| 步骤 | 操作 | 状态 |
|---|---|---|
| Step 1 | 禁用root登录,创建普通账户 | ☐ |
| Step 2 | 密钥登录,禁用密码认证 | ☐ |
| Step 3 | 修改SSH端口 | ☐ |
| Step 4 | 配置UFW防火墙 | ☐ |
| Step 5 | 安装Fail2Ban | ☐ |
| Step 6 | 启用自动安全更新 | ☐ |
| Step 7 | 关闭不必要服务 | ☐ |
| Step 8 | 安装rkhunter | ☐ |
| Step 9 | 开启auditd审计 | ☐ |
| Step 10 | 定期安全扫描 | ☐ |