適用於 **CentOS/RHEL 6/7/8** 全新安裝後的基線安全與效能調校,包含網路、安全、編輯器、語系、服務、核心參數六大面向。生產環境請先審閱並依實際服務調整服務清單。
#!/bin/bash
# desc: Linux Server Baseline Hardening & Tuning
# author: NetSeek (modified by webnginx.com)
# usage: chmod +x init_server.sh && ./init_server.sh
set -euo pipefail
echo "+--------------------------------------------------------------+"
echo "| === Welcome to CentOS System Initialization === |"
echo "+--------------------------------------------------------------+"
# 1. 關閉 IPv6
echo "=== Disabling IPv6 ==="
cat >> /etc/modprobe.d/ipv6.conf <<'EOF'
alias net-pf-10 off
alias ipv6 off
EOF
systemctl disable --now ip6tables 2>/dev/null || chkconfig --level 35 ip6tables off
echo "IPv6 disabled (requires reboot)."
# 2. 關閉 SELinux
echo "=== Disabling SELinux ==="
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
echo "SELinux disabled (requires reboot)."
# 3. Vim 語法高亮與別名
echo "=== Configuring Vim ==="
grep -q "alias vi='vim'" /root/.bashrc || echo "alias vi='vim'" >> /root/.bashrc
echo 'syntax on' > /root/.vimrc
echo 'set number' >> /root/.vimrc
echo 'set tabstop=4 shiftwidth=4 expandtab' >> /root/.vimrc
# 4. 設定系統語系為 zh_CN.UTF-8
echo "=== Setting Locale ==="
sed -i 's/^LANG=.*/LANG="zh_CN.UTF-8"/' /etc/locale.conf 2>/dev/null || \
sed -i 's/^LANG=.*/LANG="zh_CN.UTF-8"/' /etc/sysconfig/i18n
# 5. 檔案描述符限制
echo "=== Raising File Descriptor Limits ==="
cat >> /etc/security/limits.conf <<'EOF'
* soft nofile 52100
* hard nofile 52100
root soft nofile 52100
root hard nofile 52100
EOF
# 6. 精簡系統服務 (根據實際需求調整保留清單)
echo "=== Optimizing Services ==="
KEEP_SERVICES=(
crond irqbalance microcode_ctl
network sshd rsyslog
# 依環境新增:nginx php-fpm mysqld docker 等
)
for svc in $(systemctl list-unit-files --type=service --state=enabled --no-legend | awk '{print $1}'); do
if [[ ! " ${KEEP_SERVICES[@]} " =~ " ${svc%.service} " ]]; then
echo "Disabling $svc"
systemctl disable --now "$svc" 2>/dev/null || chkconfig "$svc" off
else
echo "Keeping $svc"
fi
done
# 7. 核心參數優化 (sysctl)
echo "=== Tuning Kernel Parameters ==="
cat > /etc/sysctl.d/99-tuning.conf <<'EOF'
# 網路
net.ipv4.ip_forward = 0
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 2000000
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 30000
net.core.somaxconn = 65535
net.ipv4.ip_local_port_range = 1024 65535
# 記憶體/交換
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
# 安全
kernel.sysrq = 0
kernel.core_uses_pid = 1
kernel.kptr_restrict = 2
EOF
sysctl --system
echo "Kernel parameters applied."
echo "+--------------------------------------------------------------+"
echo "| Initialization completed. Reboot recommended. |"
echo "+--------------------------------------------------------------+"
這支腳本涵蓋 Linux 伺服器上線前的**安全基線**與**效能基線**,可作為標準化交付模板。持續根據業務負載特徵微調核心參數與服務清單,打造穩定、安全、高效的生產環境。