CentOS8官方源和源配置

本CentOS8教程主要介绍CentOS8官方源和源配置,以及源更新。

加载中...
  CentOS8官方源和源配置
更新时间:2022-11-23

一、基本概念

CentOS8官方源分为两部分,一个是CentOS8官方源的main主配置文件,一个是CentOS8官方源的repository配置文件。

  • CentOS8源配置main主文件,一般无需修改,查看命令为:cat /etc/yum.conf
  • CentOS8源配置repository文件,一般是以 .repo 结尾的配置文件,路径为:/etc/yum.repos.d/


二、CentOS8官方源介绍

CentOS8默认是去读官方centos.org的mirrorlist,如果你的网络访问有问题,则需要选择第三方CentOS8源配置。


2.1、查看CentOS8官方源

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


CentOS8官方源默认信息如下:

[BaseOS]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial


官方CentOS8源配置文件详细解析:

  1. serverid:是用于区别各个不同的repository,必须有一个独一无二的名称,例如上:BaseOS;
  2. name:是对repository 的描述,支持像$releasever $basearch这样的变量;
  3. mirrorlist:镜像列表地址,多个获取更新的地址集合,系统会自动选择合适的地址进行更新;
  4. baseurl:基础地址,CentOS8默认是关闭的,默认开启mirrorlist;
  5. gpgcheck:有0和1两个值,与main中相同,即是否需要对获取的软件包进行GPG校验;
  6. enabled:决定这个配置是否可用,默认为1,可用,设置为0时该配置无效;
  7. gpgkey:用于校验的gpg密钥。


2.2、备份CentOS8官方源

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


三、第三方CentOS8源配置

在这里,我们可以选择阿里云的源或者清华大学的源并进行CentOS8源配置操作,在本CentOS8教程中您可以参考此流程,进行切换其它的第三方的源。


3.1、选择阿里的CentOS8源配置

我们此处展示修改一个CentOS-AppStream.repo文件,在本CentOS8教程中同理参考可以修改CentOS-Base.repo和CentOS-Extras.repo等其它文件。

#首先,切换到源文件的目录
cd /etc/yum.repos.d/
#然后,备份三个主要的源文件
cp CentOS-AppStream.repo CentOS-AppStream.repo.bak
cp CentOS-Base.repo CentOS-Base.repo.bak
cp CentOS-Extras.repo CentOS-Extras.repo.bak


修改CentOS-AppStream.repo。

#打开文件
vi /etc/yum.repos.d/entOS-AppStream.repo
#找到
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/AppStream/$basearch/os/
#修改为:
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra
baseurl=http://mirrors.aliyun.com/centos/$releasever/AppStream/$basearch/os


最后对CentOS8源配置进行更新,更新软件包缓存。

dnf makecache


3.2、选择清华大学的CentOS8源配置

在本CentOS8教程中,建议先备份 CentOS-Base.repo。

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


然后编辑文件,打开 /etc/yum.repos.d/CentOS-Base.repo 在 mirrorlist= 开头行前面加 # 注释掉;并将 baseurl= 开头行取消注释(如果被注释的话),把该行内的域名(例如mirror.centos.org)替换为 mirrors.tuna.tsinghua.edu.cn。

或者,你也可以直接使用如下内容覆盖掉 /etc/yum.repos.d/CentOS-Base.repo 文件:

# CentOS-Base.repo
#
[BaseOS]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/BaseOS/$basearch/os/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[AppStream]
name=CentOS-$releasever - AppStream
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/AppStream/$basearch/os/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[PowerTools]
name=CentOS-$releasever - PowerTools
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/PowerTools/$basearch/os/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=PowerTools&infra=$infra
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/os/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/os/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial


最后进行源更新,更新软件包缓存。

dnf makecache


3.3、系统升级和软件服务更新

dnf -y update


小提示:试试吧全栈教程系列之 - 《CentOS8教程》跟随软件版本的更新不断完善和优化,本教程主打原创、全部免费,欢迎学习和转载,与人方便、科技向善,敬请 收藏和分享 试试吧,谢谢。