什么是YUM
YUM(Yellowdog Updater Modified)是Fedora、CentOS、RedHat中的软件包管理器。基于 RPM 包管理,YUM通过分析RPM header数据,自动处理依赖关系,从指定服务器自动下载安装所有依赖的软件包。
常用yum命令
安装软件
# yum install package1 [package2] [...]
重装软件
# yum reinstall package1 [package2] [...]
删除软件
# yum remove package1 [package2] [...] 或 # yum erase package1 [package2] [...]
升级软件
# yum update [package1] [package2] [...] 或 # yum upgrade [package1] [package2] [...]
当不指定参数时,升级所有软件。
检查更新
# yum check-update
查询信息
# yum info [...]
查看可用的RPM包
# yum list [...]
搜索软件
# yum search string1 [string2] [...]
清除缓存
# yum clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]
下载并使metadata可用
# yum makecache [fast]
使用yum安装软件出现问题时,可试一下后两个命令:
# yum clean all # yum makecache
YUM Repository
我们要使用YUM,必须要先找到适合的YUM Server,配置YUM Repositry。CentOS有很多的镜像站点供全世界软件更新之用。CentOS安装后已配置好这些Yum Repository,配置文件位于/etc/yum.repos.d目录下,文件扩展名为repo,如您不慎误删可执行如下命令重新安装:
#rpm -Uvh http://mirror.centos.org/centos/7/os/x86_64/Packages/centos-release-7-6.1810.2.el7.centos.x86_64.rpm
repo文件
下面我们看一下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=$baasearch&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
从文件头的说明我们知道,镜像系统使用客户端的IP地址和每个镜像的更新状态来选取在地理位置上靠近客户端的镜像,如mirrorlist不工作时可以使用baseurl。
使用Yum安装软件时可以看到用了哪些镜像:
Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn
使用了哪些Repository:
======================================================================================================================= Package Arch Version Repository Size ======================================================================================================================= Removing: vim-enhanced x86_64 2:7.4.160-5.el7 @base 2.2 M Transaction Summary =======================================================================================================================
参数说明:
[repositoryid] 仓库ID,名称任意但不能重复
baseurl yum仓库URL,其下必须含有`repodata'目录,可以是http://、ftp:// 或 file:// URL。一个baseurl可以指定多个URL:
baseurl=url://server1/path/to/repository/ url://server2/path/to/repository/ url://server3/path/to/repository/
mirrorlist 指向一文件的URL,其内容为baseurl列表
enabled 是否启用这个仓库,0表示不启用,1表示启用,默认启用。如为安装某个软件自己配置了一个仓库,但又要不影响其他软件安装,可以设为0,安装这个软件时再启用:yum --enablerepo=repoid
gpgcheck 是否进行签名合法性检测,0表示不启用,1表示启用,默认启用
gpgkey gpgkey的路径,可以为本地路径,也可以为网络路径
$releasever 发行版本,可执行yum whatprovides 'system-release(releasever)' centos-release或yum version查看
$basearch 系统架构
$infra 其值可查看文件/etc/yum/vars/infra,此参数目前尚未使用。
CentOS 7,$releasever值为7,$basearch值为x86_64,$infra值为stock,替换后extras mirrorlist的值为:
http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock
在浏览器中查看输出如下结果:
http://mirrors.njupt.edu.cn/centos/7.6.1810/extras/x86_64/ http://mirrors.cn99.com/centos/7.6.1810/extras/x86_64/ http://mirrors.nwsuaf.edu.cn/centos/7.6.1810/extras/x86_64/ http://mirror.lzu.edu.cn/centos/7.6.1810/extras/x86_64/ http://mirrors.nju.edu.cn/centos/7.6.1810/extras/x86_64/ http://mirrors.aliyun.com/centos/7.6.1810/extras/x86_64/ http://mirrors.huaweicloud.com/centos/7.6.1810/extras/x86_64/ http://mirrors.neusoft.edu.cn/centos/7.6.1810/extras/x86_64/ http://mirrors.tuna.tsinghua.edu.cn/centos/7.6.1810/extras/x86_64/ http://mirrors.cqu.edu.cn/CentOS/7.6.1810/extras/x86_64/
yum.conf
Yum配置文件/etc/yum.conf,可使用man yum.conf查看各配置项的意义。
# 定义全局配置选项 [main] # yum缓存目录 cachedir=/var/cache/yum/$basearch/$releasever # 1或0,安装成功后是否保存header和package keepcache=0 # debug message输出级别,0 - 10 debuglevel=2 logfile=/var/log/yum.log exactarch=1 obsoletes=1 # 1或0,是否执行gpg签名检查 gpgcheck=1 # 1或0,是否启用yum plugins plugins=1 installonly_limit=5 bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum # 指定一个包,Yum据此判断发行版本来设置配置文件中的$releasever,可执行yum whatprovides 'system-release(releasever)' centos-release或yum version查看 distroverpkg=centos-release # This is the default, if you make this bigger yum won't see if the metadata # is newer on the remote and so you'll "gain" the bandwidth of not having to # download the new metadata and "pay" for it by yum not having correct # information. # It is esp. important, to have correct metadata, for distributions like # Fedora which don't keep old packages around. If you don't like this checking # interupting your command line usage, it's much better to have something # manually check the metadata once an hour (yum-updatesd will do this). # metadata_expire=90m # PUT YOUR REPOS HERE OR IN separate files named file.repo # in /etc/yum.repos.d
配置Repository
有时我们安装的软件不能从官方Repository获得,需要使用其它的Repository,比如:
EPEL Repository Mirrors
ELRepo Repository
Repository站点一般都提供了rpm包,安装后会自动配置repo。
EPEL CentOS/RHEL 7
# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
EPEL CentOS/RHEL 6
# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
ELRepo CentOS/RHEL 7/6
# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org # rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm # rpm -Uvh https://www.elrepo.org/elrepo-release-6-8.el6.elrepo.noarch.rpm
或自己在/etc/yum.repos.d目录创建repo文件,比如
[ali-extras] name=ali centos7 extras baseurl=https://mirrors.aliyun.com/centos/7/extras/x86_64/ enable=1 gpgcheck=0 gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
注意baseurl地址下必须含有repodata文件夹,repodata中保存了rpm依赖关系等数据。
如不知道哪个Repository有您需要的rpm,可通过以下网站查找:
Package search
RPM Search
RPM Find
Redhat订阅
默认,Redhat仅base repository是可用的,若要使用其它repository需要订阅后启用,如下:
subscription-manager register subscription-manager list --available subscription-manager attach --pool=8a85f98b62dd96fc0162f04efb0e6350 subscription-manager repos --enable rhel-7-server-extras-rpms subscription-manager repos --enable rhel-7-server-rpms
搭建Repository
CentOS ISO镜像作为Repository
CentOS ISO镜像中含有大量的RPM包,可以用作Repository。
先执行如下命令mount iso:
# mount -o loop CentOS-7-x86_64-DVD-1804.iso /mnt
然后创建repo文件,如下:
/etc/yum.repos.d/centos-iso.repo
[centosiso] name=CentOS DVD ISO baseurl=file:///mnt enabled=1 gpgcheck=1 gpgkey=file:///mnt/RPM-GPG-KEY-CentOS-7
搭建自己的Repository
搭建自己的Repository可以避免重复下载rpm,也可以存放公司内部的rpm。搭建步骤如下:
- 安装createrepo
# yum install createrepo
- 创建Repository目录
# mkdir /repository
- 获取rpm
同步Repository:
# reposync -d --norepopath -r extras -p /repository/centos/7/extras/x86_64/
获取单独的rpm可使用yumdownloader,比如:
# yumdownloader --destdir /repository/centos/7/extras/x86_64/Packages python-docker-py
- 创建/更新repodata
# createrepo --update /repository/centos/7/extras/x86_64/
- 创建repo文件
/etc/yum.repos.d/my.repo[myextras] name=CentOS-7 - Extras baseurl=file:///repository/centos/7/extras/x86_64/ enabled=1 gpgcheck=0
资源站点
YUM
List of CentOS Mirrors
清华大学开源软件镜像站
华为开源镜像站
阿里巴巴开源镜像站
EPEL Repository Mirrors
ELRepo Repository
Package search
RPM Search
RPM Find