设置CentOS yum源及第三方资源库
2010-08-09 12:38:46 阿炯

CentOS yum的使用方法

yum是什么

yum = Yellow dog Updater, Modified
主要功能是更方便的添加/删除/更新RPM包,它能自动解决包的倚赖性问题,它能便于管理大量系统的更新问题。

yum特点
可以同时配置多个资源库(Repository)
简洁的配置文件(/etc/yum.conf)
自动解决增加或删除rpm包时遇到的倚赖性问题
使用方便
保持与RPM数据库的一致性

yum安装
CentOS自带(yum-*.noarch.rpm)
#rpm -ivh yum-*.noarch.rpm
在第一次启用yum之前首先需要导入系统的RPM-GPG-KEY:
#rpm --import /usr/share/doc/centos-release-5(4)/RPM-GPG-KEY-CentOS-5(4)

yum指令
注:当第一次使用yum或yum资源库有更新时,yum会自动下载所有所需的headers放置于/var/cache/yum目录下,所需时间可能较长。

配置文件yum.conf内置的变量说明:
Sreleasever    代表系统发行版本号
Sarch    代表CPU架构
$basearch    代表系统架构
$YUMO-9    使用Shell对应的0~9个同名变量替换

rpm包的更新
检查可更新的rpm包
#yum check-update
更新所有的rpm包
#yum update
更新指定的rpm包,如更新kernel和kernel source
#yum update kernel kernel-source
大规模的版本升级,与yum update不同的是,连旧的淘汰的包也升级
#yum upgrade
查看yum源列表
#yum repolist

rpm包的安装和删除
安装rpm包,如xmms-mp3
#yum install xmms-mp3
删除rpm包,包括与该包有倚赖性的包
#yum remove licq
注:同时会提示删除licq-gnome,licq-qt,licq-text

yum暂存(/var/cache/yum/)的相关参数
清除暂存中rpm包文件
#yum clean packages
清除暂存中rpm头文件
#yum clearn headers
清除暂存中旧的rpm头文件
#yum clean oldheaders
清除暂存中旧的rpm头文件和包文件
#yum clearn 或 #yum clearn all (清除所有缓存)
注:相当于yum clean packages + yum clean oldheaders
#yum makecache 建立缓存

包列表
列出资源库中所有可以安装或更新的rpm包
#yum list
列出资源库中特定的可以安装或更新以及已经安装的rpm包
#yum list mozilla#yum list mozilla*
注:可以在rpm包名中使用匹配符,如列出所有以mozilla开头的rpm包
列出资源库中所有可以更新的rpm包
#yum list updates
列出已经安装的所有的rpm包
#yum list installed
列出已经安装的但是不包含在资源库中的rpm包
#yum list extras
注:通过其它网站下载安装的rpm包

rpm包信息显示(info参数同list)
列出资源库中所有可以安装或更新的rpm包的信息
#yum info
列出资源库中特定的可以安装或更新以及已经安装的rpm包的信息
#yum info mozilla#yum info mozilla*
注:可以在rpm包名中使用匹配符,如列出所有以mozilla开头的rpm包的信息
列出资源库中所有可以更新的rpm包的信息
#yum info updates
列出已经安装的所有的rpm包的信息
#yum info installed
列出已经安装的但是不包含在资源库中的rpm包的信息
#yum info extras
注:通过其它网站下载安装的rpm包的信息

搜索rpm包
搜索匹配特定字符的rpm包
#yum search mozilla
注:在rpm包名,包描述等中搜索
搜索有包含特定文件名的rpm包
#yum provides realplay

增加资源库
例如:增加rpm.livna.org作为资源库
安装Livna.org rpms GPG key
#rpm --import http://rpm.livna.org/RPM-LIVNA-GPG-KEY
检查GPG Key
# rpm -qa gpg-pubkey*
显示Key信息
#rpm -qi gpg-pubkey-a109b1ec-3f6e28d5
(注:如果要删除Key,使用#rpm -e gpg-pubkey-a109b1ec-3f6e28d5)

yum常用的命令
# yum install xxx #安装xxx软件
# yum info xxx #查看xxx软件的信息
# yum remove xxx #删除软件包
# yum list #列出软件包
# yum clean #清除缓冲和就的包
# yum provides xxx #以xxx为关键字搜索包(提供的信息为关键字)
# yum search xxx #搜索软件包(以名字为关键字)
# yum groupupdate xxx
# yum grouplist xxx
# yum groupremove xxx

这三个都是一组为单位进行升级 列表和删除的操作。。比如 "Mysql Database"就是一个组会同时操作相关的所有软件包;
# yum update #系统升级
# yum list available #列出所有升级源上的包;
# yum list updates #列出所有升级源上的可以更新包;
# yum list installed #列出已经安装的包;
# yun update kernel #升级内核;

yum常用的源
(1) 自动选择最快的源
由于yum中有的mirror速度是非常慢的,如果yum选择了这个mirror,这个时候yum就会非常慢,对此,可以下载fastestmirror插件,它会自动选择最快的mirror:
#yum install yum-fastestmirror

配置文件:(一般不用动)/etc/yum/pluginconf.d/fastestmirror.conf
你的yum镜像的速度测试记录文件:/var/cache/yum/timedhosts.txt

(2)使用图形界面的yum
如果觉得命令行的yum不方便,那么可以使用图形化的yumex,这个看起来更方便,因为可以自由地选择软件仓库:
#yum install yumex

然后在系统工具中就可以看到yum extender了。实际上系统自带的“添加/删除程序“也可以实现图形化的软件安装,但有些yumex的功能它没有。


取得最新有效源
可以访问下面地址获得,对于不同网络,这两个都表现很好。
网易:http://mirrors.163.com/.help/centos.html
中科大:http://lug.ustc.edu.cn/wiki/mirrors/help/centos

CentOS默认自带CentOS-Base.repo源,但官方源中去除了很多有版权争议的软件,而且安装的软件也不是最新的稳定版。Fedora自带的源中也找不到很多多媒体软件,如果需要安装,必需先添加其他源,如RPMFusion和RPMForge等第三方软件库。请针对不同的版本下载相应的包。

第三方资源库(3rd Party Repositories)
WARNING: These repositories are not provided nor supported by CentOS. They are listed in a 'catch as catch can' order, and being listed earlier does not imply any particular merit to a given repository. The CentOS project exercises no editorial control over the assertions of computability made by these sites. If something from them breaks, you get to keep the pieces. Some of the repos, such as RPMforge, ELRepo, ATrpms, EPEL, and RPMfusion have their own mailing lists for support issues with their packages.

NOTE: If you are considering using a 3rd Party Repository, then you should seriously consider how to prevent unintended 'updates' from these side archives from over-writing some core part of CentOS. One approach is to only enable these archives from time to time, and generally leave them disabled.

Security warning: The rpmforge-release package imports GPG keys into your RPM database. As long as you have verified the md5sum of the key injection package, and trust Dag, et al., then it should be as safe as your trust of them extends.

[主要的发行repo源]

RPMforge/Repoforge
Repoforge is a project that maintains RPM packages for Red Hat Enterprise Linux (RHEL), CentOS and Scientific Linux. It consists of a set of repositories compatible with various RHEL versions. This project is a fusion of or supported by various other projects and numerous independent RPM packagers. They provide over 5000 packages for SL, including wine, vlc, mplayer, xmms-mp3, and other popular media tools.

RPMForge是CentOS系统下的软件仓库,拥有4000多种的软件包,被CentOS社区认为是最安全也是最稳定的一个软件仓库。

主页:http://repoforge.org/

RPMForge下载地址:http://apt.sw.be/redhat/

ELRepo
The Community Enterprise Linux Repository
ELRepo is a community repository for Enterprise Linux distributions (e.g, Red Hat Enterprise Linux (RHEL), CentOS, Scientific Linux). ELRepo currently focuses on hardware related packages to boost your experience with Enterprise Linux. For example, this includes filesystem, graphics, hardware monitoring, network, sound and webcam drivers. All our packages are built against the RHEL kernel and are compatible with other clone distributions who maintain compatibility with the RHEL kernel (e.g, CentOS, Scientific Linux).

主页:http://elrepo.org

EPEL
EPEL,即Extra Packages for Enterprise Linux,是由 Fedora 社区创建维护,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。EPEL中含有大量的软件,对官方标准源是一个很好的补充。

EPEL (Extra Packages for Enterprise Linux) is a Fedora Special Interest Group that creates, maintains, and manages a high quality set of additional packages for Enterprise Linux, including, but not limited to, Red Hat Enterprise Linux (RHEL), CentOS and Scientific Linux (SL).

EPEL packages are usually based on their Fedora counterparts and will never conflict with or replace packages in the base Enterprise Linux distributions. EPEL uses much of the same infrastructure as Fedora, including buildsystem, bugzilla instance, updates manager, mirror manager and more.

红帽组建团队全职参与 EPEL,希望增强 EPEL

2021年9月上旬消息,CentOS 发布的公告称,红帽正在组建一个直接负责参与 EPEL 工作的小型团队。当然他们的工作不是取代 EPEL 社区,而是全职支持它。

EPEL 是"Extra Packages for Enterprise Linux"的缩写,它是 RHEL / CentOS 上的一项流行服务,能够帮助用户轻松获取 RHEL 本身无法提供的额外软件包。据介绍,EPEL 服务由 Fedora 托管,在为 RHEL、CentOS 和其他发行版补充可用的软件包方面取得了巨大成功,这个 Fedora 特别兴趣小组在提供和维护这些广泛的额外软件包方面做得很好,方便在基于 RHEL 的发行版上轻松部署。

现在红帽计划招聘全职员工来帮助开发和支持 EPEL,CentOS 期待此举能带来有益的效果,包括为 RHEL 主要版本带来更好的 EPEL 准备工作。另外,EPEL 团队将成为红帽社区平台工作组(简称 CPE)的一部分。


主页:http://fedoraproject.org/wiki/EPEL

Remi
Although a small private repo, it is used by many as additional repo next to EPEL. The owner maintains various packages for the Fedora and EPEL repositories (and also some for RPMFusion).

Remi源对于不想编译最新版的linux使用者,因为Remi源中的软件几乎都是最新稳定版。或许您会怀疑它不稳定?不用担心,这些都是Linux骨灰级的玩家编译好放进源里的,他们对于系统环境和软件编译参数的熟悉程度毋庸置疑。它目前正在成为许多人的首选源。

主页:http://rpms.famillecollet.com/

[其它的发行repo源]
这些源对日常大部分软件的管理维护基本上用不到,但有不常见的软件能在此找到,如桌面、下载等。或基于rhel的发行版本的源(不一定兼容所有版本)。

RPMFusion
如果您现在正在使用Fedora15,对RPMFusion一定不陌生吧,各种音频软件如MPlayer在标准源中是没有的,一般先安装RPMFusion源,之后就可以放便地yum install各种需要的软件啦。

RPM Fusion provides software that the Fedora Project or Red Hat doesn't want to ship. That software is provided as precompiled RPMs for all current Fedora versions and Red Hat Enterprise Linux 5; you can use the RPM Fusion repositories with tools like yum and PackageKit. RPM Fusion is a merger of Dribble, Freshrpms, and Livna; our goal is to simplify end-user experience by grouping as much add-on software as possible in a single location.

CentOS官方说RPMFusion软件库里面的软件稳定性不如rpmforge,安装使用过程如下:

RHEL 6/CentOS 6 :
su -c ’rpm -Uvh http://download1.rpmfusion.org/free/el/updates/testing/6/i386/rpmfusion-free-release-6-0.1.noarch.rpm http://download1.rpmfusion.org/nonfree/el/updates/testing/6/i386/rpmfusion-nonfree-release-6-0.1.noarch.rpm’

其他发行版本请详见:http://rpmfusion.org/Configuration

主页:http://rpmfusion.org/

KBS-Extras
This site (by a CentOS team member) provides a rebuild of selected packages from the archive formerly known as Fedora Extras but patched as needed for CentOS, as well as number of other packages. This repository is available at homepage and has a reputation for being stable and safe. Current CentOS 5 packages are in -testing repos only, the primary repos being empty.

主页:http://centos.karan.org

ATrpms
This repository provides many bleeding-edge applications and media utilities such as myth-tv. This repository is available at http://atrpms.net/

This repository's CentOS 4 and earlier archive replaces system packages with versions which are usually later than those which Base CentOS ships, and may cause stability issues. Use at your own risk.

The CentOS 5/RHEL 5 repository from atrpms.net is safe to use, if you only use the stable version. Packages in there do not overwrite system packages.

If you also enable testing and bleeding sub-trees from, ATrpms, you are in uncharted waters again - these two do overwrite system packages; testing however, has been labeled a misnomer by Axel Thimm and is pretty much required to get MythTV and a lot of other ATrpms multimedia packages to work. ATrpms testing has actually been tested and is analogous to centosplus.

主页:http://atrpms.net/

LinuxTECH.NET

The LinuxTECH.NET repos are a set of small but growing personal repos for EL6 compatible distros, created for personal use, but also accessible publicly on an "AS IS" basis. The main focus of these repos is audio/video related packages with an emphasis on wide support of codecs and as many optional features enabled as possible in all packages. Apart from audio/video related packages you will also find a few other unusual server and desktop related packages that no other EL compatible repo has.

主页:http://www.linuxtech.net/

PUIAS Linux
PUIAS is a clones also based on the sources from RHEL and a project of the computing staff of Princeton University and the Institute for Advanced Study. They offer additional repos. The Addons repository contains additional packages not included in a stock Red Hat distribution. The Computational repository also includes additional packages, however, these packages are specific to scientific computing. The Unsupported repository is a place where one time packages are put, they are unsupported and may change frequently.

主页:http://springdale.math.ias.edu/

[以下是部分软件(第三方)源]
Samba 3
The SerNet Samba 3 Repository - German site with Samba 3 packages for Centos 3, 4, and 5

Repo configs for 3, 4, and 5 are available. Some users have reported success with these packages but caution is advised.

主页:http://ftp.sernet.de/pub/samba/3.5/

第三方Java应用
JPackage Repository--"The JPackage Project has two primary goals:To provide a coherent set of Java software packages for Linux, satisfying all quality requirements of other applications.

To establish an efficient and robust policy for Java software packaging and installation. We focus on free and open source software whenever possible. For convenience, we also provide non-free packages without the restricted source code. Our RPMs are generic in that they should work on any RPM based Linux distribution (Mandrake, Red Hat, SuSE, others). Other packaging format suggestions are welcome too. "

主页:http://www.jpackage.org

Apache+php+mysql+nginx新版本源
Jason Litka - Utter Ramblings Repository - (See http://www.jasonlitka.com/yum-repository/) Updated web stack and other applications for EL4 and EL5 that will replace core packages. Includes apr, apr-util, freetds, httpd, libmcrypt, mhash, mod_evasive, mod_perl, mod_python, mod_security, mysql, mysqlclient10, mysqlclient14, pcre, perl-BSD-Resource, php, php-apc, php-eaccelerator, php-memcache, php-pear, php-xcache, subversion, tidy. Suggest caution if using this repo due to replacement of core packages.

注:在没有知道remi源之前,在做新的网站系统时用的这个源,与remi源同用时有冲突。

主页:http://www.jasonlitka.com/

在此不一列举,其它还包括Google、Percona、Postgresql、FireFox、Adobe等都有相关源,更多请留意其主页。


如何使用各种源

以上主要源对CentOS等系统完全兼容,但各软件库之间并不能保证完全兼容没有冲突。如果您需要使用以上源,需要安装yum-priorities插件。安装yum-priorities插件后,您可以给各个源设置优先级priority。一般设置官方标准源优先级为1,最高,第三方推荐>10。

priority=N (N为1到99的正整数,数值越小越优先)

[base], [addons], [updates], [extras] … priority=1
[CentOSplus],[contrib] … priority=2
其他第三的软件源为:priority=N (推荐N>10)

在remi.repo中和epel.repo中添加priority设置即可使用。

#vim CentOS-Base.repo

[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
priority=1
#released updates
……

# ls
CentOS-Base.repo CentOS-Media.repo  epel-testing.repo CentOS-Debuginfo.repo epel.repo remi.repo

编辑 remi.repo 将[remi] 中的 enabled=0 改成 enabled=1 来启用 remi 源。

[repo源控制]
列出、验证系统中的源:
# yum repolist

临时禁止更新、使用某一或多个源:
# yum --disablerepo=remi update
# yum --disablerepo=fedora-devel --disablerepo=fedora-updates-testing list
# yum --disablerepo="*" --enablerepo=google-chrome list available

如果要禁止某一源,可在其配置文件中修改:
在目录'/etc/yum.repos.d/'下将相关源字段(有多个)的'enabled'由'1'改为'0'。

如果也要临时启用被禁止的源:
yum --enablerepo=some-disabled-repository install some-package

彻底移除某一个源:
找到相关源的rpm包安装记录:
rpm -qa |grep -i repo-name
删除该软件包:
rpm -e some-repository-rpm-package
也可以删除或移出repo文件(如果是手动建立而非通过包安装):
rm|mv /etc/yum.repos.d/kde.repo

# 导入相关源的gpg key.
# rpm –import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
# rpm –import /etc/pki/rpm-gpg/RPM-GPG-KEY-remi

更多源信息可参考:http://wiki.centos.org/AdditionalResources/Repositories


Yum使用过程问题集
yum update 卡住现象 不响应(not respond)

主要是停留在这两处时:
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile

可能是本地dns的问题,看下解析有没有问题:
nslookup|dig google.com

如果解析没有问题,可手动清理下其缓存:
rm -f /var/cache/yum/timedhosts.txt

如果还不能起作用的话,可以重建一下rpm数据库:
rm -f /var/lib/rpm/__*
rpm --rebuilddb -v -v   
yum clean all

同时可改一下‘/etc/yum.conf’中的参数:
debugelevel=1
errorlevel=1
timeout=1

The timeout is standard 30 seconds. So if a repository does not respond, the error takes 30 seconds to appear. Also try using yum without the plugins (like fastest mirror and priorities) with the option --noplugins.
Now starting yum again should give you more info faster. Test with:
yum --verbose --noplugins info

也可将yum中的自动查找最快源的插件禁用,前提是主配置文件里已经加上源主机。即编辑'/etc/yum/pluginconf.d/fastestmirror.conf'文件,将‘enabled’项由'1'置为'0',再试看能否成功。

将本地光盘/iso作为yum安装源
这种情况多见于上网不方便,或使用了rhel发行版本而又需要安装软件,下面示例从rhel 6.x的dvd iso做成本地源,然后通过yum进行软件的安装。
方法一:将iso挂载到目录并做成repo源
创建挂载点
mkdir /mnt/iso

挂载iso到上述目录
mount -o loop rhel-6.x.iso /mnt/iso/

创建repository:
[rhel-local-dvd]
name=Red Hat OS DVD
baseurl=file:///mnt/iso/Server
enabled=1
gpgcheck=0

清空与更新yum缓存
yum clean all && yum update

方法二:使用localinstall方式安装具体软件
yum localinstall /mnt/iso/Server/gcc-(versiom).rpm

当然也可以直接安装具体软件,如果没有什么依赖的话。

方法三:Create a repository
需要createrepo软件包,通过源安装或rpm包。
rpm -ivh createrepo-version.rpm

将光盘或iso中的内容复制到目录'/var/ftp/pub',
创建repo库
createrepo -v /var/ftp/pub/
或cd  /var/ftp/pub/ && createrepo . && yum clean all

编辑repo源配置文件
vim /etc/yum.repos.d/base.repo
[base]
name=RHEL Local Server Repo
baseurl=http://ipaddr/pub/ 
enabled=1
gpgcheck=0

这样也可以为远程机器提供内部网络安装源。


在本地构建CentOS 7的镜像源(Create CentOS 7 Mirror)


安装必要包
yum update
yum install -y createrepo rsync nginx

创建相关目录及相应的权限

sudo mkdir -p /var/www/html/repos/centos/7/os/x86_64/
sudo mkdir -p /var/www/html/repos/centos/7/updates/x86_64/
sudo chmod 770 -R /var/www
sudo chown $USER:nginx -R /var/www

在Nginx中创建对应的主机
server {
    listen  80;
    server_name  localhost;
    root  /var/www/html/repos;
    location  / {
        autoindex  on;
    }
}

初始软件库
createrepo /var/www/html/repos/centos/7/os/x86_64/
createrepo /var/www/html/repos/centos/7/updates/x86_64/

List of CentOS Mirrors中寻找最近的rsync源,国内可以用:rsync://mirrors.shu.edu.cn/centos/

路径为:
/7/os/x86_64/
/7/updates/x86_64/

开始同步软件包:
rsync -avz -delete --exclude='repo*' \
rsync://mirrors.shu.edu.cn/centos/7/os/x86_64/ \
/var/www/html/repos/centos/7/os/x86_64/

rsync -avz --delete --exclude='repo*' \
rsync://mirrors.shu.edu.cn/centos/7/updates/x86_64/ \
/var/www/html/repos/centos/7/updates/x86_64/

更新yum源的元信息
createrepo --update /var/www/html/repos/centos/7/os/x86_64/
createrepo --update /var/www/html/repos/centos/7/updates/x86_64/

做成自动任务来保持相对的更新
#!/bin/bash
/usr/bin/rsync -avz --delete --exclude='repo*' \
rsync://mirrors.shu.edu.cn/centos/7/os/x86_64/ \
/var/www/html/repos/centos/7/os/x86_64/

/usr/bin/rsync -avz --delete --exclude='repo*' \
rsync://mirrors.shu.edu.cn/centos/7/updates/x86_64/ \
/var/www/html/repos/centos/7/updates/x86_64/

/usr/bin/createrepo --update \
/var/www/html/repos/centos/7/os/x86_64/

/usr/bin/createrepo --update \
/var/www/html/repos/centos/7/updates/x86_64/

加入到crontab中
@daily /bin/bash /path/to/yum_pkgs_update.sh

最后记得在本地局域网中使用对就的源:/etc/yum.repos.d/CentOS-Base.repo


CentOS 7获取rpm软件安装包

经常用yum下载软件,今天就教大家一个方法,把软件包单独下载下来。执行yum帮助命令:yum --help

如果列表中出现 --downloadonly --downloaddir 参数,则表示目前yum已支持只下载不安装。没有出现则还需安装一个插件yum-plugin-downloadonly。默认会将软件包下载到/var/cache/yum目录下。

安装后下载和转移

此方法需要提前设置配置文件。将 keepcache=0 修改成 keepcache=1,metadata_expire=1800。

根据实际需求,修改 exclude=*.i386 *.i686,将不安装i386 i686的软件包。

有的软件包安装的时候需要很多依赖包,内网不能上公网,这个命令就能很好的帮助我们;做离线yum源的时候,补什么包都可以用这个命令去下载离线rpm包添加到自建yum即可

yum -y install --downloadonly --downloaddir=/tmp  packagename
--downloadonly :仅下载,不安装
--downloaddir:离线包存储目录
packagename:所需下载的安装包名

例子:
# yum -y install --downloadonly --downloaddir=/tmp  docker-ce

当然也可以指定到具体版本

# 列出所有版本
yum list docker-ce --showduplicates | sort -r

# 指定版本安装
# yum -y install --downloadonly --downloaddir=/tmp/dk docker-ce-19.03-3.el7


首次下载安装包
将tigervnc-server安装包下载到/srv/tigervnc-server
yum install --downloadonly --downloaddir /srv/tigervnc-server tigervnc-server

如果已经下载,修改为reinstall参数
yum reinstall --downloadonly --downloaddir /srv/tigervnc-server tigervnc-server

复制到其它主机安装离线安装包
cd /srv/tigervnc-server
yum localinstall *.rpm



该文章最后由 阿炯 于 2024-03-18 15:46:17 更新,目前是第 3 版。