前言
目的:说明写这个初始化脚本的动机,比如统一服务器环境、提升运维效率等。
适用对象:运维工程师、开发人员、系统管理员等。
环境说明:Linux发行版(CentOS、Rocky、OpenEuler等)、权限要求(root或sudo)。
Rocky系统
#!/bin/bash
echo "=====系统环境初始化脚本====="
sleep 3
echo "——>>> 关闭防火墙与SELinux <<<——"
sleep 3
systemctl stop firewalld
systemctl disable firewalld &> /dev/null
setenforce 0
sed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config
echo "——>>> 创建阿里仓库 <<<——"
sleep 3
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
-i.bak \
/etc/yum.repos.d/rocky*.repo
dnf makecache
echo "——>>> 设置时区并同步时间 <<<——"
sleep 3
timedatectl set-timezone Asia/Shanghai
yum -y install chrony
systemctl start chronyd
systemctl enable chronyd
echo "——>>> 设置系统最大打开文件数 <<<——"
sleep 3
if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
cat >> /etc/security/limits.conf << EOF
* soft nofile 65535 #软限制
* hard nofile 65535 #硬限制
EOF
fi
echo "——>>> 系统内核优化 <<<——"
sleep 3
cat >> /etc/sysctl.conf << EOF
net.ipv4.tcp_syncookies = 1 #防范SYN洪水攻击,0为关闭
net.ipv4.tcp_max_tw_buckets = 20480 #此项参数可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死
net.ipv4.tcp_max_syn_backlog = 20480 #表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数
net.core.netdev_max_backlog = 262144 #每个网络接口 接受数据包的速率比内核处理这些包的速率快时,允许发送到队列的数据包的最大数目
net.ipv4.tcp_fin_timeout = 20 #FIN-WAIT-2状态的超时时间,避免内核崩溃
EOF
echo "——>>> 减少SWAP使用 <<<——"
sleep 3
echo "0" > /proc/sys/vm/swappiness
echo "——>>> 安装系统性能分析工具及其他 <<<——"
sleep 3
dnf install -y gcc make wget tar unzip vim net-tools lrzsz epel-releaseOpenEuler24.03系统
#!/bin/bash
echo "=====openEuler系统环境初始化脚本====="
sleep 3
# 关闭防火墙与SELinux
echo "——>>> 关闭防火墙与SELinux <<<——"
sleep 3
systemctl stop firewalld
systemctl disable firewalld &> /dev/null
setenforce 0
sed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config
# 配置正确的openEuler镜像源(根据你的系统版本选择)
echo "——>>> 配置openEuler仓库 <<<——"
sleep 3
# 检测系统版本并配置相应的源
if grep -q "24.03" /etc/os-release; then
echo "检测到 openEuler 24.03,配置对应镜像源"
cat > /etc/yum.repos.d/openEuler.repo << EOF
[openEuler-24.03]
name=openEuler-24.03
baseurl=https://repo.huaweicloud.com/openeuler/openEuler-24.03-LTS-SP2/everything/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/openeuler/openEuler-24.03-LTS-SP2/OS/x86_64/RPM-GPG-KEY-openEuler
EOF
else
echo "检测到 openEuler 22.03,配置对应镜像源"
cat > /etc/yum.repos.d/openEuler.repo << EOF
[openEuler-22.03]
name=openEuler-22.03
baseurl=https://repo.huaweicloud.com/openeuler/openEuler-22.03-LTS/everything/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/openeuler/openEuler-22.03-LTS/OS/x86_64/RPM-GPG-KEY-openEuler
EOF
fi
dnf clean all
dnf makecache
# 时区与时间同步设置
echo "——>>> 设置时区并同步时间 <<<——"
sleep 3
timedatectl set-timezone Asia/Shanghai
dnf -y install chrony
systemctl start chronyd
systemctl enable chronyd
# 系统资源限制配置
echo "——>>> 设置系统最大打开文件数 <<<——"
sleep 3
if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
cat >> /etc/security/limits.conf << EOF
* soft nofile 65535
* hard nofile 65535
EOF
fi
# 内核参数优化
echo "——>>> 系统内核优化 <<<——"
sleep 3
cat >> /etc/sysctl.conf << EOF
# 网络优化参数
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_tw_buckets = 20480
net.ipv4.tcp_max_syn_backlog = 20480
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_fin_timeout = 20
EOF
sysctl -p
# SWAP优化
echo "——>>> 减少SWAP使用 <<<——"
sleep 3
echo "vm.swappiness = 0" >> /etc/sysctl.conf
sysctl -w vm.swappiness=0
# 安装常用工具(使用兼容性更好的安装方式)
echo "——>>> 安装系统性能分析工具及其他 <<<——"
sleep 3
# 先安装基础工具
dnf install -y gcc make wget tar unzip vim net-tools lrzsz
# 然后安装性能工具,跳过有冲突的包
dnf install -y --skip-broken perf sysstat htop iotop iftop
# 或者使用更安全的方式逐个安装
dnf install -y sysstat htop iotop iftop || echo "某些性能工具安装失败,跳过"
echo "=====系统初始化完成====="OpenEuler22.03系统
#!/bin/bash
echo "=====openEuler系统环境初始化脚本====="
sleep 3
# 关闭防火墙与SELinux
echo "——>>> 关闭防火墙与SELinux <<<——"
sleep 3
systemctl stop firewalld
systemctl disable firewalld &> /dev/null # 禁用防火墙开机启动
setenforce 0 # 临时关闭SELinux
sed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config # 永久关闭SELinux
# 配置openEuler镜像源(使用华为源)
echo "——>>> 配置华为openEuler仓库 <<<——"
sleep 3
cat > /etc/yum.repos.d/openEuler.repo << EOF
[openEuler]
name=openEuler
baseurl=https://repo.huaweicloud.com/openeuler/openEuler-22.03-LTS/everything/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/openeuler/openEuler-22.03-LTS/OS/x86_64/RPM-GPG-KEY-openEuler
EOF
dnf clean all # 清理缓存
dnf makecache # 生成新缓存
# 时区与时间同步设置
echo "——>>> 设置时区并同步时间 <<<——"
sleep 3
timedatectl set-timezone Asia/Shanghai # 设置上海时区
dnf -y install chrony # 安装时间同步服务
systemctl start chronyd # 启动服务
systemctl enable chronyd # 设置开机启动
# 系统资源限制配置
echo "——>>> 设置系统最大打开文件数 <<<——"
sleep 3
if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
cat >> /etc/security/limits.conf << EOF
* soft nofile 65535 # 所有用户软限制
* hard nofile 65535 # 所有用户硬限制
EOF
fi
# 内核参数优化
echo "——>>> 系统内核优化 <<<——"
sleep 3
cat >> /etc/sysctl.conf << EOF
# 网络优化参数
net.ipv4.tcp_syncookies = 1 # 开启SYN洪水攻击保护
net.ipv4.tcp_max_tw_buckets = 20480 # 限制TIME_WAIT状态套接字数量
net.ipv4.tcp_max_syn_backlog = 20480 # 增大SYN等待队列长度
net.core.netdev_max_backlog = 262144 # 增加网络设备数据包队列长度
net.ipv4.tcp_fin_timeout = 20 # 减少FIN-WAIT-2状态超时时间
EOF
sysctl -p # 使内核参数立即生效
# SWAP优化
echo "——>>> 减少SWAP使用 <<<——"
sleep 3
echo "vm.swappiness = 0" >> /etc/sysctl.conf # 永久降低SWAP使用倾向
sysctl -w vm.swappiness=0 # 临时设置为0(优先使用物理内存)
# 安装常用工具
echo "——>>> 安装系统性能分析工具及其他 <<<——"
sleep 3
dnf install -y gcc make wget tar unzip vim net-tools lrzsz epel-release # 基础开发工具包
dnf install -y perf sysstat htop iotop iftop # 系统性能分析工具集
echo "=====系统初始化完成====="CentOS系统
#!/bin/bash
echo "=====系统环境初始化脚本====="
sleep 3
echo "——>>> 关闭防火墙与SELinux <<<——"
sleep 3
systemctl stop firewalld
systemctl disable firewalld &> /dev/null
setenforce 0
sed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config
echo "——>>> 创建阿里仓库 <<<——"
sleep 3
rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install wget
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
echo "——>>> 设置时区并同步时间 <<<——"
sleep 3
timedatectl set-timezone Asia/Shanghai
yum -y install chrony
systemctl start chronyd
systemctl enable chronyd
echo "——>>> 设置系统最大打开文件数 <<<——"
sleep 3
if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
cat >> /etc/security/limits.conf << EOF
* soft nofile 65535 #软限制
* hard nofile 65535 #硬限制
EOF
fi
echo "——>>> 系统内核优化 <<<——"
sleep 3
cat >> /etc/sysctl.conf << EOF
net.ipv4.tcp_syncookies = 1 #防范SYN洪水攻击,0为关闭
net.ipv4.tcp_max_tw_buckets = 20480 #此项参数可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死
net.ipv4.tcp_max_syn_backlog = 20480 #表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数
net.core.netdev_max_backlog = 262144 #每个网络接口 接受数据包的速率比内核处理这些包的速率快时,允许发送到队列的数据包的最大数目
net.ipv4.tcp_fin_timeout = 20 #FIN-WAIT-2状态的超时时间,避免内核崩溃
EOF
echo "——>>> 减少SWAP使用 <<<——"
sleep 3
echo "0" > /proc/sys/vm/swappiness
echo "——>>> 安装系统性能分析工具及其他 <<<——"
sleep 3
yum install -y gcc make autoconf vim sysstat net-tools iostat lrzsz
评论区