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

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享