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

简介: 自动切换yum源

#!/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

                                               # 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 -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo

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

yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

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

#sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*

#sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*

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 -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo

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

yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

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

#sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*

#sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*

yum clean all ; yum makecache  ; yum list

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

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

# 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/

目录
相关文章
|
17天前
|
Linux C语言
linux yum安装ffmpeg 图文详解
linux yum安装ffmpeg 图文详解
38 0
|
17天前
|
Linux
linux yum 安装rar和unrar
linux yum 安装rar和unrar
57 0
|
25天前
|
Linux
Linux中centos桌面消失网络图标
Linux中centos桌面消失网络图标
13 0
|
19天前
|
Ubuntu Linux 编译器
【Linux】4. 开发工具的使用(yum/vim)
【Linux】4. 开发工具的使用(yum/vim)
44 2
|
17天前
|
关系型数据库 MySQL Linux
linux CentOS 7.4下 mysql5.7.20 密码改简单的方法
linux CentOS 7.4下 mysql5.7.20 密码改简单的方法
22 0
|
3天前
|
人工智能 数据可视化 Linux
【linux基础(五)】Linux中的开发工具(上)---yum和vim
【linux基础(五)】Linux中的开发工具(上)---yum和vim
|
3天前
|
Linux
centos 6.5安装yum
centos 6.5安装yum
22 0
|
5天前
|
存储 Linux 网络安全
centos7使用yum网络安装
这些是使用Yum进行网络安装的基本步骤。根据你的需求,你可以重复步骤3和4来安装其他软件包。请注意,执行Yum操作需要root或具有sudo权限的用户。
16 1
|
17天前
|
缓存 Linux
linux centos7 挂载本地iso yum源
linux centos7 挂载本地iso yum源
73 0
|
17天前
|
关系型数据库 MySQL Linux
CentOS 7 下使用yum安装MySQL5.7.20 最简单 图文详解
CentOS 7 下使用yum安装MySQL5.7.20 最简单 图文详解
78 0