跳到主要内容
08:44:14
🔅🔆

系统优化脚本

#!/usr/bin/env bash
#

set -e

# 修改系统yum源为aliyun并添加epel源
# 幂等:首次运行才备份原 repo 并下载阿里 repo,之后跳过(重复执行安全)
# 注意:条件判断用 if 而非 [ ] &&,否则条件不满足时 set -e 会中断脚本
if [ ! -e /etc/yum.repos.d/CentOS-Base.repo ]; then
[ -e /etc/yum.repos.d/CentOS-Base.repo.bak ] || mv /etc/yum.repos.d/CentOS-Base.repo{,.bak} 2>/dev/null || true
curl -fsSL -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
fi
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

if [ ! -e /etc/yum.repos.d/epel.repo ]; then
curl -fsSL -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
fi
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/s/^/#/ }' /etc/fstab

# 关闭防火墙、selinux、NetworkManager
systemctl disable --now firewalld NetworkManager
# 幂等:按 SELINUX= 前缀匹配(原来写死第 7 行,行号变了就失效)
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

# 同步时间计划任务
# 幂等:已存在则不追加
grep -q 'ntpdate ntp2.aliyun.com' /var/spool/cron/root 2>/dev/null || \
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

# ===== 安装并配置 grc(源码安装:clone 官方仓库 + 官方 install.sh;命令输出着色)=====
# 安装路径与官方一致:bin→/usr/bin、conf→/usr/share/grc、grc.conf→/etc、
# grc.sh→/etc/profile.d/、grc.zsh→/etc
# 幂等:已安装则跳过;clone 失败仅提示跳过,不中断脚本(配置段探测不到文件就不会写入)
if ! command -v grc >/dev/null 2>&1; then
rm -rf /tmp/grc-src
# clone 和安装一条链:任一步失败都走 else 分支提示跳过,不会中断脚本
if git clone --depth 1 https://github.com/garabik/grc.git /tmp/grc-src \
&& (cd /tmp/grc-src && ./install.sh /usr ""); then
echo "grc: 源码安装完成(/usr/bin/grc,配置在 /etc/profile.d/grc.sh、/etc/grc.zsh)"
else
echo "grc: git clone 或 install.sh 失败(网络原因?),跳过安装,着色配置不生效"
fi
fi

# 配置部分:依据官方 README/源码,bash 的 grc.sh 必须先设 GRC_ALIASES=true 才会创建别名(grc.zsh 无此要求);
# bash 启用文件为 /etc/profile.d/grc.sh(源码与 Debian 包默认位置),zsh 为 /etc/grc.zsh;已配置则跳过
USER_SHELL="$(getent passwd "$(id -un)" | cut -d: -f7)"
GRC_CFG=""
RCFILE="$HOME/.bashrc"
case "$USER_SHELL" in
*/zsh)
RCFILE="$HOME/.zshrc"
for f in /etc/grc.zsh /usr/local/etc/grc.zsh /usr/share/grc/grc.zsh; do
if [ -f "$f" ]; then GRC_CFG="$f"; break; fi
done
;;
*)
for f in /etc/profile.d/grc.sh /etc/grc.sh /usr/local/etc/profile.d/grc.sh; do
if [ -f "$f" ]; then GRC_CFG="$f"; break; fi
done
;;
esac
if [ -n "$GRC_CFG" ] && ! grep -q 'grc' "$RCFILE" 2>/dev/null; then
if [ "$(basename "$GRC_CFG")" = "grc.sh" ]; then
echo 'GRC_ALIASES=true' >> "$RCFILE"
fi
echo "[ -s $GRC_CFG ] && . $GRC_CFG" >> "$RCFILE"
fi

# 设置别名(幂等,避免重复追加)
grep -q "alias n=" ~/.bashrc || cat >> ~/.bashrc << EOF
alias n='netstat -ntpl'
alias u='netstat -nupl'
alias a='netstat -natupl'
EOF

# 重启系统使部分配置生效
reboot
🛡️本站已安全运行2334天23时19分10秒
Bottom GIF
Top GIF