说明:
CentOS 7 已于 2024 年 6 月 正式停止维护,官方的mirrorlist.centos.org服务已关闭,继续使用默认源会出现:
Could not resolve host: mirrorlist.centos.org因此,必须将 YUM 源替换为仍在维护的镜像站,如阿里云镜像源。
1. YUM 源文件位置:
/etc/yum.repos.d/每个
.repo文件表示一组仓库YUM 会自动加载该目录下所有
.repo文件
2.备份并移除旧源
mkdir -p /etc/yum.repos.d/backup mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/mkdir -p:创建备份目录(不存在则创建)mv *.repo:将所有旧 YUM 源文件移走,防止冲突避免系统继续访问已失效的官方 mirrorlist
3.创建新的 CentOS-Base.repo
vi /etc/yum.repos.d/CentOS-Base.repo4.写入以下内容(阿里云 CentOS 7 镜像源):
[base] name=CentOS-7 - Base baseurl=https://mirrors.aliyun.com/centos/7/os/$basearch/ enabled=1 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 [updates] name=CentOS-7 - Updates baseurl=https://mirrors.aliyun.com/centos/7/updates/$basearch/ enabled=1 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 [extras] name=CentOS-7 - Extras baseurl=https://mirrors.aliyun.com/centos/7/extras/$basearch/ enabled=1 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7解析:
[base] / [updates] / [extras]
仓库 ID(repoid)
用于 YUM 内部识别
baseurl=
RPM 包的真实下载地址
使用硬编码地址,不依赖 mirrorlist
$basearch会自动替换为x86_64等架构
enabled=1
1 表示启用仓库
0 表示禁用仓库
gpgcheck=1
启用 RPM 包签名校验
防止安装被篡改的软件包
gpgkey=
指定 GPG 公钥位置
第一次使用会提示导入
5.清理旧缓存
yum clean all清除旧的仓库元数据
避免 YUM 使用历史缓存
6.生成新的缓存
yum makecache从阿里云镜像下载元数据
验证仓库是否可用
7.查看仓库状态
yum repolist如果输出:
base/7/x86_64 updates/7/x86_64 extras/7/x86_64说明配置成功
扩展:其他镜像网站
1.腾讯云镜像站
https://mirrors.cloud.tencent.com/centos/7/
2.华为云镜像站
https://repo.huaweicloud.com/centos/7/
3.清华大学 TUNA
https://mirrors.tuna.tsinghua.edu.cn/centos/7/