在阿里云上安装 CentOS Stream 8 后如何配置基础安全策略?

在阿里云上安装 CentOS Stream 8 后,建议从以下几个关键方面配置基础安全策略,以构建纵深防御体系:

一、系统账户与认证安全

1. 禁用 root 远程登录

# 编辑 SSH 配置文件
sudo vi /etc/ssh/sshd_config

# 修改以下参数
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes

重启 SSH 服务:sudo systemctl restart sshd

2. 创建专用管理员账户

# 创建新用户(替换 username)
sudo useradd -m -G wheel username
sudo passwd username

# 确保新用户可以 sudo(CentOS Stream 8 中 wheel 组默认有 sudo 权限)

3. 增强密码策略

# 安装并配置 password-auth
sudo dnf install authselect
authselect select sssd with-pam_password_change

二、网络防火墙配置

1. 启用 firewalld 并配置规则

# 启动并设置开机自启
sudo systemctl enable --now firewalld

# 允许 SSH(默认已开放,但建议明确配置)
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload

# 仅允许特定 IP 访问 SSH(可选)
# sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" service name="ssh" accept'

2. 关闭不必要的服务

# 检查并停止非必要服务
sudo systemctl disable --now cups bluetooth NetworkManager-wait-online

三、系统更新与漏洞管理

1. 配置自动安全更新

# 安装 yum-cron
sudo dnf install yum-cron

# 编辑配置文件
sudo vi /etc/yum-cron.conf

# 设置每日自动安装安全更新
download_updates = yes
apply_updates = yes
report_results = email

# 启动服务
sudo systemctl enable --now yum-cron

2. 定期手动更新

# 每周执行一次全面更新
sudo dnf update --security

四、日志监控与审计

1. 启用 auditd

# 安装并启动
sudo dnf install audit
sudo systemctl enable --now auditd

# 添加关键文件监控规则
echo '-w /etc/passwd -p wa -k identity' | sudo tee -a /etc/audit/rules.d/identity.rules
echo '-w /etc/shadow -p wa -k identity' | sudo tee -a /etc/audit/rules.d/identity.rules
echo '-w /etc/sudoers -p wa -k privilege' | sudo tee -a /etc/audit/rules.d/privilege.rules

2. 配置日志轮转

# 确保 rsyslog 正常运行
sudo systemctl enable --now rsyslog

# 查看安全相关日志
journalctl -u sshd -f

五、阿里云特有安全加固

1. 配置安全组规则

  • 最小化开放端口:仅开放必要端口(如 22, 80, 443)
  • 限制源 IP:SSH 端口仅对管理 IP 开放
  • 使用阿里云云盾:启用 Web 应用防火墙和主机安全

2. 配置云监控告警

# 安装阿里云监控插件
curl https://goss.alicdn.com/aliyun-monitor/install.sh | sh

# 配置关键指标告警(CPU、内存、磁盘等)

六、额外安全建议

1. 启用 SELinux

# 确认 SELinux 状态
getenforce

# 如果为 Disabled,修改配置
sudo vi /etc/selinux/config
SELINUX=enforcing

# 重启生效
sudo reboot

2. 配置 fail2ban 防止暴力破解

sudo dnf install fail2ban
sudo systemctl enable --now fail2ban

# 自定义防护规则
sudo vi /etc/fail2ban/jail.local
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/secure
maxretry = 3
bantime = 3600

3. 定期备份配置

# 创建备份脚本
cat > /usr/local/bin/backup-config.sh << 'EOF'
#!/bin/bash
BACKUP_DIR="/root/backups"
DATE=$(date +%Y%m%d)
tar -czf $BACKUP_DIR/etc-backup-$DATE.tar.gz /etc
find $BACKUP_DIR -name "*.tar.gz" -mtime +7 -delete
EOF
chmod +x /usr/local/bin/backup-config.sh

重要提示:所有修改前请务必备份原配置文件。在阿里云环境中,建议结合控制台的安全组、RAM 角色、云防火墙等多层防护机制,形成完整的安全体系。定期检查 /var/log/securejournalctl 日志,及时发现异常登录尝试。