在Linux系统中,CentOS是一个广泛使用的操作系统,为了优化性能或解决兼容性问题,用户可能会选择更换CentOS的镜像源,以下是关于如何在CentOS中更换镜像源的详细指南。

了解镜像源
镜像源是指存储软件包的地方,它可以是官方源,也可以是第三方提供的源,更换镜像源的主要目的是提高软件包下载速度,或者获取到特定版本的软件包。
更换CentOS 7镜像源
查看当前镜像源
在终端中输入以下命令,查看当前使用的镜像源:
cat /etc/yum.repos.d/CentOS-Base.repo
下载并替换镜像源
以下是一个常用的CentOS 7镜像源列表,您可以根据需要选择合适的源进行替换。
国内常用镜像源:
| 镜像源名称 | 镜像源地址 |
|---|---|
| 网易163 | http://mirrors.163.com/centos/7/ |
| 阿里云 | http://mirrors.aliyun.com/centos/7/ |
| 禹盟 | http://mirrors.yun-idc.com/centos/7/ |
| 网易163(64位) | http://mirrors.163.com/centos/7/os/ |
| 阿里云(64位) | http://mirrors.aliyun.com/centos/7/os/ |
| 禹盟(64位) | http://mirrors.yun-idc.com/centos/7/os/ |
替换步骤:
使用文本编辑器打开镜像源配置文件:
sudo vi /etc/yum.repos.d/CentOS-Base.repo
删除原有的镜像源配置,并粘贴以下内容(以网易163为例):
[base] name=CentOS-7 - Base mirrorlist=http://mirrors.163.com/mirrors/centos/mirrorlist?release=7&arch=x86_64&repo=base&infra=client baseurl=http://mirrors.163.com/centos/7/os/x86_64/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 [epel] name=CentOS-7 - EPEL (Extra Packages for Enterprise Linux) mirrorlist=http://mirrors.163.com/mirrors/centos/mirrorlist?release=7&arch=x86_64&repo=epel&infra=client baseurl=http://mirrors.163.com/centos/7/epel/x86_64/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
保存并关闭文件。
更新本地缓存
在终端中执行以下命令,更新本地缓存:

sudo yum makecache
更换CentOS 8镜像源
CentOS 8的镜像源更换步骤与CentOS 7类似,以下是具体步骤:
查看当前镜像源
cat /etc/yum.repos.d/CentOS-Base.repo
下载并替换镜像源
国内常用镜像源:
| 镜像源名称 | 镜像源地址 |
|---|---|
| 网易163 | http://mirrors.163.com/centos/8/ |
| 阿里云 | http://mirrors.aliyun.com/centos/8/ |
| 禹盟 | http://mirrors.yun-idc.com/centos/8/ |
| 网易163(64位) | http://mirrors.163.com/centos/8/os/x86_64/ |
| 阿里云(64位) | http://mirrors.aliyun.com/centos/8/os/x86_64/ |
| 禹盟(64位) | http://mirrors.yun-idc.com/centos/8/os/x86_64/ |
替换步骤:
使用文本编辑器打开镜像源配置文件:
sudo vi /etc/yum.repos.d/CentOS-Base.repo
删除原有的镜像源配置,并粘贴以下内容(以网易163为例):
[base] name=CentOS Linux $releasever - Base mirrorlist=http://mirrors.163.com/mirrors/centos/mirrorlist?release=8&arch=x86_64&repo=base&infra=client baseurl=http://mirrors.163.com/centos/8/os/x86_64/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 [epel] name=CentOS Linux $releasever - EPEL (Extra Packages for Enterprise Linux) mirrorlist=http://mirrors.163.com/mirrors/centos/mirrorlist?release=8&arch=x86_64&repo=epel&infra=client baseurl=http://mirrors.163.com/centos/8/epel/x86_64/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
保存并关闭文件。
更新本地缓存
sudo yum makecache
FAQs
Q1:更换镜像源后,如何验证是否成功?
A1:您可以通过以下命令查看当前使用的镜像源:
yum repolist enabled
如果看到新的镜像源地址,则说明更换成功。

Q2:更换镜像源后,如何恢复到默认的官方源?
A2:您可以通过以下步骤恢复到默认的官方源:
使用文本编辑器打开镜像源配置文件:
sudo vi /etc/yum.repos.d/CentOS-Base.repo
删除自定义的镜像源配置,并粘贴以下内容:
[base] name=CentOS-7 - Base mirrorlist=http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=base&infra=client enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 [epel] name=CentOS-7 - EPEL (Extra Packages for Enterprise Linux) mirrorlist=http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=epel&infra=client enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
保存并关闭文件。
更新本地缓存:
sudo yum makecache

