新机上线基线清单
每台新服务器从裸机到可用于生产,都需要经历一系列标准化配置。遗漏任何一步都可能在日后带来安全隐患或运维麻烦。本文提供一份经过实战验证的上线基线清单,适用于 CentOS Stream 9 & 10、AlmaLinux 9 & 10 和 Rocky Linux 9 & 10。
- 已完成操作系统最小化安装
- 拥有 root 或具备 sudo 权限的账户
- 网络已联通,可以访问 yum/dnf 仓库
第 1 步:设置主机名
Section titled “第 1 步:设置主机名”使用有意义的主机名命名,建议格式:用途-环境-编号,如 web-prod-01。
# 设置主机名hostnamectl set-hostname web-prod-01
# 验证hostnamectl同时更新 /etc/hosts,确保主机名可以本地解析:
echo "127.0.0.1 web-prod-01" >> /etc/hosts第 2 步:配置时区
Section titled “第 2 步:配置时区”统一使用与业务匹配的时区,国内服务器通常使用 Asia/Shanghai:
# 查看当前时区timedatectl
# 设置时区timedatectl set-timezone Asia/Shanghai
# 验证date第 3 步:配置 NTP 时间同步
Section titled “第 3 步:配置 NTP 时间同步”EL 9 默认使用 chrony 作为 NTP 客户端:
# 确认 chrony 已安装dnf install -y chrony
# 编辑配置(可选,替换为内网或可信 NTP 源)vi /etc/chrony.conf推荐的 NTP 服务器配置示例:
# 国内常用公共 NTP 服务器server ntp.aliyun.com iburstserver ntp.tencent.com iburstserver cn.ntp.org.cn iburst启动并启用服务:
systemctl enable --now chronyd
# 验证同步状态chronyc trackingchronyc sources -v第 4 步:全量系统更新
Section titled “第 4 步:全量系统更新”上线前务必将系统更新到最新:
dnf update -y
# 查看是否需要重启(内核更新后)needs-restarting -r如果提示需要重启,可以在所有基线配置完成后统一重启。
第 5 步:创建管理员用户
Section titled “第 5 步:创建管理员用户”禁止在生产环境中直接使用 root 登录。 创建专用管理员账户:
# 创建用户useradd -m -s /bin/bash admin
# 设置密码passwd admin
# 加入 wheel 组以获得 sudo 权限usermod -aG wheel admin验证 sudo 配置:
# 确认 wheel 组有 sudo 权限grep '%wheel' /etc/sudoers# 应输出:%wheel ALL=(ALL) ALL第 6 步:SSH 加固
Section titled “第 6 步:SSH 加固”SSH 是服务器最主要的攻击面之一,必须加固。
6.1 部署密钥认证
Section titled “6.1 部署密钥认证”在本地客户端生成密钥对(如果还没有):
ssh-keygen -t ed25519 -C "admin@web-prod-01"将公钥复制到服务器:
ssh-copy-id admin@服务器IP6.2 修改 SSH 配置
Section titled “6.2 修改 SSH 配置”编辑 /etc/ssh/sshd_config.d/99-hardening.conf(EL 9 推荐使用 drop-in 配置):
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 adminEOF重启 SSH 服务:
systemctl restart sshd第 7 步:防火墙基线
Section titled “第 7 步:防火墙基线”EL 9 默认使用 firewalld:
# 确认 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=cockpitfirewall-cmd --permanent --zone=public --remove-service=dhcpv6-client
# 重新加载firewall-cmd --reload
# 确认最终规则firewall-cmd --list-all根据业务需要,后续按需开放端口:
# 示例:开放 HTTP/HTTPSfirewall-cmd --permanent --add-service=httpfirewall-cmd --permanent --add-service=httpsfirewall-cmd --reload第 8 步:验证 SELinux 状态
Section titled “第 8 步:验证 SELinux 状态”SELinux 是 EL 系统重要的安全层,不要关闭它:
# 检查状态getenforce# 应输出:Enforcing
sestatus如果状态为 Disabled 或 Permissive,需要启用:
# 编辑配置sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
# 重启后生效(如果从 Disabled 切换,需要重新标记文件系统)touch /.autorelabel安装 SELinux 管理工具:
dnf install -y policycoreutils-python-utils setools-console第 9 步:安装基础工具
Section titled “第 9 步:安装基础工具”安装运维常用工具:
dnf install -y \ vim \ tmux \ htop \ iotop \ lsof \ strace \ tcpdump \ net-tools \ bind-utils \ wget \ curl \ tar \ unzip \ bash-completion \ man-pages \ yum-utils可选的高级诊断工具:
dnf install -y \ sysstat \ perf \ bpftool \ nmap-ncat第 10 步:配置监控 Agent
Section titled “第 10 步:配置监控 Agent”以 node_exporter(Prometheus 生态)为例:
# 创建系统用户useradd --no-create-home --shell /sbin/nologin node_exporter
# 下载并安装(请替换为最新版本号)cd /tmpcurl -LO https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gztar xzf node_exporter-1.8.2.linux-amd64.tar.gzcp 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 ExporterAfter=network-online.targetWants=network-online.target
[Service]Type=simpleUser=node_exporterExecStart=/usr/local/bin/node_exporterRestart=on-failureRestartSec=5
[Install]WantedBy=multi-user.targetEOF
systemctl daemon-reloadsystemctl enable --now node_exporter开放监控端口(仅对监控服务器):
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第 11 步:配置备份 Agent
Section titled “第 11 步:配置备份 Agent”以 borgbackup 为例配置基础备份:
dnf install -y epel-releasednf install -y borgbackup
# 初始化备份仓库(本地或远程)borg init --encryption=repokey /backup/borg-repo
# 创建首次备份脚本cat > /usr/local/bin/backup.sh << 'SCRIPT'#!/bin/bashREPO="/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配置定时执行:
# 每天凌晨 2 点执行备份cat > /etc/cron.d/backup << 'EOF'0 2 * * * root /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1EOF第 12 步:配置日志轮转
Section titled “第 12 步:配置日志轮转”确认 logrotate 已安装并正常运行:
# 通常已预装dnf install -y logrotate
# 查看默认配置cat /etc/logrotate.conf为自定义应用日志添加轮转规则:
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手动测试轮转:
logrotate -d /etc/logrotate.d/app-logs第 13 步:重启验证
Section titled “第 13 步:重启验证”所有配置完成后,执行一次完整的重启测试:
# 重启前记录当前状态uptimesystemctl list-units --failed
# 重启reboot重启后逐项检查:
# 1. 主机名hostname
# 2. 时区和时间同步timedatectlchronyc tracking
# 3. SELinuxgetenforce
# 4. 防火墙firewall-cmd --list-all
# 5. SSH 服务systemctl status sshd
# 6. 监控 Agentsystemctl status node_exporter
# 7. 无失败服务systemctl list-units --failed
# 8. 磁盘和内存df -hfree -h基线清单速查表
Section titled “基线清单速查表”| 序号 | 检查项 | 命令/验证方法 | 预期结果 |
|---|---|---|---|
| 1 | 主机名 | hostname | 有意义的名称 |
| 2 | 时区 | timedatectl | 正确时区 |
| 3 | NTP | chronyc tracking | 同步正常 |
| 4 | 系统更新 | dnf check-update | 无可用更新 |
| 5 | 管理员用户 | id admin | 存在且属于 wheel 组 |
| 6 | SSH 加固 | sshd -T | grep permitrootlogin | no |
| 7 | 防火墙 | firewall-cmd --list-all | 仅开放必要端口 |
| 8 | SELinux | getenforce | Enforcing |
| 9 | 基础工具 | which vim htop tmux | 均已安装 |
| 10 | 监控 | systemctl status node_exporter | active (running) |
| 11 | 备份 | ls /backup/borg-repo | 仓库已初始化 |
| 12 | 日志轮转 | logrotate -d /etc/logrotate.conf | 无错误 |
| 13 | 重启测试 | systemctl list-units --failed | 0 failed |
基线配置完成后,根据服务器角色继续进行:
- Web 服务器 - 参考 Nginx 应用部署实战
- 数据库服务器 - 参考 PostgreSQL 或 Redis 指南
- 备份验证 - 参考 备份与恢复演练
- SSL 证书 - 参考 SSL 证书管理