将Centos的yum源更换为国内的阿里云源


在使用centos系统进行软件开发或服务器管理时,我们经常需要安装或更新各种软件包。而yum(Yellowdog Updater Modified)是centos中非常便捷的工具,用于自动获取、配置、安装和更新计算机上的软件包。但随着时间的推移,默认的yum源可能变得不再那么可靠或快速。这时,将yum源更换为国内的镜像站点(如阿里云)可以大大提高软件包下载的速度和稳定性。

以下是针对不同版本的centos更换yum源的详细步骤:

一、备份原有的yum源文件

首先,我们需要备份系统原有的yum源文件,以便在必要时可以恢复到原始状态。执行以下命令

mv /etc/yum.repos.d/centos-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

二、下载并替换新的yum源文件

根据您的centos版本,选择相应的命令下载并替换新的yum源文件。

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

由于各大镜像站(如阿里、163、清华等)已停止维护centos 6的yum源镜像,因此建议使用以下方法:

直接编辑/etc/yum.repos.d/centos-Base.repo文件,清空所有内容,并替换为以下代码:

vi /etc/yum.repos.d/Centos-Base.repo

然后粘贴以下内容:

[base]  name=CentOS-6  failovermethod=priority  baseurl=https://vault.centos.org/6.9/os/x86_64/  gpgcheck=0
bash复制代码wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

对于centos 8,我们需要更新三个yum源文件。首先进入yum源配置文件目录:

cd /etc/yum.repos.d

然后备份这三个源文件:

cp CentOS-Base.repo CentOS-Base.repo.bak    cp CentOS-AppStream.repo CentOS-AppStream.repo.bak     cp CentOS-Extras.repo CentOS-Extras.repo.bak

接下来,修改这三个文件中的地址,将默认的镜像地址替换为阿里云的镜像地址:

sed -i 's/mirrorlist=/#mirrorlist=/g' CentOS-Base.repo CentOS-AppStream.repo CentOS-Extras.repo    sed -i 's/#baseurl=/baseurl=/g' CentOS-Base.repo CentOS-AppStream.repo CentOS-Extras.repo    sed -i 's/http://mirror.centos.org/https://mirrors.aliyun.com/g' CentOS-Base.repo CentOS-AppStream.repo CentOS-Extras.repo

三、清空并重新生成yum缓存

完成上述步骤后,我们需要清空原有的yum缓存并重新生成新的缓存,以确保系统使用新的yum源进行软件包下载和更新。执行以下命令

yum clean all    yum makecache

现在,您的centos系统已经成功更换为阿里云的yum源,可以享受到更快速、更稳定的软件包下载和更新服务了!

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享