Yum Repository詳解

什麼是YUM

YUM(Yellowdog Updater Modified)是Fedora、CentOS、RedHat中的軟件包管理器。基於 RPM 包管理,YUM經過分析RPM header數據,自動處理依賴關係,從指定服務器自動下載安裝全部依賴的軟件包。php

經常使用yum命令

安裝軟件html

# yum install package1 [package2] [...]

重裝軟件python

# yum reinstall package1 [package2] [...]

刪除軟件linux

# yum remove package1 [package2] [...]
或
# yum erase package1 [package2] [...]

升級軟件c++

# yum update [package1] [package2] [...]
或
# yum upgrade [package1] [package2] [...]

當不指定參數時,升級全部軟件。docker

檢查更新apache

# yum check-update

查詢信息vim

# yum info [...]

查看可用的RPM包centos

# yum list [...]

列出已安裝的包瀏覽器

# yum list installed

搜索軟件

# yum search string1 [string2] [...]

清除緩存

# yum clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]

下載並使metadata可用

# yum makecache [fast]

經常使用的開發工具包

安裝編譯器:

# yum install -y gcc gcc-c++ libstdc++ libstdc++-devel

安裝Development Tools:

# yum groupinstall 「Development Tools」

如出現there is no installed groups file錯誤,請附加參數--setopt=group_package_types=mandatory,default,optional。
查看組Development Tools的內容:

# yum groupinfo "Development Tools"

解決yum安裝問題

可先試如下命令:

# yum clean all
# yum makecache

Structure needs cleaning
yum安裝報錯:Structure needs cleaning,可以使用package-cleanup查找有問題的包,刪除後再從新安裝:

# package-cleanup --problems or --leaves or --orphans or --oldkernels
# rpm -e xxxxxxxxx

duplicate錯誤
首先查看是否存在未完成的transaction:

# yum-complete-transaction

而後刪除重複包:

package-cleanup --dupes lists duplicate packages
package-cleanup --cleandupes removes duplicate packages

rpmdb open failed
錯誤信息以下:

error: db5 error(-30973) from dbenv->open: BDB0087 DB_RUNRECOVERY: Fatal error, run database recovery
error: cannot open Packages index using db5 -  (-30973)
error: cannot open Packages database in /var/lib/rpm
CRITICAL:yum.main:

Error: rpmdb open failed

解決方法:

# rm -f /var/lib/rpm/__db*
# rpm --rebuilddb

更多yum工具,請查看yum-utils:

# man yum-utils

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的軟件庫很龐大,AWS EC2僅提供了部分的repo訂閱,可以使用yum repolist all查詢。若要使用其它repository須要訂閱後啓用,以下:

# subscription-manager register
# subscription-manager list --available
# subscription-manager attach --pool=8a85f98b62dd96fc0162f04efb0e6350
# subscription-manager repos --list
# subscription-manager repos --enable rhel-7-server-rpms
# subscription-manager repos --enable rhel-7-server-extras-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。搭建步驟以下:

  1. 安裝createrepo
# yum install createrepo
  1. 建立Repository目錄
# mkdir /repository
  1. 獲取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
  1. 建立/更新repodata
# createrepo --update /repository/centos/7/extras/x86_64/
  1. 安裝配置Apache

若要從外部訪問Repository需安裝apache並配置目錄/repository。

# yum install httpd
# systemctl enable httpd

能夠簡單的編輯/etc/httpd/conf/httpd.conf,將DocumentRoot改成/repository,以下:

DocumentRoot "/repository"
<Directory "/repository">
  Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

若是系統啓用了SELINUX須要修改/repository安全上下文(可經過查看文件/etc/selinux/config或運行命令getenforce來判斷是否啓用了selinux)。

先看一下默認DocumentRoot的安全上下文:

# ls -dZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

查看/repository安全上下文:

# ls -dZ /repository/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /repository/

default_t類型,httpd進程是不能訪問的,可使用 chcon 或 restorecon 命令修改。修改/repository安全上下文類型:

# chcon -R -t httpd_sys_content_t /repository

使用chcon修改重啓系統後會失效,如要永久性修改上下文類型,使用 semanage fcontext 和 restorecon 命令:

# semanage fcontext -a -t httpd_sys_content_t "/repository(/.*)?"
# restorecon -R -v /repository

啓動apche:

# systemctl start httpd
  1. 使用本身的Repository,建立repo文件

/etc/yum.repos.d/my.repo

[myextras]
name=CentOS-7 - Extras
baseurl=file:///repository/centos/7/extras/x86_64/
enabled=1
gpgcheck=0

[myextras]
name=CentOS-7 - Extras
baseurl=http://192.168.0.1/centos/7/extras/x86_64/
enabled=1
gpgcheck=0

2019港百男子前十
Yum Repository詳解

資源站點

YUM
List of CentOS Mirrors
清華大學開源軟件鏡像站
華爲開源鏡像站
阿里巴巴開源鏡像站
EPEL Repository Mirrors
ELRepo Repository
Package search
RPM Search
RPM Find
Top 5 Yum Repositories for CentOS/RHEL 7/6/5
一文完全明白linux中的selinux究竟是什麼
SELinux 環境下網絡服務設置 -- 配置 Apache、Samba、NFS

相關文章
相關標籤/搜索