linux更换镜像源[CentOs]

问题:在使用linux的yum命令时常常会遇到由于无法加载到centos官方镜像源的问题,报错信息如图所示

解决方法:更换国内的数据源

1. 备份原有仓库配置
bash 复制代码
sudo cp -r /etc/yum.repos.d/ /etc/yum.repos.d.backup  # 备份整个目录
sudo rm -rf /etc/yum.repos.d/CentOS-*                # 删除默认官方仓库
2. 选择国内镜像源
选项一:阿里云镜像源(推荐)
  • 创建新仓库文件:
bash 复制代码
sudo vi /etc/yum.repos.d/CentOS-Base-Aliyun.repo
  • 粘贴以下内容(适用于 CentOS 7):
bash 复制代码
[base]
name=CentOS-7 - Base - Aliyun
baseurl=https://mirrors.aliyun.com/centos/7/os/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-7 - Updates - Aliyun
baseurl=https://mirrors.aliyun.com/centos/7/updates/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-7 - Extras - Aliyun
baseurl=https://mirrors.aliyun.com/centos/7/extras/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
选项二:腾讯云镜像源
  • 直接下载预配置的 repo 文件:
bash 复制代码
sudo curl -o /etc/yum.repos.d/CentOS-Base-Tencent.repo https://mirrors.cloud.tencent.com/repo/centos7_base.repo
选项三:华为云镜像源
  • 下载华为云仓库配置:
bash 复制代码
sudo curl -o /etc/yum.repos.d/CentOS-Base-Huawei.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo
3. 清理并重建缓存
bash 复制代码
sudo yum clean all       # 清除旧缓存
sudo yum makecache       # 生成新缓存
4. 测试镜像源
bash 复制代码
sudo yum update          # 检查是否能正常更新
5. 常见问题处理
  • GPG 密钥错误:
    若提示 GPG key retrieval failed,手动导入密钥:
bash 复制代码
sudo rpm --import https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  • 镜像源速度慢:

    尝试更换其他镜像源(如清华源、网易源)。

  • 仓库元数据损坏:

    彻底清理缓存后重试:

bash 复制代码
sudo rm -rf /var/cache/yum
sudo yum makecache

其他国内镜像源地址

镜像源 CentOS 7 仓库配置地址
清华源 https://mirrors.tuna.tsinghua.edu.cn/help/centos/
网易源 http://mirrors.163.com/.help/centos.html
中科大源 https://mirrors.ustc.edu.cn/help/centos.html
附:一键脚本(适用于 CentOS 7)
bash 复制代码
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.aliyun.com|g' \
         -i.bak /etc/yum.repos.d/CentOS-*.repo
sudo yum makecache
相关推荐
涵信6 分钟前
第十二节:性能优化高频题-shallowRef/shallowReactive使用场景
linux·ubuntu·性能优化
老秦包你会20 分钟前
Linux课程五课---Linux进程认识1
linux·运维·服务器
等猪的风33 分钟前
openwrt作旁路由时的几个常见问题 openwrt作为旁路由配置zerotier 图文讲解
运维·服务器·网络
互联网搬砖老肖37 分钟前
运维打铁:域名详解及常见问题解决
运维·github
浩浩测试一下37 分钟前
网络安全实战指南:从安全巡检到权限维持的应急响应与木马查杀全(命令查收表)
linux·安全·web安全·ubuntu·网络安全·负载均衡·安全架构
我想吃余1 小时前
Linux学习笔记(一):Linux下的基本指令
linux·笔记·学习
LaughingZhu1 小时前
PH热榜 | 2025-04-24
运维·经验分享·搜索引擎·产品运营·jenkins
刘某的Cloud1 小时前
openstack迁移虚机rbd报错,删除异常rbd
linux·运维·openstack·nova·rbd
啊吧怪不啊吧2 小时前
Linux权限概念讲解
linux·运维·服务器
文牧之2 小时前
PostgreSQL oracle_fdw 扩展解析
运维·数据库·postgresql