1. 备份原有的 yum 源配置

在更改源之前,建议先备份现有的 yum 源配置文件,以防需要恢复:

sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

2. 更换为阿里云源

可以使用以下命令直接下载并替换为阿里云提供的 CentOS-Base.repo

sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

或者,如果你使用的是 CentOS 8:

sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo

3. 更换为清华源

同样,可以使用以下命令直接下载并替换为清华大学提供的 CentOS-Base.repo

  • 对于 CentOS 7:

    sudo wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tuna.tsinghua.edu.cn/repo/Centos-7.repo
  • 对于 CentOS 8:

    sudo wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tuna.tsinghua.edu.cn/repo/Centos-8.repo

4. 清理缓存并更新源

下载并替换了新的 .repo 文件后,执行以下命令来清理 yum 缓存并更新:

sudo yum clean all
sudo yum makecache

5. 验证源是否更换成功

最后,运行以下命令以确认新的源配置已经生效,并且可以正常使用:

sudo yum repolist

如果命令输出中显示了来自阿里云或清华大学镜像站点的源列表,说明源更换成功。

总结

通过上述步骤,你可以将 CentOS 的默认软件源更换为国内的阿里云源或清华源,以提高软件包下载的速度。这对于在国内使用 CentOS 服务器非常有帮助。