服务器系统配置初始化脚本
#!/bin/bash
set -euo pipefail # 安全设置:遇错退出、未定义变量报错、管道错误处理# 设置时区并同步时间
timedatectl set-timezone Asia/Shanghai >/dev/null || ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime# 安装chrony并配置时间同步
if ! command -v chronyc &>/dev/null; thenyum install -y chrony >/dev/null
fi
systemctl enable chronyd --now >/dev/null# 禁用selinux
sed -i '/SELINUX/{s/enforcing\|permissive/disabled/}' /etc/selinux/config
setenforce 0 &>/dev/null# 关闭防火墙(兼容6.x/7.x/CentOS Stream)
if grep -E '7\.|8\.|9\.|Stream' /etc/redhat-release &>/dev/null; thensystemctl disable --now firewalld &>/dev/null
elif grep '6\.' /etc/redhat-release &>/dev/null; thenservice iptables stop &>/dev/nullchkconfig iptables off &>/dev/null
fi# 历史命令显示操作时间
grep -q HISTTIMEFORMAT /etc/bashrc || echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/bashrc# SSH超时和配置优化
grep -q TMOUT /etc/profile || echo "export TMOUT=600" >> /etc/profilesed -i '
s/#PermitRootLogin.*/PermitRootLogin no/
s/#ClientAliveInterval.*/ClientAliveInterval 300/
s/#ClientAliveCountMax.*/ClientAliveCountMax 0/
' /etc/ssh/sshd_config# 禁止定时任务邮件通知
sed -i 's/^MAILTO=.*/MAILTO=""/' /etc/crontab# 设置最大打开文件数
if ! grep -q "nofile 65535" /etc/security/limits.conf; thencat <<EOF >> /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
EOF
fi# 系统内核优化
grep -q tcp_max_syn_backlog /etc/sysctl.conf || cat <<EOF >> /etc/sysctl.conf
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 20
net.ipv4.tcp_max_tw_buckets = 20480
net.core.somaxconn = 20480
net.ipv4.tcp_max_syn_backlog = 20480
net.core.netdev_max_backlog = 262144
vm.swappiness = 5
EOF
sysctl -p &>/dev/null# 安装基础工具
yum install -y epel-release >/dev/null
yum install -y gcc make autoconf vim sysstat net-tools bind-utils >/dev/null# 应用SSH配置(避免立即断开连接)
echo "配置完成!请手动执行以下命令:"
echo "systemctl restart sshd && source /etc/profile"
echo "注意部分设置需要重启服务器才能完全生效"
关键优化点及解释:
-
安全增强:
set -euo pipefail
-e
:命令失败时立即退出-u
:未定义变量时报错-o pipefail
:管道中任意命令失败则整个管道失败
-
时间同步优化:
timedatectl set-timezone Asia/Shanghai systemctl enable chronyd --now
- 使用
chronyd
替代ntpdate
(更精确的NTP实现) timedatectl
是官方推荐的时区设置工具- 系统自动维护时间同步,无需crontab
- 使用
-
SELinux禁用改进:
sed -i '/SELINUX/{s/enforcing\|permissive/disabled/}' /etc/selinux/config setenforce 0
- 同时处理
enforcing
和permissive
状态 setenforce 0
立即生效无需重启
- 同时处理
-
防火墙处理增强:
if grep -E '7\.|8\.|9\.|Stream' /etc/redhat-release; then
- 支持CentOS 7/8/9和CentOS Stream
- 使用
disable --now
同时停止并禁用服务
-
SSH安全加固:
s/#ClientAliveInterval.*/ClientAliveInterval 300/ s/#ClientAliveCountMax.*/ClientAliveCountMax 0/
- 添加会话超时自动断开(300秒无操作断开)
- 禁止root登录和会话保持
-
内核参数优化:
net.ipv4.tcp_tw_reuse = 1 vm.swappiness = 5 sysctl -p
- 添加端口复用减少TIME_WAIT
- 持久化swap优化(原脚本
/proc
设置重启失效) sysctl -p
立即应用配置
-
错误处理改进:
command -v chronyc &>/dev/null || yum install -y chrony grep -q ... || echo ...
- 所有检查操作均先验证是否必要
- 静默安装和配置(
&>/dev/null
)
-
用户友好提示:
echo "配置完成!请手动执行以下命令:" echo "systemctl restart sshd && source /etc/profile"
- 避免直接重启sshd导致当前连接断开
- 提示需要重启的应用
使用建议:
- 保存为
init_server.sh
- 添加执行权限:
chmod +x init_server.sh
- 测试执行:
./init_server.sh
- 重新登录SSH(应用新SSH配置)
- 关键敏感操作(如防火墙关闭)应根据实际环境调整
⚠️ 注意:部分设置(如SELinux禁用、文件描述符限制)需重启服务器才能完全生效。生产环境中建议根据安全需求调整防火墙规则,而不是完全关闭防火墙。