Linux 服务器安全加固指南

为什么要加固?

买了云服务器,第一件事不是部署应用,而是做安全加固。每天都有无数扫描器在尝试暴力破解你的 SSH 密码。

SSH 加固

禁用密码登录

# /etc/ssh/sshd_config
PasswordAuthentication no
PermitRootLogin prohibit-password
PubkeyAuthentication yes

更换 SSH 端口

Port 2222  # 避开默认的 22 端口

重启 SSH

systemctl restart sshd

防火墙配置

使用 firewalld

# 只开放必要端口
firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --reload

Fail2ban 防暴力破解

yum install -y fail2ban

cat > /etc/fail2ban/jail.local << EOF
[sshd]
enabled = true
port = 2222
maxretry = 3
bantime = 3600
EOF

systemctl enable --now fail2ban

系统更新

# 设置自动安全更新
yum install -y yum-cron
systemctl enable --now yum-cron

检查清单

  • [x] SSH 密钥登录
  • [x] 禁用密码认证
  • [x] 修改 SSH 端口
  • [x] 防火墙白名单
  • [x] Fail2ban 防暴破
  • [ ] 定期备份
  • [ ] 日志监控