系统优化脚本
- centos7
- rocky10
- ubuntu
#!/usr/bin/env bash
#
set -e
# 修改系统yum源为aliyun并添加epel源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
[ ! -e /etc/yum.repos.d/CentOS-Base.repo ] && curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[ ! -e /etc/yum.repos.d/epel.repo ] && curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
yum -y install ntpdate tar wget net-tools git vim tree lrzsz htop iftop iotop psmisc python36 python3-devel zlib zlib-devel gcc gcc-c++ conntrack-tools jq socat bash-completion telnet nload strace tcpdump lsof sysstat
# 关闭swap
sed -i 's/.*swap.*/#&/' /etc/fstab
# 关闭防火墙、selinux、NetworkManager
systemctl disable firewalld NetworkManager
sed -i '7s/enforcing/disabled/' /etc/selinux/config
# 同步时间计划任务
echo "*/10 * * * * /usr/sbin/ntpdate ntp2.aliyun.com &>/dev/null" >> /var/spool/cron/root
# 历史命令显示时间
sed -i '/HISTFILESIZE=2000/d' /etc/bashrc
sed -i '/HISTSIZE=2000/d' /etc/bashrc
sed -i '/HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "/d' /etc/bashrc
sed -i '/export HISTTIMEFORMAT/d' /etc/bashrc
cat >> /etc/bashrc << EOF
HISTFILESIZE=2000
HISTSIZE=2000
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
export HISTTIMEFORMAT
EOF
source /etc/bashrc
# 修改系统最大文件描述符
sed -i '/root soft nofile 65535/d' /etc/security/limits.conf
sed -i '/root hard nofile 65535/d' /etc/security/limits.conf
sed -i '/* soft nofile 65535/d' /etc/security/limits.conf
sed -i '/* hard nofile 65535/d' /etc/security/limits.conf
cat >>/etc/security/limits.conf<<'EOF'
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
EOF
# 设置pip国内源
[ -d ~/.pip ] || mkdir ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
# 安装 glances mycli
pip3 install --upgrade pip
pip3 install glances mycli==1.24.0
# 设置别名
cat >> ~/.bashrc << EOF
alias n='netstat -ntpl'
alias u='netstat -nupl'
alias a='netstat -natupl'
EOF
# 重启系统使部分配置生效
reboot
#!/usr/bin/env bash
#
set -e
# 修改系统yum源为阿里云
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/[Rr]ocky*.repo
# 添加epel源
dnf install -y epel-release
# 修改epel源为阿里云
sed -e 's|^metalink=|#metalink=|g' \
-e 's|^#baseurl=https://download.example/pub/epel|baseurl=https://mirrors.aliyun.com/epel|g' \
-i.bak \
/etc/yum.repos.d/epel.repo
# 更新缓存
dnf makecache
# 设置24小时制
localectl set-locale LC_TIME=en_GB.UTF-8
# 安装一些常用工具
dnf -y install \
yum-utils \
tar \
wget \
net-tools \
git \
vim \
tree \
lrzsz \
htop \
iftop \
iotop \
psmisc \
python3-devel \
zlib \
zlib-devel \
gcc \
gcc-c++ \
conntrack-tools \
jq \
socat \
bash-completion \
telnet \
nload \
strace \
tcpdump \
lsof \
sysstat
# 关闭swap
sed -i.bak '/swap/s/^/#/' /etc/fstab
swapoff -a
# Rocky 10 部分安装方式默认带 zram 交换设备(不受 fstab 控制),一并关闭
dnf remove -y zram-generator-defaults >/dev/null 2>&1 || true
# 关闭防火墙、selinux
systemctl disable --now firewalld
sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
# 配置时间同 步(Rocky 10 用 chrony 替代 ntpdate,改为国内 NTP 源)
dnf install -y chrony
sed -i.bak '/^pool/d' /etc/chrony.conf
cat >> /etc/chrony.conf << 'EOF'
server ntp.aliyun.com iburst
server ntp.tencent.com iburst
server cn.pool.ntp.org iburst
EOF
systemctl enable --now chronyd
systemctl restart chronyd
# 仿 ntpdate:一键强制同步时间(用法:ntpdate [NTP服务器],如 ntpdate ntp1.aliyun.com)
cat > /usr/local/bin/ntpdate <<'EOF'
#!/bin/bash
# 模拟 ntpdate:强制同步一次时间(兼容原用法,可选指定 NTP 服务器)
SERVER="${1:-ntp.aliyun.com}"
systemctl stop chronyd
chronyd -q "server ${SERVER} iburst"
systemctl start chronyd
EOF
chmod +x /usr/local/bin/ntpdate
# 配置历史命令显示时间
sed -i.bak '/HISTFILESIZE=2000/d; /HISTSIZE=2000/d; /HISTTIMEFORMAT="/d; /export HISTTIMEFORMAT/d' /etc/bashrc
cat >> /etc/bashrc << 'EOF'
HISTFILESIZE=2000
HISTSIZE=2000
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
export HISTTIMEFORMAT
EOF
source /etc/bashrc
# 修改系统最大文件描述符
sed -i.bak '/root soft nofile 65535/d; /root hard nofile 65535/d; /\* soft nofile 65535/d; /\* hard nofile 65535/d' /etc/security/limits.conf
cat >> /etc/security/limits.conf << 'EOF'
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
EOF
# 设置pip国内源
[ -d ~/.pip ] || mkdir ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
# 安装 glances mycli
pip3 install --upgrade pip
pip3 install glances mycli
# 设置别名(幂等,避免重复追加)
grep -q "alias n=" ~/.bashrc || cat >> ~/.bashrc << EOF
alias n='netstat -ntpl'
alias u='netstat -nupl'
alias a='netstat -natupl'
EOF
# 重启系统使部分配置生效
reboot
rocky10 与 centos7 版的差异说明
Rocky 10 已移除 ntpdate,用 chrony 替代:常驻的 chronyd 负责自动同步,无需 cron 定时任务(原来的 chronyc -a makestep 定时任务在现代 chrony 上需要认证,实际执行不了,已删除);时间偏差过大时 chronyd 只会慢慢漂移,此时用上面的 ntpdate 命令(内部执行 chronyd -q)强制跳变一次即可。
This is a banana 🍌

