该文件夹只提供 CentOS 7 与 8,架构仅为x86_64 ,如果需要较早版本的 CentOS,请参考 centos-vault 的帮助,若需要其他架构,请参考 centos-altarch 的帮助。
建议先备份 /etc/yum.repos.d/ 内的文件(CentOS 7 及之前为 CentOS-Base.repo,CentOS 8 为CentOS-Linux-*.repo)
[root@dev workspace]# mkdir -p /etc/yum.repos.d.bak[root@dev workspace]# cp -rf /etc/yum.repos.d/*.* /etc/yum.repos.d.bak/[root@dev workspace]# ls /etc/yum.repos.d.bak/CentOS-Base.repo CentOS-Linux-Devel.repo CentOS-Linux-Plus.repoCentOS-Linux-AppStream.repo CentOS-Linux-Extras.repo CentOS-Linux-PowerTools.repoCentOS-Linux-BaseOS.repo CentOS-Linux-FastTrack.repo CentOS-Linux-Sources.repoCentOS-Linux-ContinuousRelease.repo CentOS-Linux-HighAvailability.repoCentOS-Linux-Debuginfo.repo CentOS-Linux-Media.repo[root@dev workspace]#
然后编辑 /etc/yum.repos.d/ 中的相应文件,在 mirrorlist= 开头行前面加 # 注释掉;并将 baseurl= 开头行取消注释(如果被注释的话),把该行内的域名(例如mirror.centos.org)替换为 mirrors.tuna.tsinghua.edu.cn。
以上步骤可以被下方的命令一步完成
[root@dev workspace]# ls /etc/yum.repos.d/CentOS-Linux-AppStream.repo CentOS-Linux-Debuginfo.repo CentOS-Linux-FastTrack.repo CentOS-Linux-Plus.repoCentOS-Linux-BaseOS.repo CentOS-Linux-Devel.repo CentOS-Linux-HighAvailability.repo CentOS-Linux-PowerTools.repoCentOS-Linux-ContinuousRelease.repo CentOS-Linux-Extras.repo CentOS-Linux-Media.repo CentOS-Linux-Sources.repo[root@dev workspace]# cat /etc/yum.repos.d/CentOS-Linux-BaseOS.repo# CentOS-Linux-BaseOS.repo## The mirrorlist system uses the connecting IP address of the client and the# update status of each mirror to pick current mirrors that are 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, you can try the commented out# baseurl line instead.[baseos]name=CentOS Linux $releasever - BaseOSmirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra#baseurl=http://mirror.centos.org/$contentdir/$releasever/BaseOS/$basearch/os/gpgcheck=1enabled=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial[root@dev workspace]#
sed -e 's|^mirrorlist=|#mirrorlist=|g' \-e 's|^#baseurl=http://mirror.centos.org/altarch/|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/|g' \-e 's|^#baseurl=http://mirror.centos.org/$contentdir/|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/|g' \-i.bak \/etc/yum.repos.d/CentOS-*.repo
[root@dev workspace]# cat /etc/yum.repos.d/CentOS-Linux-BaseOS.repo

注意其中的*通配符,如果只需要替换一些文件中的源,请自行增删。
注意,如果需要启用其中一些 repo,需要将其中的
enabled=0改为enabled=1。
最后,更新软件包缓存
[root@dev workspace]# yum clean all //清除缓存45 files removedyum clean all[root@dev workspace]# yum makecacheCentOS Linux 8 - AppStream 2.1 MB/s | 8.4 MB 00:04CentOS Linux 8 - BaseOS 916 kB/s | 4.6 MB 00:05CentOS Linux 8 - Extras 3.7 kB/s | 10 kB 00:02Metadata cache created.[root@dev workspace]#
