[root@haimati shell]# cat Yum_Aliyun.sh
#!/bin/bash
# 替换默认Yum源为 国内阿里云Yum源
# 适用于Linux Centos 6-8 版本!
. /etc/init.d/functions
REDHAT=$(cat /etc/redhat-release | awk '{print $4}'| awk -F . '{print $1}')
if [ $REDHAT -eq 7 ];then
if ! [ -d /etc/yum.repos.d/Yum.bak ];then
mkdir /etc/yum.repos.d/Yum.bak/
fi
mv /etc/yum.repos.d/CentOS-base.repo /etc/yum.repos.d/Yum.bak
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/Yum.bak
(wget -O /etc/yum.repos.d/CentOS-base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo) &>/dev/null
if [ $? -eq 0 ];then
action "Yum:"
else
action "Yum" false
fi
elif [ $REDHAT -eq 6 ];then
if ! [ -d /etc/yum.repos.d/Yum.bak ];then
mkdir /etc/yum.repos.d/Yum.bak/
fi
(mv /etc/yum.repos.d/CentOS-base.repo /etc/yum.repos.d/Yum.bak
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/Yum.bak) &>/dev/null
(wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
wget -O /etc/yum.repos.d/CentOS-base.repo https://mirrors.aliyun.com/repo/Centos-6.repo) &>/dev/null
if [ $? -eq 0 ];then
action "Yum:"
else
action "Yum" false
fi
elif [ $REDHAT -eq 8 ];then
if ! [ -d /etc/yum.repos.d/Yum.bak ];then
mkdir /etc/yum.repos.d/Yum.bak/
fi
(mv /etc/yum.repos.d/CentOS-base.repo /etc/yum.repos.d/Yum.bak
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/Yum.bak) &>/dev/null
(wget -O /etc/yum.repos.d/CentOS-base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*) &>/dev/null
if [ $? -eq 0 ];then
action "Yum:"
else
action "Yum" false
fi
fi