yum源配置,看这一篇就够了!

发布时间 2023-11-22 17:07:09作者: 湖中客

yum源是什么

yum(全称Yellow Dog Updater)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器。基于RPM 包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理包之间的依赖关系,并且一次安装所有依赖的软件包。

yum源的作用

  1. yum源是Linux系统中的软件管理仓库,可以完成安装、卸载、自动升级rpm软件包等任务。
  2. yum源能够自动查找并解决rpm包之间的依赖关系,并一次安装所有依赖的相关软件包,无需管理员手动去安装每个rpm包。
  3. yum源也能够在大量Linux服务器中,缓解软件安装、升级等对Internet的依赖。
  4. yum源最大的好处就是自动解决依赖关系,联网装包非常方便,要注意的是在卸载时也会卸载的很干净,依赖的所有软件都会卸载。

国内企业镜像站

阿里开源镜像站:https://developer.aliyun.com/mirror/
华为开源镜像站:https://mirrors.huaweicloud.com/home
腾讯开源镜像站:https://mirrors.cloud.tencent.com/
网易开源镜像站:https://mirrors.163.com/
搜狐开源镜像站:http://mirrors.sohu.com/
平安开源镜像站:https://mirrors.pinganyun.com/

国内高校镜像站

清华大学开源镜像站:https://mirrors.tuna.tsinghua.edu.cn/
南京大学开源镜像站:https://mirrors.nju.edu.cn/download

系统环境

CentOS Linux 服务器 
系统版本:CentOS Linux release 7.9.2009 (Core)
内核版本:3.10.0-1160.102.1.el7.x86_64

安装wget包,以防下载时提示未找到命令...

[root@hzk /]# yum -y install wget

备份本地yum源包

# 创建备份目录
[root@hzk /]# mkdir -p /etc/yum.repos.d/backup/

# 备份本地yum包     
[root@hzk /]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/  

查看系统版本

[root@hzk /]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

配置系统相应版本阿里云yum源、epel库

# 下载对应系统版本的阿里云yum源
[root@hzk /]# wget -O /etc/yum.repos.d/CentOs-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 下载epel开源发行软件包版本库,可以提供额外的软件包
[root@hzk /]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

清除缓存,重新生成缓存

# 删除缓存数据
yum clean all

# 创建元数据缓存
yum makecache

其它常见yum命令

# 下载软件包,-y表示安装过程中回答全部问题为'是'
yum -y install [软件包名]

# 删除软件包
yum -y remove [软件包名]

# 显示已配置的源
yum repolist

# 更新软件包
yum update 

# 查看yum更多命令
yum -h 或 yum --help

CentOS 一键配置网络yum源

#!/bin/bash
#
#**************************************************************
# @Author:                          hzk
# @FileName:                        yumtest.sh
# @Version:                         V1.0
# @CreationDate:                    2023-11-22 16:26:16
# @Description:                     The yum test script 
# @Copyright(C)2023 xx公司 All Rights Reserved.
#*************************************************************

# 检查yum是否安装
# command -v [命令名]:显示指定命令是否已安装及其路径。
if ! command -v yum >/dev/null 2>&1; then
    echo "yum未安装,请先执行yum -y install yum 命令,安装yum工具。"
    exit 1
fi

# 创建备份目录
mkdir -p /etc/yum.repos.d/backup

# 备份本地原有CentOS、epel库的yum源
mv /etc/yum.repos.d/CentOS*.repo /etc/yum.repos.d/backup/
mv /etc/yum.repos.d/epel*.repo /etc/yum.repos.d/backup/

# 获取版本ID,若其它系统不适用,修改变量语句
osversion=$(cat /etc/os-release | grep '^VERSION_ID' | cut -d '=' -f 2 | tr -d '"')

# 判断 CentOS 系统版本,安装不同版本yum源
case ${osversion} in
  6)
    # CentOS 6 官方源已下线,建议切换centos-vault源
    wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo

    # epel 6 官方源已下线,建议切换epel-archive源
    wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-archive-6.repo
    ;;
  7)
    # 下载 CentOS 7 阿里云yum源
    wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

    # 下载 epel 7 阿里云yum源
    wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
    ;;
  8) 
    # CentOS 8 官方源已下线,建议切换centos-vault源
    wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

    # 下载 epel-release-latest-8 阿里云yum源
    yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

    # 将 repo 配置中的地址替换为阿里云镜像站地址
    sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
    sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
   ;;
  *)
    echo "请检查系统版本,重新安装!"
    ;;
esac

# 清除本地缓存
yum clean all

# 重新生成缓存
yum makecache

创建并执行脚本

# 创建 yumtest.sh 文件 ,并复制以上脚本内容
[root@hzk /]# vim /etc/yum.repos.d/yumtest.sh

# 执行
[root@hzk /]# bash  /etc/yum.repos.d/yumtest.sh