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
相关推荐
C_心欲无痕14 分钟前
ts - tsconfig.json配置讲解
linux·前端·ubuntu·typescript·json
冰西瓜6001 小时前
国科大2025操作系统高级教程期末回忆版
linux
HIT_Weston1 小时前
93、【Ubuntu】【Hugo】搭建私人博客:面包屑(一)
linux·运维·ubuntu
cuijiecheng20182 小时前
Linux下Beyond Compare过期
linux·运维·服务器
喵叔哟2 小时前
20.部署与运维
运维·docker·容器·.net
HIT_Weston2 小时前
92、【Ubuntu】【Hugo】搭建私人博客:侧边导航栏(六)
linux·运维·ubuntu
CodeAllen嵌入式2 小时前
Windows 11 本地安装 WSL 支持 Ubuntu 24.04 完整指南
linux·运维·ubuntu
码农小韩4 小时前
基于Linux的C++学习——指针
linux·开发语言·c++·学习·算法
wdfk_prog4 小时前
[Linux]学习笔记系列 -- [fs]seq_file
linux·笔记·学习
Jay Chou why did4 小时前
wsl安装完无法进入wsl
linux