本文介绍一个centos各版本更换阿里源的脚本,以作参考
#!/bin/bash
# Detect CentOS version
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_NAME=$ID
OS_VERSION_ID=$VERSION_ID
elif [ -f /etc/centos-release ]; then
OS_NAME="centos"
OS_VERSION_ID=$(rpm -q --qf "%{VERSION}" $(rpm -q --whatprovides redhat-release))
else
echo "不支持的系统类型."
exit 1
fi
# Function to backup and replace YUM repo files
replace_yum_repo() {
local url=$1
local dest_dir="/etc/yum.repos.d"
# Backup existing repo files
mkdir -p "${dest_dir}/backup"
mv ${dest_dir}/*.repo ${dest_dir}/backup/
# Download new repo file
curl -o ${dest_dir}/CentOS-Base.repo ${url}
}
# Replace YUM repo based on CentOS version
case $OS_VERSION_ID in
6)
echo "Detected CentOS 6"
replace_yum_repo "http://www.landui.com/repo/Centos-6.repo"
;;
7)
echo "Detected CentOS 7"
replace_yum_repo "http://www.landui.com/repo/Centos-7.repo"
;;
8)
echo "Detected CentOS 8"
replace_yum_repo "http://www.landui.com/repo/Centos-8.repo"
;;
*)
echo "不支持的系统版本: $OS_VERSION_ID"
exit 1
;;
esac
# Clean YUM cache
yum clean all
yum makecache
echo "yum已成功更换为阿里源!"
# End of script