1. 备份当前yum源防止出现意外还可以还原回来

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

2. 使用wget下载阿里yum源repo文件到/etc/yum.repos.d/目录

使用之前请确保已经安装wget,如未安装请执行下面一条命令来安装 yum install -y wget

centos 6

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

centos 7

阿里源

yum源

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

epel源

wget -O epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

163源

wget -O /etc/yum.repos.dCentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo

清华源

vim CentOS-Base.repo

[base]

name=CentOS-$releasever - Base

baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[updates]

name=CentOS-$releasever - Updates

baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[extras]

name=CentOS-$releasever - Extras

baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[centosplus]

name=CentOS-$releasever - Plus

baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/

gpgcheck=1

enabled=0

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

vim epel.repo源

[epel]

name=Extra Packages for Enterprise Linux 7 - $basearch

baseurl=http://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch

failovermethod=priority

enabled=1

gpgcheck=1

gpgkey=https://mirrors.tuna.tsinghua.edu.cn/epel/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]

name=Extra Packages for Enterprise Linux 7 - $basearch - Debug

baseurl=http://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch/debug

failovermethod=priority

enabled=0

gpgkey=https://mirrors.tuna.tsinghua.edu.cn/epel/RPM-GPG-KEY-EPEL-7

gpgcheck=1

[epel-source]

name=Extra Packages for Enterprise Linux 7 - $basearch - Source

baseurl=http://mirrors.tuna.tsinghua.edu.cn/epel/7/SRPMS

failovermethod=priority

enabled=0

gpgkey=https://mirrors.tuna.tsinghua.edu.cn/epel/RPM-GPG-KEY-EPEL-7

gpgcheck=1

```

centos 8

阿里源

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

3. 清理旧包

yum clean all

4. 生成新的yum源缓存并更新yum源

yum makecache

yum update

5. 非阿里云服务器会出现 Couldn’t resolve host ‘mirrors.cloud.aliyuncs.com’ 信息,不影响使用。

也可使用下面命令解决。

sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS

6. 查看存储库是否更换成功

yum repolist

![image-1679289311865](https://zmzycc.top/upload/2023/03/image-1679289311865.png)