yum源配置,看这一篇就够了!
一、yum源是什么
yum(全称Yellow Dog Updater)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器。基于RPM 包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理包之间的依赖关系,并且一次安装所有依赖的软件包。
二、yum源的作用
- yum源是Linux系统中的软件管理仓库,可以完成安装、卸载、自动升级rpm软件包等任务。
- yum源能够自动查找并解决rpm包之间的依赖关系,并一次安装所有依赖的相关软件包,无需管理员手动去安装每个rpm包。
- yum源也能够在大量Linux服务器中,缓解软件安装、升级等对Internet的依赖。
- yum源最大的好处就是自动解决依赖关系,联网装包非常方便,要注意的是在卸载时也会卸载的很干净,依赖的所有软件都会卸载。
三、镜像站点
1、国内企业镜像站
阿里开源镜像站: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/
2、国内高校镜像站
清华大学开源镜像站:https://mirrors.tuna.tsinghua.edu.cn/
南京大学开源镜像站:https://mirrors.nju.edu.cn/download
四、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
本文作者:湖中客
本文链接:https://www.cnblogs.com/hzke/p/17849772.html
转载声明:转载请注明出处,如有侵权,请联系作者修改或删除。
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步