Yum Repository详解
什么是YUM
YUM(Yellowdog Updater Modified)是Fedora、CentOS、RedHat中的軟件包管理器。基于 RPM 包管理,YUM通過分析RPM header數(shù)據(jù),自動處理依賴關系,從指定服務器自動下載安裝所有依賴的軟件包。
常用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] [...]
當不指定參數(shù)時,升級所有軟件。
檢查更新
# yum check-update
查詢信息
# yum info [...]
查看可用的RPM包
# yum list [...]
列出已安裝的包
# yum list installed
搜索軟件
# yum search string1 [string2] [...]
清除緩存
# yum clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]
下載并使metadata可用
# yum makecache [fast]
常用的開發(fā)工具包
安裝編譯器:
# yum install -y gcc gcc-c++ libstdc++ libstdc++-devel
安裝Development Tools:
# yum groupinstall “Development Tools”
如出現(xiàn)there is no installed groups file錯誤,請附加參數(shù)--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,如您不慎誤刪可執(zhí)行如下命令重新安裝:
#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
從文件頭的說明我們知道,鏡像系統(tǒng)使用客戶端的IP地址和每個鏡像的更新狀態(tài)來選取在地理位置上靠近客戶端的鏡像,如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 MTransaction Summary
=======================================================================================================================
參數(shù)說明:
[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的路徑,可以為本地路徑,也可以為網(wǎng)絡路徑
$releasever 發(fā)行版本,可執(zhí)行yum whatprovides 'system-release(releasever)' centos-release或yum version查看
$basearch 系統(tǒng)架構
$infra 其值可查看文件/etc/yum/vars/infra,此參數(shù)目前尚未使用。
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,是否執(zhí)行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據(jù)此判斷發(fā)行版本來設置配置文件中的$releasever,可執(zhí)行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目錄創(chuàng)建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依賴關系等數(shù)據(jù)。
如不知道哪個Repository有您需要的rpm,可通過以下網(wǎng)站查找:
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。
先執(zhí)行如下命令mount iso:
# mount -o loop CentOS-7-x86_64-DVD-1804.iso /mnt
然后創(chuàng)建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
- 創(chuàng)建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
- 創(chuàng)建/更新repodata
# createrepo --update /repository/centos/7/extras/x86_64/
- 安裝配置Apache
若要從外部訪問Repository需安裝apache并配置目錄/repository。
# yum install httpd
# systemctl enable httpd
可以簡單的編輯/etc/httpd/conf/httpd.conf,將DocumentRoot改為/repository,如下:
DocumentRoot "/repository"
<Directory "/repository">Options Indexes FollowSymLinksAllowOverride NoneRequire all granted
</Directory>
如果系統(tǒng)啟用了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修改重啟系統(tǒng)后會失效,如要永久性修改上下文類型,使用 semanage fcontext 和 restorecon 命令:
# semanage fcontext -a -t httpd_sys_content_t "/repository(/.*)?"
# restorecon -R -v /repository
啟動apche:
# systemctl start httpd
- 使用自己的Repository,創(chuàng)建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
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 環(huán)境下網(wǎng)絡服務設置 -- 配置 Apache、Samba、NFS
轉載于:https://blog.51cto.com/7308310/2339182
總結
以上是生活随笔為你收集整理的Yum Repository详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle 中文脚本,ORACLE常用
- 下一篇: 在Linux系统里安装Virtual B