收藏文章 楼主

linux centos6/7/8自动 切换yum源

版块:自动化运维linux   类型:普通   作者:小绿叶技术博客   查看:3777   回复:9   获赞:4   时间:2020-04-01 16:34:36

#!/bin/bash

centos6d(){

release=`cat /etc/redhat-release  |grep release | awk -F" " '{print $3}' | awk -F"." '{print $1}'` ; echo "centos 版本为: $release "

case "$release" in

"6") echo " 当前匹配centos 版本为 6"

repo="/etc/yum.repos.d/centos6-epel.repo"

if [ ! -e $repo ]; then                 # -e 判断文件存在;参考:Shell if 条件判断文件或目录

yum install -y wget

cd /etc/yum.repos.d/

mkdir bak

mv * bak

wget work.eisc.cn/ruanjian/rpm/yumrepo/centos6-epel.repo

wget work.eisc.cn/ruanjian/rpm/yumrepo/centos6.repo

yum clean all                           # 清除原有缓存

yum makecache                      # 生成新的缓存

                                                # make cache [me?k]  [ka?]  制作 缓存{ka xi}

yum  list                                       # 校验yum

cd

else

echo "yum已经切换"

fi

;;

*) echo "低版本未匹配到,正在匹配红帽版本配置yum !!!"

redhat8d

;;

esac

}




centos7(){

repo="/etc/yum.repos.d/epel-7.repo"

if [ ! -e $repo ]; then                 # -e 判断文件存在;参考:Shell if 条件判断文件或目录

yum install -y wget

cd /etc/yum.repos.d/

mkdir bak

mv * bak

wget work.eisc.cn/ruanjian/rpm/yumrepo/Centos-7.repo

wget work.eisc.cn/ruanjian/rpm/yumrepo/epel-7.repo

# wget http://mirrors.aliyun.com/repo/Centos-7.repo

# wget http://mirrors.aliyun.com/repo/epel-7.repo

                                                # 移动两个文件到 目录

#wget http://mirrors.163.com/.help/CentOS7-Base-163.repo

#sed -i  's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo

# sed -i  's/$releasever/7/g' /etc/yum.repos.d/*

                                                # yum源没有对应版本包匹配;修改yum为centos7

yum clean all                           # 清除原有缓存

yum makecache                      # 生成新的缓存

                                                # make cache [me?k]  [ka?]  制作 缓存{ka xi}

yum  list                                       # 校验yum

cd

else

echo "yum已经切换"

fi

}

centos8(){


repo="/etc/yum.repos.d/Centos-8.repo"

if [ ! -e $repo ]; then

cd /etc/yum.repos.d/

mkdir bak

mv * bak


wget work.eisc.cn/ruanjian/rpm/yumrepo/centos8/centos8.repo

wget work.eisc.cn/ruanjian/rpm/yumrepo/centos8/epel.repo


yum clean all #清理缓存

yum makecache #更新源

else

echo "yum已经切换"

fi

}


redhat8d(){

release=`cat /etc/redhat-release  |grep release | awk -F" " '{print $6}' | awk -F"." '{print $1}'` ; echo "redhat 版本为: $release "

case "$release" in

"8") echo " 当前匹配redhat 版本为 8"


repo="/etc/yum.repos.d/Centos-8.repo"

if [ ! -e $repo ]; then


cd /etc/yum.repos.d/

mkdir bak

mv * bak

wget https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo -O /etc/yum.repos.d/Centos-vault-8.5.2111.repo

wget https://mirrors.aliyun.com/repo/epel-archive-8.repo -O /etc/yum.repos.d/epel-archive-8.repo

sed -i 's/mirrors.cloud.aliyuncs.com/url_tmp/g'  /etc/yum.repos.d/Centos-vault-8.5.2111.repo &&  sed -i 's/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g' /etc/yum.repos.d/Centos-vault-8.5.2111.repo && sed -i 's/url_tmp/mirrors.aliyun.com/g' /etc/yum.repos.d/Centos-vault-8.5.2111.repo

sed -i 's/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g' /etc/yum.repos.d/epel-archive-8.repo

yum clean all && yum makecache


else

echo "yum已经切换"

fi

;;

*) echo "红帽版本未匹配到,退出!!!"

;;

esac

}




release=`cat /etc/redhat-release  |grep release | awk -F" " '{print $4}' | awk -F"." '{print $1}'` ; echo "centos 版本为: $release "

case "$release" in

"6") echo " 当前匹配centos 版本为 6"

centos6

;;

"7") echo " 当前匹配centos 版本为 7"

centos7

;;

"8") echo "当前匹配centos 版本为 8"

centos8

;;

*) echo "您的centos过于老旧,正在匹配低版本"

centos6d

;;

esac


# 清华 apt 源:https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

# centos 6 切换yum: https://help.aliyun.com/document_detail/193569.htm

# 一键切换yum执行: wget www.eisc.cn/file/shell/centos-yum.sh ; sh centos-yum.sh

# 感谢网友提供yum解决方案:https://developer.aliyun.com/article/748336

# 阿里巴巴开源镜像站,镜像仓库:  https://developer.aliyun.com/mirror/

# 阿里云linux系统镜像: https://mirrors.aliyun.com/centos-vault/



1. /etc/yum.repos.d 文件夹里面的文件全部移动到其他目录,重新部署 yum

2. 辛苦切换一下yum :https://developer.aliyun.com/mirror/centos?spm=a2c6h.13651102.0.0.3e221b11Pi5Ktu



#---------- alibaba3 -----------#

mkdir /yumback

mv /etc/yum.repos.d/* /yumback

wget work.eisc.cn/ruanjian/rpm/yumrepo/alibaba3/AliYun.repo -O /etc/yum.repos.d/al3.repo

wget work.eisc.cn/ruanjian/rpm/yumrepo/alibaba3/epel.repo -O /etc/yum.repos.d/epel3.repo

yum clear all ; yum list



#--------- centos8 停止维护,更换yum  -------------#

停止维护公告:https://help.aliyun.com/noticelist/articleid/1060980265.html

更换yum:https://help.aliyun.com/document_detail/405635.html

迁移服务器系统:https://help.aliyun.com/document_detail/370865.htm?spm=a2c4g.11186623.0.0.58863201lhpfjW#task-2156316

有些梦虽然遥不可及,但并不是不可能实现。 
回复列表
默认   热门   正序   倒序

回复:linux centos6/7/8自动 切换yum源

Powered by ddoss.cn 12.0

©2015 - 2024 ddoss

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息