跳转到内容

新机上线基线清单

每台新服务器从裸机到可用于生产,都需要经历一系列标准化配置。遗漏任何一步都可能在日后带来安全隐患或运维麻烦。本文提供一份经过实战验证的上线基线清单,适用于 CentOS Stream 9 & 10、AlmaLinux 9 & 10 和 Rocky Linux 9 & 10。

  • 已完成操作系统最小化安装
  • 拥有 root 或具备 sudo 权限的账户
  • 网络已联通,可以访问 yum/dnf 仓库

使用有意义的主机名命名,建议格式:用途-环境-编号,如 web-prod-01

Terminal window
# 设置主机名
hostnamectl set-hostname web-prod-01
# 验证
hostnamectl

同时更新 /etc/hosts,确保主机名可以本地解析:

Terminal window
echo "127.0.0.1 web-prod-01" >> /etc/hosts

统一使用与业务匹配的时区,国内服务器通常使用 Asia/Shanghai

Terminal window
# 查看当前时区
timedatectl
# 设置时区
timedatectl set-timezone Asia/Shanghai
# 验证
date

EL 9 默认使用 chrony 作为 NTP 客户端:

Terminal window
# 确认 chrony 已安装
dnf install -y chrony
# 编辑配置(可选,替换为内网或可信 NTP 源)
vi /etc/chrony.conf

推荐的 NTP 服务器配置示例:

# 国内常用公共 NTP 服务器
server ntp.aliyun.com iburst
server ntp.tencent.com iburst
server cn.ntp.org.cn iburst

启动并启用服务:

Terminal window
systemctl enable --now chronyd
# 验证同步状态
chronyc tracking
chronyc sources -v

上线前务必将系统更新到最新:

Terminal window
dnf update -y
# 查看是否需要重启(内核更新后)
needs-restarting -r

如果提示需要重启,可以在所有基线配置完成后统一重启。

禁止在生产环境中直接使用 root 登录。 创建专用管理员账户:

Terminal window
# 创建用户
useradd -m -s /bin/bash admin
# 设置密码
passwd admin
# 加入 wheel 组以获得 sudo 权限
usermod -aG wheel admin

验证 sudo 配置:

Terminal window
# 确认 wheel 组有 sudo 权限
grep '%wheel' /etc/sudoers
# 应输出:%wheel ALL=(ALL) ALL

SSH 是服务器最主要的攻击面之一,必须加固。

本地客户端生成密钥对(如果还没有):

Terminal window
ssh-keygen -t ed25519 -C "admin@web-prod-01"

将公钥复制到服务器:

Terminal window
ssh-copy-id admin@服务器IP

编辑 /etc/ssh/sshd_config.d/99-hardening.conf(EL 9 推荐使用 drop-in 配置):

Terminal window
cat > /etc/ssh/sshd_config.d/99-hardening.conf << 'EOF'
# 禁止 root 直接登录
PermitRootLogin no
# 禁止密码认证(确保密钥已部署)
PasswordAuthentication no
# 禁止空密码
PermitEmptyPasswords no
# 限制认证尝试次数
MaxAuthTries 3
# 限制并发未认证连接
MaxStartups 10:30:60
# 设置登录超时
LoginGraceTime 30
# 仅允许特定用户登录
AllowUsers admin
EOF

重启 SSH 服务:

Terminal window
systemctl restart sshd

EL 9 默认使用 firewalld

Terminal window
# 确认 firewalld 运行中
systemctl enable --now firewalld
# 查看当前规则
firewall-cmd --list-all
# 只允许 SSH(默认已开放)
firewall-cmd --permanent --zone=public --add-service=ssh
# 移除不需要的服务(如 cockpit、dhcpv6-client)
firewall-cmd --permanent --zone=public --remove-service=cockpit
firewall-cmd --permanent --zone=public --remove-service=dhcpv6-client
# 重新加载
firewall-cmd --reload
# 确认最终规则
firewall-cmd --list-all

根据业务需要,后续按需开放端口:

Terminal window
# 示例:开放 HTTP/HTTPS
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

SELinux 是 EL 系统重要的安全层,不要关闭它

Terminal window
# 检查状态
getenforce
# 应输出:Enforcing
sestatus

如果状态为 DisabledPermissive,需要启用:

Terminal window
# 编辑配置
sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
# 重启后生效(如果从 Disabled 切换,需要重新标记文件系统)
touch /.autorelabel

安装 SELinux 管理工具:

Terminal window
dnf install -y policycoreutils-python-utils setools-console

安装运维常用工具:

Terminal window
dnf install -y \
vim \
tmux \
htop \
iotop \
lsof \
strace \
tcpdump \
net-tools \
bind-utils \
wget \
curl \
tar \
unzip \
bash-completion \
man-pages \
yum-utils

可选的高级诊断工具:

Terminal window
dnf install -y \
sysstat \
perf \
bpftool \
nmap-ncat

node_exporter(Prometheus 生态)为例:

Terminal window
# 创建系统用户
useradd --no-create-home --shell /sbin/nologin node_exporter
# 下载并安装(请替换为最新版本号)
cd /tmp
curl -LO https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
tar xzf node_exporter-1.8.2.linux-amd64.tar.gz
cp node_exporter-1.8.2.linux-amd64/node_exporter /usr/local/bin/
# 创建 systemd 服务
cat > /etc/systemd/system/node_exporter.service << 'EOF'
[Unit]
Description=Prometheus Node Exporter
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=node_exporter
ExecStart=/usr/local/bin/node_exporter
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now node_exporter

开放监控端口(仅对监控服务器):

Terminal window
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.100/32" port protocol="tcp" port="9100" accept'
firewall-cmd --reload

borgbackup 为例配置基础备份:

Terminal window
dnf install -y epel-release
dnf install -y borgbackup
# 初始化备份仓库(本地或远程)
borg init --encryption=repokey /backup/borg-repo
# 创建首次备份脚本
cat > /usr/local/bin/backup.sh << 'SCRIPT'
#!/bin/bash
REPO="/backup/borg-repo"
TIMESTAMP=$(date +%Y-%m-%d_%H%M)
borg create --stats --compression zstd \
"${REPO}::${TIMESTAMP}" \
/etc \
/home \
/var/log \
--exclude '*.cache'
# 保留策略:7天 + 4周 + 6月
borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=6 "$REPO"
SCRIPT
chmod +x /usr/local/bin/backup.sh

配置定时执行:

Terminal window
# 每天凌晨 2 点执行备份
cat > /etc/cron.d/backup << 'EOF'
0 2 * * * root /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
EOF

确认 logrotate 已安装并正常运行:

Terminal window
# 通常已预装
dnf install -y logrotate
# 查看默认配置
cat /etc/logrotate.conf

为自定义应用日志添加轮转规则:

Terminal window
cat > /etc/logrotate.d/app-logs << 'EOF'
/var/log/app/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 0640 root root
sharedscripts
postrotate
/bin/systemctl reload rsyslog > /dev/null 2>&1 || true
endscript
}
EOF

手动测试轮转:

Terminal window
logrotate -d /etc/logrotate.d/app-logs

所有配置完成后,执行一次完整的重启测试:

Terminal window
# 重启前记录当前状态
uptime
systemctl list-units --failed
# 重启
reboot

重启后逐项检查:

Terminal window
# 1. 主机名
hostname
# 2. 时区和时间同步
timedatectl
chronyc tracking
# 3. SELinux
getenforce
# 4. 防火墙
firewall-cmd --list-all
# 5. SSH 服务
systemctl status sshd
# 6. 监控 Agent
systemctl status node_exporter
# 7. 无失败服务
systemctl list-units --failed
# 8. 磁盘和内存
df -h
free -h
序号检查项命令/验证方法预期结果
1主机名hostname有意义的名称
2时区timedatectl正确时区
3NTPchronyc tracking同步正常
4系统更新dnf check-update无可用更新
5管理员用户id admin存在且属于 wheel 组
6SSH 加固sshd -T | grep permitrootloginno
7防火墙firewall-cmd --list-all仅开放必要端口
8SELinuxgetenforceEnforcing
9基础工具which vim htop tmux均已安装
10监控systemctl status node_exporteractive (running)
11备份ls /backup/borg-repo仓库已初始化
12日志轮转logrotate -d /etc/logrotate.conf无错误
13重启测试systemctl list-units --failed0 failed

基线配置完成后,根据服务器角色继续进行: