新手入门必须的CentOS 7更换yum源教程

简介

有些yum源确实不好用,甚至更新某些软件都无法更新或者下载,国内的阿里云、163、腾讯云的yum源就不用多

说了,我们就直接更换yum源,不管是你用国内源还是官方源,这个任意选择,这里只提供一个换源的教学,让新手学的

更简单,更容易。

 

 

教程

备份

备份当前yum源配置,必须得操作,避免造成倒腾坏的情况

#进入yum源文件夹
cd /etc/yum.repos.d/

#批量把repo的文件改名进行备份
rename repo repo.old CentOS-*.repo

备份前和后,我们可以看到凡是CentOs-*.repo的已经全被批量改名后面加上了  .old后缀

 

更换官方yum源

修改repo文件

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

官方源复制进去,保存后,重建缓存

[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

安装官方epel源,安装完后会有:

epel.repo(正式版,所有的软件都是稳定可以信赖的)

epel-testing.repo(测试版)

想试试测试版的话,需要修改/etc/yum.repos.d/epel-testing.repo,把enabled=0改成enabled=1

yum install epel-release

官方epel源内容

[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place its address here.
#baseurl=http://download.example/pub/epel/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch&infra=$infra&content=$contentdir
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place its address here.
#baseurl=http://download.example/pub/epel/7/$basearch/debug
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch&infra=$infra&content=$contentdir
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place it's address here.
#baseurl=http://download.example/pub/epel/7/source/tree/
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch&infra=$infra&content=$contentdir
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

 

阿里云yum源

阿里云的yum源

wget http://mirrors.aliyun.com/repo/Centos-7.repo

阿里云的epel源

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

 

腾讯yum源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo

 

163yum源

wget http://mirrors.163.com/.help/CentOS7-Base-163.repo

 

由于系统中配置了两个源,在yum安装软件时,默认优先会使用epel的源,如果想要优先使用CentOS的源,就要

安装一个优先级插件:yum-plugin-priorities,进行控制调整2个yum源的优先级,以阿里云的举例

#安装插件
yum install yum-plugin-priorities

vim /etc/yum.repo.d/epel7.repo 在[epel]里的最后一行添加
priority=88

vim /etc/yum.repo.d/Centos-7.repo

在[base]里的最后一行添加priority=6 (这个数要小于epel里的88即可,数字越小优先级越高)

 

 

重建缓存

清理缓存并生成新的缓存

yum clean all

yum makecache

#也可以使用省事的一条命令执行

yum clean all && yum makecache

 

查看yum源

查看系统中yum源和所有的yum源

yum repolist enabled

THE END