目录

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
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
司朋几秒前
VMware安装CentOS 7(全网超详细图文保姆版教程)
linux·运维·centos
庶生几秒前
deepseek本地部署基于Linux系统(Ubuntu 22.04)Ollama、Cherry Studio和本地知识库系统
linux·python·ubuntu·bash
paoludecaic2 分钟前
CentOS 7的下载与安装
linux·centos
xxin¥2 分钟前
在 Ubuntu上安装 Node js 的三种方法
linux·javascript·ubuntu
Grayson_Zheng3 分钟前
基于VMware虚拟机的Ubuntu22.04系统安装和配置(新手保姆级教程)
linux·物联网·ubuntu
珹洺8 分钟前
计算机操作系统(四) 操作系统的结构与系统调用
运维·服务器·网络·数据库·计算机网络
dllmayday16 分钟前
root无权限修改文件内容处理
linux·ubuntu
竹竿袅袅22 分钟前
Docker 镜像构建与优化
运维·docker·容器
A charmer30 分钟前
深入理解 Linux 基础 IO:从文件操作到缓冲区机制
linux·运维·服务器
Arbori_2621536 分钟前
批量配置Linux ~/.bash_profile
linux·运维·bash