本文最后更新于 2023-05-14,文章内容可能已经过时。

DNF/YUM源配置文件替换为阿里的

由于系统安装的包管理配置文件链接的国外的服务器,导致我们安装软件、升级内核和升级软件的时候会从国外的服务器下载相关文件。由于众所周知的原因,国外服务器的网速真的不敢恭维,所以我们要把他们替换为国内的服务器,这样安装和升级软件的速度就会提高,降低维护人员在等待上所花费的时间。
因为阿里源文件里面已经包含了AppStream、Base、centosplus、Extras和PowerTools的相关内容,所以需要把这些文件改名为bak,不让系统执行。

cd /etc/yum.repos.d/
mv /etc/yum.repos.d/CentOS-AppStream.repo /etc/yum.repos.d/CentOS-AppStream.repo.bak
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
mv /etc/yum.repos.d/CentOS-centosplus.repo /etc/yum.repos.d/CentOS-centosplus.repo.bak
mv /etc/yum.repos.d/CentOS-Extras.repo /etc/yum.repos.d/CentOS-Extras.repo.bak
mv /etc/yum.repos.d/CentOS-PowerTools.repo /etc/yum.repos.d/CentOS-PowerTools.repo.bak

下载阿里云的源文件

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

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

如果没有安装wget的,运行这个命令会提示“bash: wget: 未找到命令”,那就用curl的那个命令来执行好了。或者你也可以先安装wget。

yum -y install wget

# 使用这个命令的前提是在将上面的文件改为“.bak”之前就运行,如果已经改了,先改回去再执行下述命令

查看一下是否安装完成。

ls -l /etc/yum.repos.d/

执行完成之后你会看到以下信息注明已经正确安装了

总用量 76
-rw-r--r--. 1 root root  731 9月  16 04:10 CentOS-AppStream.repo.bak
-rw-r--r--  1 root root 2595 12月 19 2019 CentOS-Base.repo
-rw-r--r--. 1 root root 2593 11月 20 13:08 CentOS-Base.repo.bak
-rw-r--r--. 1 root root  798 9月  16 04:10 CentOS-centosplus.repo.bak
-rw-r--r--. 1 root root 1043 9月  16 04:10 CentOS-CR.repo
-rw-r--r--. 1 root root  668 9月  16 04:10 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  743 9月  16 04:10 CentOS-Devel.repo
-rw-r--r--. 1 root root  756 9月  16 04:10 CentOS-Extras.repo.bak
-rw-r--r--. 1 root root  338 9月  16 04:10 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  738 9月  16 04:10 CentOS-HA.repo
-rw-r--r--. 1 root root  928 9月  16 04:10 CentOS-Media.repo
-rw-r--r--. 1 root root  736 9月  16 04:10 CentOS-PowerTools.repo.bak
-rw-r--r--. 1 root root 1382 9月  16 04:10 CentOS-Sources.repo
-rw-r--r--. 1 root root   74 9月  16 04:10 CentOS-Vault.repo
-rw-r--r--. 1 root root 1167 12月 19 2019 epel-modular.repo
-rw-r--r--. 1 root root 1249 12月 19 2019 epel-playground.repo
-rw-r--r--. 1 root root 1104 12月 19 2019 epel.repo
-rw-r--r--. 1 root root 1266 12月 19 2019 epel-testing-modular.repo
-rw-r--r--. 1 root root 1203 12月 19 2019 epel-testing.repo

再执行以下命令查看一下内容,确认是否更改成功

cat /etc/yum.repos.d/CentOS-Base.repo

如果看到如下内容,则代表替换成功。

# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/$releasever/BaseOS/$basearch/os/
        http://mirrors.aliyuncs.com/centos/$releasever/BaseOS/$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/os/
        http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/os/
        http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

[PowerTools]
name=CentOS-$releasever - PowerTools - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/$releasever/PowerTools/$basearch/os/
        http://mirrors.aliyuncs.com/centos/$releasever/PowerTools/$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/PowerTools/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official


[AppStream]
name=CentOS-$releasever - AppStream - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/$releasever/AppStream/$basearch/os/
        http://mirrors.aliyuncs.com/centos/$releasever/AppStream/$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/AppStream/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

这样就完成了将yum安装源更换为国内源(阿里源)。

运行以下命令生成以下缓存

yum makecache

执行以下命令升级所有的软件包看一下速度如何:

yum -y update