适用发行版:CentOS 7/8、Rocky Linux、Alibaba Cloud Linux 等 RHEL 系
目标:安全加固 + 性能调优 + 可观测性(日志/时间)
风险提示:部分操作(如关闭 Swap、修改 SSH 配置)可能影响现有业务或导致无法登录,请仔细阅读注释并在测试环境验证。
1. 为什么要做系统初始化?
生产环境操作系统初始状态普遍存在:
- 安全漏洞:SELinux 策略复杂、防火墙规则冲突、默认 SSH 端口暴露
- 性能瓶颈:Swap 导致内存抖动、内核参数未调优、I/O 调度器不当
- 故障难排查:日志仅存于内存、时间不同步、ulimit 过小
本文提供 基础加固 → 环境依赖 → 内核深度调优 → 日志管理 → 时间同步 → 磁盘/内存专项优化 全流程,每个参数均有中文释义与推荐值。
2. 操作系统基础安全加固
2.1 关闭 Swap 交换分区
原因:Swap 引入磁盘 I/O 竞争,在 Kubernetes 等容器环境中会干扰资源统计。生产环境强烈建议禁用 Swap。
# 立即关闭所有 swap 分区
swapoff -a
# 永久禁用:注释 /etc/fstab 中所有包含 swap 的行(使用更宽松的正则匹配空格/tab)
sed -i '/\sswap\s/ s/^/#/' /etc/fstab2.2 关闭 SELinux
原因:SELinux 提供强制访问控制,但配置复杂易干扰应用。云原生场景通常关闭。
# 当前关闭
setenforce 0
# 永久禁用
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config2.3 关闭 firewalld 并重置 iptables
原因:云环境已由安全组防护;冗余的防火墙规则可能干扰容器网络(如 Calico)。
systemctl stop firewalld && systemctl disable firewalld
yum -y install iptables-services
systemctl start iptables && systemctl enable iptables
iptables -F
service iptables save2.4 关闭非必要服务(示例:postfix)
systemctl stop postfix && systemctl disable postfix3. 基础环境与依赖包安装
3.1 更新系统并安装常用工具
yum makecache && yum -y update
# 注意:包名 yum-utils(不带 .noarch 后缀)
yum -y install conntrack ipvsadm ipset jq iptables curl sysstat libseccomp \
wget vim-enhanced lrzsz net-tools git psmisc bash-completion yum-utils \
nmap bind-utils chrony工具说明:conntrack(连接跟踪)、ipvsadm(kube-proxy IPVS 模式)、chrony(高精度时间同步)
3.2 配置时区与硬件时钟
timedatectl set-timezone Asia/Shanghai
timedatectl set-local-rtc 0 # 硬件时钟使用 UTC
systemctl restart rsyslog crond 2>/dev/null || true # 若服务不存在则不报错3.3 配置 Chrony 时间同步(使用阿里云 NTP)
cp /etc/chrony.conf /etc/chrony.conf.bak
sed -i 's/^pool/#pool/' /etc/chrony.conf
cat >> /etc/chrony.conf <<EOF
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
allow 127.0.0.1
EOF
systemctl enable --now chronyd
chronyc sources -v4. 系统日志持久化与限流
默认 systemd-journald 将日志存储在内存中(/run/log/journal),重启会丢失。生产环境必须持久化。
mkdir -p /var/log/journal /etc/systemd/journald.conf.d创建配置文件 /etc/systemd/journald.conf.d/99-production.conf:
⚠️ 重要:systemd-journald 配置文件不支持行内注释(即 Key=value # comment 会导致解析失败)。注释必须单独成行。cat > /etc/systemd/journald.conf.d/99-production.conf <<EOF
# 持久化到磁盘
Storage=persistent
# 压缩历史日志
Compress=yes
# 写入硬盘的间隔时间
SyncIntervalSec=5m
# 限流周期:30秒
RateLimitInterval=30s
# 周期内每个服务最多产生1000条日志
RateLimitBurst=1000
# 日志最大占用空间10GB
SystemMaxUse=10G
# 单个日志文件最大200MB
SystemMaxFileSize=200M
# 最长保留2周(单位可用 weeks/d)
MaxRetentionSec=2weeks
# 不转发到 syslog,避免重复
ForwardToSyslog=no
EOF
systemctl restart systemd-journald5. 内核参数深度调优
⚠️ 高风险提示:内核参数修改后立即生效,若配置错误可能导致网络中断或系统不稳定。建议逐项理解并在测试环境验证。
5.1 加载必要内核模块(必须在设置参数前执行)
modprobe bridge
modprobe nf_conntrack5.2 创建完整的内核配置文件
cat > /etc/sysctl.d/99-production.conf <<EOF
# ========== 网络桥接与转发(容器必备) ==========
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
# ========== TCP 连接与拥塞控制 ==========
net.ipv4.tcp_max_syn_backlog = 16384
net.core.somaxconn = 32768
net.ipv4.tcp_tw_reuse = 1
# 注意:tcp_tw_recycle 已在 Linux 4.12+ 移除,此处不再设置
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
# ========== 内存管理 ==========
vm.swappiness = 0
vm.overcommit_memory = 1
vm.panic_on_oom = 0
vm.max_map_count = 262144
# ========== 文件系统与 inotify ==========
fs.file-max = 52706963
fs.nr_open = 52706963
fs.inotify.max_user_instances = 8192
fs.inotify.max_user_watches = 1048576
# ========== IPv6(可选关闭) ==========
net.ipv6.conf.all.disable_ipv6 = 1
# ========== 连接跟踪(Conntrack) ==========
net.netfilter.nf_conntrack_max = 2310720
net.netfilter.nf_conntrack_tcp_timeout_established = 86400
# ========== 高吞吐调优 ==========
kernel.pid_max = 4194303
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
kernel.panic = 3
# ========== 容器/K8s 网络补充 ==========
net.ipv4.conf.all.rp_filter = 0
net.ipv4.neigh.default.gc_thresh1 = 2048
net.ipv4.neigh.default.gc_thresh2 = 4096
net.ipv4.neigh.default.gc_thresh3 = 8192
EOF
sysctl -p /etc/sysctl.d/99-production.conf6. 用户资源限制(ulimit)
原因:即使内核 fs.file-max 很大,单个进程默认 nofile 仍为 1024,会导致 “too many open files”。
cat >> /etc/security/limits.conf <<EOF
* soft nofile 1048576
* hard nofile 1048576
* soft nproc 65536
* hard nproc 65536
* soft core unlimited
* hard core unlimited
EOF注意:nofile的 hard 限制不能超过内核参数fs.nr_open(此处 1048576 < 52706963,安全)。重新登录生效。
7. 磁盘 I/O 调度器优化
原因:SSD 使用 none(原 noop),HDD 使用 mq-deadline 可降低延迟。
cat > /etc/udev/rules.d/60-iosched.rules <<EOF
# HDD (旋转盘)
ACTION=="add|change", KERNEL=="sd*[!0-9]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="mq-deadline"
# SSD (非旋转盘)
ACTION=="add|change", KERNEL=="sd*[!0-9]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="none"
# NVMe 设备
ACTION=="add|change", KERNEL=="nvme*[!0-9]", ATTR{queue/scheduler}="none"
EOF
# 对已有设备立即生效(示例)
echo mq-deadline > /sys/block/sda/queue/scheduler 2>/dev/null || true
echo none > /sys/block/nvme0n1/queue/scheduler 2>/dev/null || true8. 透明大页与 CPU 频率调控
8.1 禁用透明大页(THP)
原因:THP 会导致内存分配延迟,引起数据库、JVM 应用性能抖动。生产环境务必禁用。
# 临时禁用
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
# 永久禁用(确保 rc-local 服务已启用)
cat >> /etc/rc.local <<EOF
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
EOF
chmod +x /etc/rc.local
systemctl enable rc-local
systemctl start rc-local8.2 CPU 频率调控器设为 performance
原因:ondemand 模式会动态降频增加延迟。对延迟敏感的服务应固定为最高频率。
# 仅在物理机或支持频率调节的环境执行(虚拟机/云环境可能不支持)
if [ -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; then
yum install -y kernel-tools
cpupower frequency-set -g performance
cat > /etc/systemd/system/cpupower.service <<EOF
[Unit]
Description=CPU performance governor
After=sysinit.target
[Service]
Type=oneshot
ExecStart=/usr/bin/cpupower frequency-set -g performance
[Install]
WantedBy=multi-user.target
EOF
systemctl enable cpupower.service
else
echo "CPU frequency scaling not supported (cloud/vm), skipping."
fi9. SSH 安全加固(生产必备,⚠️ 高风险操作)
⚠️ 严重警告:以下修改可能导致您无法登录服务器!
- 修改端口前,请确保防火墙/安全组已放通新端口
- 禁止 root 登录前,请确保已创建普通用户并配置 sudo 权限和 SSH 公钥
- 务必保持一个已连接的 root 会话,用于调试
# 备份原配置
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
# 禁止 root 登录(需要提前有普通用户)
sed -i 's/^#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
# 禁止密码登录(必须使用密钥)
sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
# 修改 SSH 端口(示例改为 2222)
sed -i 's/^#Port 22/Port 2222/' /etc/ssh/sshd_config
# 重启 sshd(小心:重启不会踢出现有连接)
systemctl restart sshd安全提示:建议先单独执行 systemctl restart sshd 测试新配置,再关闭当前会话。10. 一键初始化脚本(汇总,带安全提示)
以下脚本整合了上述所有步骤,并加入了容错处理、高风险操作确认提示,且日志配置文件中已使用单独行注释。
tee>production_init.sh<<EOF
#!/bin/bash
# production_init.sh - Linux 服务器生产环境初始化脚本(交互式 + 状态检测)
# 适用发行版:CentOS 7+ / RHEL 7+ / Rocky Linux / Alibaba Cloud Linux
# 使用方法:chmod +x production_init.sh && ./production_init.sh
set -e
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# 询问是否执行某一步
ask_step() {
local step_name="$1"
local answer
while true; do
echo -e "${YELLOW}>>> 是否执行「${step_name}」?[y/N]${NC} "
read -r answer
case "$answer" in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
"" ) return 1;;
* ) echo "请输入 y 或 n";;
esac
done
}
echo -e "${YELLOW}===== 生产环境初始化脚本(交互式 + 状态检测)=====${NC}"
echo -e "${RED}警告:部分步骤将修改系统配置(关闭 Swap、SELinux、调整内核参数等)。"
echo -e "请确保已在测试环境验证,并备份重要数据。${NC}"
read -p "是否继续?(y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
# ------------------------------------------------------------
# 步骤1:关闭 Swap
# ------------------------------------------------------------
if ask_step "关闭 Swap"; then
echo "=== 1. 关闭 Swap ==="
# 检查是否已关闭
if swapon --show | grep -q "^"; then
swapoff -a && sed -i '/\sswap\s/ s/^/#/' /etc/fstab
echo "Swap 已关闭。"
else
echo "Swap 已经关闭,跳过。"
fi
else
echo "跳过步骤1"
fi
# ------------------------------------------------------------
# 步骤2:关闭 SELinux
# ------------------------------------------------------------
if ask_step "关闭 SELinux"; then
echo "=== 2. 关闭 SELinux ==="
current=$(getenforce)
if [[ "$current" != "Disabled" ]]; then
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
echo "SELinux 已关闭。"
else
echo "SELinux 已经是 disabled 状态,跳过。"
fi
else
echo "跳过步骤2"
fi
# ------------------------------------------------------------
# 步骤3:关闭 firewalld 并重置 iptables
# ------------------------------------------------------------
if ask_step "关闭 firewalld 并重置 iptables"; then
echo "=== 3. 关闭 firewalld 并重置 iptables ==="
# 检查 firewalld 状态
if systemctl is-enabled firewalld 2>/dev/null | grep -q "enabled"; then
systemctl stop firewalld && systemctl disable firewalld
echo "firewalld 已停止并禁用。"
else
echo "firewalld 已经禁用,跳过。"
fi
# 安装 iptables-services(幂等)
if ! rpm -q iptables-services &>/dev/null; then
yum -y install iptables-services
fi
# 检查 iptables 是否已启用且规则为空(简单判断:INPUT 链只有默认策略)
if systemctl is-enabled iptables 2>/dev/null | grep -q "enabled" && \
iptables -L INPUT -n | grep -q "Chain INPUT (policy ACCEPT)"; then
echo "iptables 已启用且规则为空,跳过重置。"
else
systemctl start iptables && systemctl enable iptables
iptables -F && service iptables save
echo "iptables 已重置并保存。"
fi
else
echo "跳过步骤3"
fi
# ------------------------------------------------------------
# 步骤4:安装基础软件包与时间同步
# ------------------------------------------------------------
if ask_step "安装基础软件包与时间同步"; then
echo "=== 4. 安装基础软件包与时间同步 ==="
# 检查关键包是否已安装(例如 chrony)
if ! rpm -q chrony &>/dev/null; then
yum makecache && yum -y update
yum -y install conntrack ipvsadm ipset jq iptables curl sysstat libseccomp \
wget vim-enhanced lrzsz net-tools git psmisc bash-completion yum-utils \
nmap bind-utils chrony
else
echo "基础软件包似乎已安装(至少 chrony 存在),跳过安装。"
fi
# 时区检查
current_tz=$(timedatectl show --property=Timezone --value)
if [[ "$current_tz" != "Asia/Shanghai" ]]; then
timedatectl set-timezone Asia/Shanghai
timedatectl set-local-rtc 0
echo "时区已设为 Asia/Shanghai。"
else
echo "时区已是 Asia/Shanghai,跳过。"
fi
# 重启 rsyslog/crond(忽略错误)
systemctl restart rsyslog crond 2>/dev/null || true
# chrony 配置检查
if ! grep -q "ntp.aliyun.com" /etc/chrony.conf; then
cp /etc/chrony.conf /etc/chrony.conf.bak
sed -i 's/^pool/#pool/' /etc/chrony.conf
cat >> /etc/chrony.conf <<EOF
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
allow 127.0.0.1
EOF
systemctl enable --now chronyd
echo "chrony 已配置并启动。"
else
echo "chrony 已配置阿里云 NTP,跳过。"
fi
else
echo "跳过步骤4"
fi
# ------------------------------------------------------------
# 步骤5:持久化日志
# ------------------------------------------------------------
if ask_step "持久化日志(systemd-journald)"; then
echo "=== 5. 持久化日志 ==="
if [[ ! -f /etc/systemd/journald.conf.d/99-production.conf ]]; then
mkdir -p /var/log/journal /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/99-production.conf <<EOF
# 持久化到磁盘
Storage=persistent
# 压缩历史日志
Compress=yes
# 写入硬盘间隔
SyncIntervalSec=5m
# 限流周期30秒
RateLimitInterval=30s
# 周期内最大日志1000条
RateLimitBurst=1000
# 日志最大占用10GB
SystemMaxUse=10G
# 单个日志文件200MB
SystemMaxFileSize=200M
# 最长保留2周
MaxRetentionSec=2weeks
# 不转发到syslog
ForwardToSyslog=no
EOF
systemctl restart systemd-journald
echo "journald 持久化配置已应用。"
else
echo "journald 持久化配置文件已存在,跳过。"
fi
else
echo "跳过步骤5"
fi
# ------------------------------------------------------------
# 步骤6:加载内核模块
# ------------------------------------------------------------
if ask_step "加载内核模块(bridge, nf_conntrack)"; then
echo "=== 6. 加载内核模块 ==="
if ! lsmod | grep -q "^bridge"; then
modprobe bridge
echo "bridge 模块已加载。"
else
echo "bridge 模块已加载,跳过。"
fi
if ! lsmod | grep -q "^nf_conntrack"; then
modprobe nf_conntrack
echo "nf_conntrack 模块已加载。"
else
echo "nf_conntrack 模块已加载,跳过。"
fi
else
echo "跳过步骤6"
fi
# ------------------------------------------------------------
# 步骤7:内核参数调优
# ------------------------------------------------------------
if ask_step "内核参数调优(写入 /etc/sysctl.d/99-production.conf)"; then
echo "=== 7. 内核参数调优 ==="
# 检查配置文件是否存在且包含关键参数(以 vm.swappiness=0 为例)
if [[ -f /etc/sysctl.d/99-production.conf ]] && grep -q "vm.swappiness=0" /etc/sysctl.d/99-production.conf; then
echo "内核参数配置文件已存在且包含关键设置,跳过。"
else
cat > /etc/sysctl.d/99-production.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_max_syn_backlog=16384
net.core.somaxconn=32768
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_keepalive_time=600
net.ipv4.tcp_keepalive_intvl=30
net.ipv4.tcp_keepalive_probes=3
vm.swappiness=0
vm.overcommit_memory=1
vm.panic_on_oom=0
vm.max_map_count=262144
fs.file-max=52706963
fs.nr_open=52706963
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
net.netfilter.nf_conntrack_tcp_timeout_established=86400
kernel.pid_max=4194303
net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.ipv4.tcp_rmem=4096 87380 16777216
net.ipv4.tcp_wmem=4096 65536 16777216
kernel.panic=3
net.ipv4.conf.all.rp_filter=0
net.ipv4.neigh.default.gc_thresh1=2048
net.ipv4.neigh.default.gc_thresh2=4096
net.ipv4.neigh.default.gc_thresh3=8192
EOF
sysctl -p /etc/sysctl.d/99-production.conf
echo "内核参数已应用。"
fi
else
echo "跳过步骤7"
fi
# ------------------------------------------------------------
# 步骤8:设置 ulimit
# ------------------------------------------------------------
if ask_step "设置 ulimit(/etc/security/limits.conf)"; then
echo "=== 8. 设置 ulimit ==="
if grep -q "soft nofile 1048576" /etc/security/limits.conf; then
echo "ulimit 配置已存在,跳过。"
else
cat >> /etc/security/limits.conf <<EOF
* soft nofile 1048576
* hard nofile 1048576
* soft nproc 65536
* hard nproc 65536
* soft core unlimited
* hard core unlimited
EOF
echo "ulimit 配置已添加。"
fi
else
echo "跳过步骤8"
fi
# ------------------------------------------------------------
# 步骤9:磁盘 I/O 调度器
# ------------------------------------------------------------
if ask_step "设置磁盘 I/O 调度器(udev 规则)"; then
echo "=== 9. 磁盘 I/O 调度器 ==="
if [[ -f /etc/udev/rules.d/60-iosched.rules ]]; then
echo "udev 调度器规则文件已存在,跳过。"
else
cat > /etc/udev/rules.d/60-iosched.rules <<EOF
# HDD
ACTION=="add|change", KERNEL=="sd*[!0-9]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="mq-deadline"
# SSD
ACTION=="add|change", KERNEL=="sd*[!0-9]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="none"
# NVMe
ACTION=="add|change", KERNEL=="nvme*[!0-9]", ATTR{queue/scheduler}="none"
EOF
echo "udev 规则已添加。"
fi
else
echo "跳过步骤9"
fi
# ------------------------------------------------------------
# 步骤10:禁用透明大页
# ------------------------------------------------------------
if ask_step "禁用透明大页(Transparent Huge Pages)"; then
echo "=== 10. 禁用透明大页 ==="
thp_enabled=$(cat /sys/kernel/mm/transparent_hugepage/enabled 2>/dev/null || echo "never")
if [[ "$thp_enabled" == *"never"* ]]; then
echo "透明大页已禁用,跳过。"
else
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
cat >> /etc/rc.local <<EOF
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then echo never > /sys/kernel/mm/transparent_hugepage/enabled; fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then echo never > /sys/kernel/mm/transparent_hugepage/defrag; fi
EOF
chmod +x /etc/rc.local
systemctl enable rc-local
systemctl start rc-local
echo "透明大页已禁用,并写入 rc.local。"
fi
else
echo "跳过步骤10"
fi
# ------------------------------------------------------------
# 步骤11:CPU 性能模式
# ------------------------------------------------------------
if ask_step "CPU 性能模式(设置为 performance)"; then
echo "=== 11. CPU 性能模式 ==="
if [ -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; then
current_gov=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
if [[ "$current_gov" == "performance" ]]; then
echo "CPU 已经是 performance 模式,跳过。"
else
yum install -y kernel-tools
cpupower frequency-set -g performance
cat > /etc/systemd/system/cpupower.service <<EOF
[Unit]
Description=CPU performance governor
After=sysinit.target
[Service]
Type=oneshot
ExecStart=/usr/bin/cpupower frequency-set -g performance
[Install]
WantedBy=multi-user.target
EOF
systemctl enable cpupower.service
echo "CPU performance 模式已启用。"
fi
else
echo "CPU frequency scaling not supported, skipping."
fi
else
echo "跳过步骤11"
fi
# ------------------------------------------------------------
# 步骤12:SSH 加固提醒(仅提醒,不自动执行)
# ------------------------------------------------------------
if ask_step "显示 SSH 加固建议(仅提醒)"; then
echo "=== 12. SSH 加固(需手动执行) ==="
echo -e "${YELLOW}建议手动配置 /etc/ssh/sshd_config:${NC}"
echo " PermitRootLogin no"
echo " PasswordAuthentication no"
echo " Port 2222"
echo -e "${RED}注意:修改前请确保安全组/防火墙已放通新端口,并保留一个 root 会话!${NC}"
else
echo "跳过步骤12"
fi
echo -e "${GREEN}所有选中的初始化步骤执行完毕!建议重启服务器验证。${NC}"
EOF11. 验证与测试
| 检查项 | 命令 | 预期结果 |
|---|---|---|
| Swap 是否关闭 | free -h | swap 行均为 0 |
| SELinux 状态 | getenforce | Disabled |
| 时区与时间同步 | timedatectl,chronyc sources | 时区为 Asia/Shanghai,有 ^* 同步源 |
| 内核参数 | sysctl net.ipv4.tcp_tw_reuse | 值为 1 |
| ulimit | ulimit -n | 1048576 |
| 透明大页 | cat /sys/kernel/mm/transparent_hugepage/enabled | 显示 never |
| CPU 调控器 | cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor | performance(若支持) |
| 日志持久化 | ls /var/log/journal/ | 有内容 |
| journald 配置无报错 | journalctl --verify | 无解析错误 |
12. 最佳实践与注意事项
- 分批次应用:已有业务服务器请逐项调整,不要直接运行全量脚本。
- 备份配置:修改
/etc/ssh/sshd_config前务必保留一个 root 会话窗口。 - 云环境差异:部分云平台已优化 I/O 调度和 CPU 频率,可跳过物理机级调优。
- 定期复盘:内核参数每半年根据业务负载重新评估。
- 使用配置管理工具:建议将上述配置写成 Ansible role 实现标准化批量部署。
- journald 配置文件格式:切记不要使用行内注释,否则服务启动会失败。