1. 备份原来的yum源

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

2.设置aliyun的yum源

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

3.添加EPEL源

EPEL(http://fedoraproject.org/wiki/EPEL)是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。装上 EPEL后,可以像在 Fedora 上一样,可以通过 yum install package-name,安装更多软件。

sudo wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo  

4.清理缓存并生成新的缓存

1、

sudo yum clean all  

2、

sudo yum makecache 

参考2:

你可以按照以下步骤将 CentOS 7 的 YUM 软件源更改为阿里云的镜像源:

1、先备份原有的 CentOS-Base.repo 文件

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

2、下载阿里云的 CentOS 7 的 repo 文件

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

3、清除并生成新的缓存

sudo yum clean all
sudo yum makecache

现在你的 CentOS 7 的 yum 软件源应该已经被更改为了阿里云的镜像源,你可以尝试安装或者更新软件包看看是否成功。

这个过程假设你的系统有 wget 命令,如果没有,你可以先用

yum install wget -y

命令安装。同时,这个过程需要有相应的网络访问权限,否则无法从阿里云下载 repo 文件。

参考操作流程

[root@Beta-CentOS7 ~]# sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@Beta-CentOS7 ~]# sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

--2023-09-27 18:52:44-- http://mirrors.aliyun.com/repo/Centos-7.repo

正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 113.240.96.113, 122.225.213.252, 119.96.33.219, ...

正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|113.240.96.113|:80... 已连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:2523 (2.5K) [application/octet-stream]

正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”

100%[==============================================================================================>] 2,523 --.-K/s 用时 0.001s

2023-09-27 18:52:44 (3.45 MB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])

[root@Beta-CentOS7 ~]# sudo yum clean all

已加载插件:fastestmirror, langpacks

正在清理软件源: base extras updates

Cleaning up list of fastest mirrors

[root@Beta-CentOS7 ~]# sudo yum makecache

已加载插件:fastestmirror, langpacks

Determining fastest mirrors

* base: mirrors.aliyun.com

* extras: mirrors.aliyun.com

* updates: mirrors.aliyun.com

http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: mirrors.aliyuncs.com; 未知的错误"

正在尝试其它镜像。

http://mirrors.cloud.aliyuncs.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: mirrors.cloud.aliyuncs.com; 未知的错误"

正在尝试其它镜像。

base | 3.6 kB 00:00:00

extras | 2.9 kB 00:00:00

updates | 2.9 kB 00:00:00

(1/10): base/7/x86_64/group_gz | 153 kB 00:00:00

(2/10): base/7/x86_64/filelists_db | 7.2 MB 00:00:01

(3/10): base/7/x86_64/primary_db | 6.1 MB 00:00:00

(4/10): extras/7/x86_64/filelists_db | 303 kB 00:00:00

(5/10): extras/7/x86_64/primary_db | 250 kB 00:00:00

(6/10): extras/7/x86_64/other_db | 150 kB 00:00:00

(7/10): base/7/x86_64/other_db | 2.6 MB 00:00:00

(8/10): updates/7/x86_64/filelists_db | 13 MB 00:00:01

(9/10): updates/7/x86_64/other_db | 1.4 MB 00:00:00

(10/10): updates/7/x86_64/primary_db | 23 MB 00:00:03

元数据缓存已建立

[root@Beta-CentOS7 ~]# yum repolist

已加载插件:fastestmirror, langpacks

Loading mirror speeds from cached hostfile

* base: mirrors.aliyun.com

* extras: mirrors.aliyun.com

* updates: mirrors.aliyun.com

源标识 源名称 状态

base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,072

extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 518

updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 5,283

repolist: 15,873

[root@Beta-CentOS7 ~]#