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
相关推荐
鹏大师运维1 天前
为什么信创电脑装软件总提示“软件包架构不匹配”?
linux·运维·架构·国产化·麒麟·deb·统信uos
007张三丰1 天前
软件测试专栏(11/20):测试框架开发:pytest深度解析与插件体系
运维·服务器·自动化测试·pytest·测试框架
weixin_604236671 天前
华三 路由器 极简核心配置
运维·服务器·网络·h3c·h3c路由器
鹤落晴春1 天前
【Linux复习】管理SELinux安全性
linux·运维·服务器
yz_aiks1 天前
Linux Jar包配置Systemd自启动实战:从排查到配置全流程
linux·python·jar·自启动·systemd
AI智图坊1 天前
多件装组合SKU图的批量生产效率分析:从PS手工到AI自动化的工作流改造
大数据·运维·人工智能·gpt·ai作画·自动化·aigc
bjzhang751 天前
CentOS下安装MySQL详解
linux·mysql·centos
Jason_chen1 天前
Linux 6.2 音频机制深度解析:AI驱动的低延迟音频与零信任音频安全架构
linux
下午写HelloWorld1 天前
Linux系统及Ubuntu常用指令
linux·ubuntu·操作系统
云计算磊哥@1 天前
运维开发宝典026-MySQL02数据库表操作
运维·数据库·运维开发