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

一、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源最大的好处就是自动解决依赖关系,联网装包非常方便,要注意的是在卸载时也会卸载的很干净,依赖的所有软件都会卸载。

三、镜像站点

1、国内企业镜像站

阿里开源镜像站:阿里巴巴开源镜像站-OPSX镜像站-阿里云开发者社区
华为开源镜像站:https://mirrors.huaweicloud.com/home
腾讯开源镜像站:腾讯软件源
网易开源镜像站:欢迎访问网易开源镜像站
搜狐开源镜像站:Index of /
平安开源镜像站:https://mirrors.pinganyun.com/

2、国内高校镜像站

清华大学开源镜像站:清华大学开源软件镜像站 | Tsinghua Open Source Mirror
南京大学开源镜像站:NJU Mirror

四、yum源部署

1、准备工作

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

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

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

3、备份本地yum源

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

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

4、查看系统版本

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

5、配置阿里云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

6、重新生成yum缓存

# 删除缓存数据
yum clean all

# 创建元数据缓存
yum makecache

7、yum常用命令

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

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

# 显示已配置的源
yum repolist

# 更新软件包
yum update 

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

五、一键配置yum源

1、编辑yum配置脚本

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

2、配置CentOS 7的yum源

#!/bin/bash

# 检查wget工具是否安装
if ! command -v wget &> /dev/null; then
    echo "wget未安装,执行yum -y install wget 安装"
    exit 1
fi

# 检查网络连接
ping -c 1 wwww.baidu.com > /dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "没有网络连接,脚本退出。"
    exit 1
fi

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

# 备份本地原有CentOS、epel库的yum源
cd /etc/yum.repos.d/
if [ -f "*repo" ]; then
    echo "repo文件存在,开始备份..."
    rm -f /etc/yum.repos.d/backup/*repo  # 删除旧的备份文件
    mv /etc/yum.repos.d/CentOS*.repo /etc/yum.repos.d/backup/
    mv /etc/yum.repos.d/epel*.repo /etc/yum.repos.d/backup/

    echo "备份完成!"
else
    echo "repo文件不存在,准备新建CentOS 7 yum源"
fi

# 获取系统类型和版本ID
distro=$(cat /etc/os-release | grep '^ID=' | cut -d '=' -f 2 | tr -d '"')
osversion=$(cat /etc/os-release | grep '^VERSION_ID' | cut -d '=' -f 2 | tr -d '"')

# 判断系统类型和版本并配置yum源
if [ "$distro" = "centos" ]; then
    if [ "$osversion" = "7" ]; then
        # CentOS 7 repo
        wget -q -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo >/dev/null 2>&1
        
        # epel 7 repo
        wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo >/dev/null 2>&1

        if [ $? -eq 0 ]; then  # 检查wget是否成功
            echo "CentOS 7 yum源配置完成。"
            # 更新yum软件包仓库
            yum repolist
            yum clean all
            yum makecache
        else
            echo "无法获取CentOS 7 yum源,请检查网络或源地址。"
        fi
    else
        echo "非 CentOS 7 系统,请手动添加yum源。"
    fi
else
    echo "非 CentOS 系统,请手动添加yum源。"
fi

3、配置其它版本的yum源

#!/bin/bash

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

# 检查网络连接
ping -c 1 wwww.baidu.com > /dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "没有网络连接,脚本退出。"
    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/

# 获取系统类型
distro=`cat /etc/os-release | grep '^ID='| cut -d '=' -f 2 | tr -d '"'`

# 获取系统版本ID
osversion=`cat /etc/os-release | grep '^VERSION_ID' | cut -d '=' -f 2 | tr -d '"'`

if [ "$distro" = "centos" ]; then

# 判断 CentOS 系统版本,安装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
  else
    echo "非CentOS系统,请手动添加yum源。"
fi

4、执行脚本

# 执行脚本
[root@hzk /]# bash  /etc/yum.repos.d/yumtest.sh 
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值