Linux 服务器安全加固指南:15 个必做配置

刚买的云服务器默认配置并不安全。以下是我每台服务器上线前的 15 项安全检查清单。

1. 更新系统

# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y

# CentOS/RHEL
sudo yum update -y

# 启用自动安全更新
sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades

2. 创建非 root 用户

永远不要使用 root 用户进行日常操作:

# 创建新用户
sudo useradd -m -s /bin/bash deploy
sudo passwd deploy

# 添加到 sudo 组
sudo usermod -aG sudo deploy

# 测试
su - deploy
sudo whoami  # 应输出 root

3. SSH 安全配置

编辑 /etc/ssh/sshd_config

# 禁用 root 登录
PermitRootLogin no

# 修改默认端口(减少扫描)
Port 2222

# 仅允许密钥登录
PasswordAuthentication no
PubkeyAuthentication yes

# 限制登录用户
AllowUsers deploy

# 空闲超时
ClientAliveInterval 300
ClientAliveCountMax 0

# 限制认证尝试次数
MaxAuthTries 3

重启 SSH 服务:sudo systemctl restart sshd

4. 配置防火墙

# UFW (Ubuntu)
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 2222/tcp      # SSH
sudo ufw allow 80/tcp         # HTTP
sudo ufw allow 443/tcp        # HTTPS
sudo ufw enable

# firewalld (CentOS)
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

5. 安装 Fail2Ban

防止暴力破解:

sudo apt install fail2ban -y

# 配置 /etc/fail2ban/jail.local
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 5

[sshd]
enabled = true
port = 2222

sudo systemctl enable fail2ban
sudo systemctl start fail2ban

# 查看封禁状态
sudo fail2ban-client status sshd

6. 禁用不需要的服务

# 查看运行中的服务
sudo systemctl list-unit-files --state=enabled

# 禁用不需要的服务
sudo systemctl disable --now bluetooth.service
sudo systemctl disable --now cups.service
sudo systemctl disable --now avahi-daemon.service

7. 配置日志监控

# 安装 logwatch
sudo apt install logwatch -y

# 配置每日报告
sudo cp /usr/share/logwatch/default.conf/logwatch.conf \
  /etc/logwatch/conf/logwatch.conf

# 编辑配置
# MailTo = your@email.com
# Detail = High
# Range = yesterday

8. 内核参数加固

编辑 /etc/sysctl.d/99-security.conf

# 防止 IP 欺骗
net.ipv4.conf.all.rp_filter = 1

# 忽略 ICMP 重定向
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0

# 忽略 ICMP 广播请求
net.ipv4.icmp_echo_ignore_broadcasts = 1

# 启用 SYN Cookie 防护
net.ipv4.tcp_syncookies = 1

# 记录异常包
net.ipv4.conf.all.log_martians = 1

应用配置:sudo sysctl -p /etc/sysctl.d/99-security.conf

9. 文件权限加固

# 限制 cron 访问
sudo chmod 600 /etc/crontab
sudo chmod 700 /etc/cron.d
sudo chmod 700 /etc/cron.daily
sudo chmod 700 /etc/cron.hourly
sudo chmod 700 /etc/cron.weekly
sudo chmod 700 /etc/cron.monthly

# 保护 SSH 密钥
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub

10. 安装入侵检测系统

# AIDE - 文件完整性检查
sudo apt install aide -y
sudo aideinit
sudo mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db

# 定期检查
sudo aide --check

11. 限制用户资源

编辑 /etc/security/limits.conf

* hard nproc 4096
* hard nofile 65535
* soft nofile 65535

12. 使用 SELinux 或 AppArmor

# Ubuntu: 确认 AppArmor 状态
sudo aa-status

# CentOS: 确认 SELinux 状态
sestatus
# 如果是 enforcing 模式则已启用

13. 配置自动安全审计

# 安装 Lynis
sudo apt install lynis -y

# 运行审计
sudo lynis audit system

# 查看建议
sudo lynis show suggestions

14. 启用 HTTPS(Let's Encrypt)

# 安装 Certbot
sudo apt install certbot python3-certbot-nginx -y

# 获取证书
sudo certbot --nginx -d example.com -d www.example.com

# 自动续期
sudo certbot renew --dry-run

15. 备份策略

# 简单的 rsync 备份
rsync -avz --delete /var/www/ backup@backup-server:/backups/www/

# 数据库备份
mysqldump -u root -p database_name | gzip > /backups/db_$(date +%Y%m%d).sql.gz

# 加入 cron
0 2 * * * /usr/local/bin/backup-script.sh

安全检查清单

完成以上配置后,你可以使用这份检查清单验证:

  • 系统已更新到最新
  • root 无法 SSH 登录
  • SSH 仅允许密钥认证
  • 防火墙已启用并正确配置
  • Fail2Ban 正在运行
  • 不需要的服务已禁用
  • 内核参数已加固
  • 文件权限正确设置
  • 自动安全更新已启用

安全不是一次性工作,而是持续的过程。定期审查日志、更新系统、检查入侵迹象,才能保持服务器的长期安全。