一键替换主流 Centos 版本国内(阿里云、华为云、腾讯云)yum和epel源
#!/bin/bash
get_release_version() {
cat /etc/redhat-release | awk '{match($0,"release "); print substr($0,RSTART+RLENGTH)}' | awk -F '.' '{print $1}'
}
update_yum_source() {
local start_time=$(date +%s%N)
local version=$1
local provider=$2
local repo_url=""
local backup_dir="/etc/yum.repos.d/backup"
local backup_file="$backup_dir/CentOS-Base.repo.bak.$(date +%Y%m%d%H%M%S)"
mkdir -pv "$backup_dir"
[ -f "/etc/yum.repos.d/CentOS-Base.repo" ] && mv "/etc/yum.repos.d/CentOS-Base.repo" "$backup_file" || true
case $provider in
aliyun)
case $version in
6)
wget_repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo
if host mirrors.cloud.aliyuncs.com >/dev/null 2>&1; then
echo -e "\033[32m是阿里云ECS实例,YUM源已成功更新为阿里云内网YUM源地址...\033[0m"
else
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
echo -e "\033[32m非阿里云ECS实例,YUM源已成功更新为阿里云外网YUM源地址...\033[0m"
fi
;;
7)
wget_repo https://mirrors.aliyun.com/repo/Centos-7.repo
if host mirrors.cloud.aliyuncs.com >/dev/null 2>&1; then
echo -e "\033[32m是阿里云ECS实例,YUM源已成功更新为阿里云内网YUM源地址...\033[0m"
else
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
echo -e "\033[32m非阿里云ECS实例,YUM源已成功更新为阿里云外网YUM源地址...\033[0m"
fi
;;
8)
if host mirrors.cloud.aliyuncs.com >/dev/null 2>&1; then
wget_repo http://mirrors.cloud.aliyuncs.com/repo/Centos-vault-8.5.2111.repo
sed -i 's/mirrors.cloud.aliyuncs.com/url_tmp/g' /etc/yum.repos.d/CentOS-Base.repo && sed -i 's/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g' /etc/yum.repos.d/CentOS-Base.repo && sed -i 's/url_tmp/mirrors.aliyun.com/g' /etc/yum.repos.d/CentOS-Base.repo
echo -e "\033[32m是阿里云ECS实例,YUM源已成功更新为阿里云内网YUM源地址...\033[0m"
else
wget_repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
echo -e "\033[32m非阿里云ECS实例,YUM源已成功更新为阿里云外网YUM源地址...\033[0m"
fi
;;
*)
echo "当前尚不支持此 CentOS 版本:$version,未来有时间将会进行补充。如有需要,请发送邮件至 zuoyangs@qq.com。"
exit 1
;;
esac
;;
huaweicloud)
echo "准备更新为华为云YUM源..."
wget_repo "https://mirrors.huaweicloud.com/repository/conf/CentOS-${version}-anon.repo"
;;
tencent)
case $version in
5)
wget_repo "http://mirrors.cloud.tencent.com/repo/centos5_base.repo"
;;
6)
wget_repo "http://mirrors.cloud.tencent.com/repo/centos6_base.repo"
;;
7)
wget_repo "http://mirrors.cloud.tencent.com/repo/centos7_base.repo"
;;
8)
wget_repo "http://mirrors.cloud.tencent.com/repo/centos8_base.repo"
;;
*)
echo "当前尚不支持此 CentOS 版本:$version,未来有时间将会进行补充。如有需要,请发送邮件至 zuoyangs@qq.com。"
exit 1
;;
esac
;;
*)
echo "未知的云服务提供商: $provider"
exit 1
;;
esac
}
wget_repo() {
local repo_url=$1
echo -e "\033[33m正在从 $repo_url 下载YUM源配置文件...\033[0m"
if ! wget -O /etc/yum.repos.d/CentOS-Base.repo "$repo_url"; then
echo -e "\033[31m执行 wget 失败,请检查网络连接或URL是否正确。\033[0m" # 红色错误信息
exit 1
else
echo -e "\033[32mYUM源配置文件已成功下载并保存到 /etc/yum.repos.d/CentOS-Base.repo\033[0m"
fi
}
pre_install() {
local start_time=$(date +%s%N)
if command -v wget &> /dev/null; then
:
else
echo "wget 没有预安装,准备安装 wget"
yum install wget -y >/dev/null 2>&1
echo "已安装好 wget"
fi
local end_time=$(date +%s%N)
local duration=$((end_time - start_time))
print_duration $duration "pre_install 函数执行耗时:"
}
print_duration() {
local duration_ns=$1
local prefix=$2
if [ -z "$prefix" ]; then
prefix="执行耗时:"
fi
if [ $duration_ns -gt 1000000000 ]; then
duration_s=$(($duration_ns / 1000000000))
echo "${prefix}${duration_s}秒"
else
duration_ms=$(($duration_ns / 1000000))
echo "${prefix}${duration_ms}毫秒"
fi
}
main() {
local start_time=$(date +%s%N)
echo "当前时间:$(date +'%Y-%m-%d %H:%M:%S')"
pre_install
local version=$(get_release_version)
local provider=$1
update_yum_source "$version" "$provider"
yum clean all
yum makecache
yum install -y epel-release
local end_time=$(date +%s%N)
local duration=$((end_time - start_time))
print_duration $duration "main 函数执行总耗时:"
}
# 检查是否以 root 用户运行
if [ "$(id -u)" != "0" ]; then
echo "该脚本必须以root用户运行" 1>&2
exit 1
fi
if [[ $# -eq 0 ]]; then
echo "用法: $0 {aliyun|huaweicloud}"
exit 1
fi
main $1