咨询热线:4006-75-4006
售前:9:00-23:30 备案:9:00-18:00 技术:7*24h
centos各版本更换阿里源的脚本
蓝队云小课堂:
本文介绍一个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
更多小知识,可联系蓝队云一起探讨。