这里我整理一下国内较知名的镜像站点,供大家在生产测试环境中使用。
你可能还需要看:Debian10常用国内源
一、基础知识
红帽系的源一般都是由
/etc/yum.repos.d/
目录下的repo文件进行配置,默认文件结构为:
[root@localhost yum.repos.d]# ll
总用量 32
-rw-r--r--. 1 root root 1664 9月 5 2019 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 9月 5 2019 CentOS-CR.repo
-rw-r--r--. 1 root root 649 9月 5 2019 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 9月 5 2019 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 9月 5 2019 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 9月 5 2019 CentOS-Sources.repo
-rw-r--r--. 1 root root 6639 9月 5 2019 CentOS-Vault.repo
[root@localhost yum.repos.d]#
然后查看一下CentOS-Base.repo文件的内容
[root@localhost yum.repos.d]# cat 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
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
可以看到,此时的镜像地址是
http://mirrorlist.centos.org
此类源一般来说都是比较慢的,下面开始说明如何快速的修改源
二、如何修改
2.1、备份
在Linux中,配置任何文件之前都建议先进行备份,下面是备份命令
mkdir -p /yum_bak&&mv /etc/yum.repos.d/* /yum_bak/
2.2、添加
备份完成之后,开始添加/编辑源文件,添加源文件主要有三种方法
- 直接编辑内容
- 下载rpeo源文件
- yum安装相关源
首先讲解第一个
2.2.1、直接编辑
使用下面的命令写入配置内容
cat <<EOF > /etc/yum.repos.d/CentOS-Base.repo
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[updates]
name=CentOS-\$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/\$releasever/updates/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
EOF
执行效果如下:
2.2.2、下载源文件
这个就比较省事了,下载下来就能用,命令如下:
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
由于阿里云的源默认接入内网域名,所以如果不是阿里云服务器的话,需要执行下面的语句进行相关修改
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
2.2.3、这个就不讲了
三、快速替换源站
在使用前面的方式下载好了源配置文件之后,我们就可以针对这些模板进行快速的替换了,下面先附上一个模板,然后附上各大国内源站
3.1、源模板
直接执行下面的指令即可
echo '[base]
name=CentOS-$releasever - Base
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' > /etc/yum.repos.d/CentOS-Base.repo
3.2、国内常用源站列表
**
PS:下面的指令仅限执行了3.1的指令之后的模板有效,如果需要切换不同的源,请重新执行3.1步骤的指令下执行替换
**
网易源
http://tel.mirrors.163.com
sed -i 's#http://mirrors.aliyun.com/#http://tel.mirrors.163.com/#g' /etc/yum.repos.d/CentOS-Base.repo
腾讯源
https://mirrors.cloud.tencent.com/
sed -i 's#http://mirrors.aliyun.com/#https://mirrors.cloud.tencent.com/#g' /etc/yum.repos.d/CentOS-Base.repo
清华大学
https://mirrors.tuna.tsinghua.edu.cn/
sed -i 's#http://mirrors.aliyun.com/#https://mirrors.tuna.tsinghua.edu.cn/#g' /etc/yum.repos.d/CentOS-Base.repo
中国科技技术大学
https://mirrors.ustc.edu.cn/
sed -i 's#http://mirrors.aliyun.com/#https://mirrors.ustc.edu.cn/#g' /etc/yum.repos.d/CentOS-Base.repo
华为镜像站
https://mirrors.huaweicloud.com/
sed -i 's#http://mirrors.aliyun.com/#https://mirrors.huaweicloud.com/#g' /etc/yum.repos.d/CentOS-Base.repo