你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux yum的配置使用和程序包的编译安装

[日期:2017-09-16] 来源:Linux社区  作者:liubinsh [字体: ]

在上一篇博客中,写到了如何使用rpm命令来安装、卸载软件等,但是大家都知道,各个软件包之间可能存在依赖关系,如果安装某个软件需要额外的依赖其他若干的包,那么我们就需要将其他额外的包一个一个的安装上去,这样的一个过程是非常耗时的,所以出现了一个前端工具,如yum,dnf,apt-get等,本次博客就CentOS上的yum和程序包的编译安装做出详细的描述,最后还通过实际的例子介绍了程序包的编译安装。

yum的实现原理

yum实现软件包的获取其实是基于C/S架构的,C为你的主机,S为yum repository(文件服务器或者本地yum  repository),yum repository里面存储了众多的rpm包,以及包的相关元数据文件(包含软件包的列表和依赖关系等)和计算出来的特征码,一般放在repodata目录下面。

当我们使用yum安装一个软件包的时候,会进行如下几个步骤:

1.本地主机会在配置文件里面找到yum repository的指向,并建立连接;

2.从yum repository获取元数据文件和特征码放在缓存里面;

3.通过查看元数据文件获知需要安装的软件包和其依赖的软件的名称并向yum repository发起请求;

4.本地主机获取到软件包后进行安装。

5.安装好后,rpm包会被删除,但是元数据文件和特征码会被保存下来,其目的是为了当你再向yum repository发起连接的时候,先会向yum repository获取到特征码和本地缓存的特征码做对比,如果相同则说明yum repository里面的文件没有发生改变,如果不同,则说明yum repository里面的文件发生了改变,这时候就需要重新向yum repository获取元数据文件了。

 

通常情况下yum repository都是文件服务器,你可以通过URL进行访问,如:

ftp://path/to/repository/    通过FTP进行访问和传输

http://path/to/repository/  通过http进行访问和传输

nfs://path/to/repository/   通过nfs进行访问和传输

file:///path/to/repository/  用于本地的yum仓库

 

yum的配置文件

yum的配置文件有:

/etc/yum.conf:为所有仓库提供公共配置

/etc/yum.repos.d/下以repo结尾的文件:为仓库的指向提供配置

yum.conf的内容:

[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
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
distroverpkg=centos-release

cachedir:缓存目录,默认情况下在"/var/cache/yum";

keepcache:是否保存缓存,0是不保存,1是保存;

debuglevel:Debug信息的输出等级,范围为0-10,默认为2

logfile:存放log的文件路径

exactarch:是否精确匹配平台,默认是1,精确匹配平台

obsolete:这个选项仅仅会影响升级,简单的说相当于upgrade,允许更新旧版本的rpm包

gpgcheck:是否经验安装包的来源合法性

plugins:是否允许使用插件安装插件,1表示安装

installonly_limit:允许同时保持的内核包

bugtracker_url:bug的追踪的url

distroverpkg:指定一个软件包,yum会根据这个包判断你的发行版本,这里是centos-release

具体的可以是使用命令man yum.conf查看

下面我们来看一下/etc/yum.repos.d/下的文件

[root@localhost ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo

这里就有很多以repo结尾的文件,都是yum repository的指向:

让我们来看一下/etc/yum.repo.d/CentOS-Base.repo,其他里面的yum repository都是disable的:

 
# 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=$basearch&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

repository指向的定义:

[repositoryID]   #具有唯一性
name=some name for this repository
baseurl=url://path/to/repository  #yum repository的url可以为多个,路径下必须有repodata
enable=1   #是否开启,1为开启,0为关闭,不写的话,默认开启
gpgckeck=1
gpgkey=URL
cost=1000
enablegroup=1 #是否允许使用包组,默认是允许
 
但是我们可以看到CentOS-Base.repo里面没有baseurl而是mirrorlist,mirrorlist指向的URL就是存放了baseurl的位置。
yum的repo配置文件中的可用的变量:
$releasever:当前os的发行版的主版本号
$arch:平台
$basearch:基础平台
$YUM0-$YUM9:用户自定义的变量

yum命令的用法

yum [options] [command] [package ...]
       command is one of:
        * install package1 [package2] [...]
        * update [package1] [package2] [...]
        * update-to [package1] [package2] [...]
        * update-minimal [package1] [package2] [...]
        * check-update
        * upgrade [package1] [package2] [...]
        * upgrade-to [package1] [package2] [...]
        * distribution-synchronization [package1] [package2] [...]
        * remove | erase package1 [package2] [...]
        * autoremove [package1] [...]
        * list [...]
        * info [...]
        * provides | whatprovides feature1 [feature2] [...]
        * clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]
        * makecache [fast]
        * groups [...]
        * search string1 [string2] [...]
        * shell [filename]
        * resolvedep dep1 [dep2] [...]
           (maintained for legacy reasons only - use repoquery or yum provides)
        * localinstall rpmfile1 [rpmfile2] [...]
           (maintained for legacy reasons only - use install)
        * localupdate rpmfile1 [rpmfile2] [...]
           (maintained for legacy reasons only - use update)
        * reinstall package1 [package2] [...]
        * downgrade package1 [package2] [...]
        * deplist package1 [package2] [...]
        * repolist [all|enabled|disabled]
        * repoinfo [all|enabled|disabled]
        * repository-packages <enabled-repoid> <install|remove|remove-or-reinstall|remove-or-

1.显示repository列表:

repolist [all|enabled|disabled]
all:显示所有的yum repository
enable:不带参数默认显示启用的yum repository
disable:显示关闭的yum repository
[root@localhost yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.cn99.com
 * updates: mirrors.aliyun.com
repo id                                            repo name                                           status
!base/7/x86_64                                     CentOS-7 - Base                                     9,363
!extras/7/x86_64                                   CentOS-7 - Extras                                     451
!updates/7/x86_64                                  CentOS-7 - Updates                                  2,146
[root@localhost yum.repos.d]# yum repolist all
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.cn99.com
 * updates: mirrors.aliyun.com
repo id                                          repo name                                         status
C7.0.1406-base/x86_64                            CentOS-7.0.1406 - Base                            disabled
C7.0.1406-centosplus/x86_64                      CentOS-7.0.1406 - CentOSPlus                      disabled
C7.0.1406-extras/x86_64                          CentOS-7.0.1406 - Extras                          disabled
C7.0.1406-fasttrack/x86_64                       CentOS-7.0.1406 - CentOSPlus                      disabled
C7.0.1406-updates/x86_64                         CentOS-7.0.1406 - Updates                         disabled
C7.1.1503-base/x86_64                            CentOS-7.1.1503 - Base                            disabled
C7.1.1503-centosplus/x86_64                      CentOS-7.1.1503 - CentOSPlus                      disabled
C7.1.1503-extras/x86_64                          CentOS-7.1.1503 - Extras                          disabled
C7.1.1503-fasttrack/x86_64                       CentOS-7.1.1503 - CentOSPlus                      disabled
C7.1.1503-updates/x86_64                         CentOS-7.1.1503 - Updates                         disabled
C7.2.1511-base/x86_64                            CentOS-7.2.1511 - Base                            disabled
C7.2.1511-centosplus/x86_64                      CentOS-7.2.1511 - CentOSPlus                      disabled
C7.2.1511-extras/x86_64                          CentOS-7.2.1511 - Extras                          disabled
C7.2.1511-fasttrack/x86_64                       CentOS-7.2.1511 - CentOSPlus                      disabled
C7.2.1511-updates/x86_64                         CentOS-7.2.1511 - Updates                         disabled
!base/7/x86_64                                   CentOS-7 - Base                                   enabled: 9,363
base-debuginfo/x86_64                            CentOS-7 - Debuginfo                              disabled
base-source/7                                    CentOS-7 - Base Sources                           disabled
c7-media                                         CentOS-7 - Media                                  disabled
centosplus/7/x86_64                              CentOS-7 - Plus                                   disabled
centosplus-source/7                              CentOS-7 - Plus Sources                           disabled
cr/7/x86_64                                      CentOS-7 - cr                                     disabled
!extras/7/x86_64                                 CentOS-7 - Extras                                 enabled:   451
extras-source/7                                  CentOS-7 - Extras Sources                         disabled
fasttrack/7/x86_64                               CentOS-7 - fasttrack                              disabled
!updates/7/x86_64                                CentOS-7 - Updates                                enabled: 2,146
updates-source/7                                 CentOS-7 - Updates Sources                        disabled

2.显示程序包

yum list [all | glob_exp1] [glob_exp2] [...]
yum list {available|installed|updates} [glob_exp1] [...]

3.安装程序包

yum install package1 [package2] [...]
yum reinstall package1 [package2] [...]  (重新安装)

4.升级程序包

yum update [package1] [package2] [...]
yum downgrade package1 [package2] [...] (降级)

5.检查可用升级

yum check-update

6.卸载程序包

yum remove | erase package1 [package2] [...]

7.查看程序包information

yum info package1 [package2] [...]

8.查看指定的特性(可以是某文件)是由哪个程序包所提供的

yum provides | whatprovides feature1 [feature2] [...]
[root@localhost yum.repos.d]# yum provides /bin/ls
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.cn99.com
 * updates: mirrors.aliyun.com
coreutils-8.22-18.el7.x86_64 : A set of basic GNU tools commonly used in shell scripts
Repo        : base
Matched from:
Filename    : /bin/ls



coreutils-8.22-18.el7.x86_64 : A set of basic GNU tools commonly used in shell scripts
Repo        : @anaconda
Matched from:
Filename    : /bin/ls

 9.清理本地缓存

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

10.构建缓存

yum makecache

11.搜索

yum search string1 [string2] [...] #以指定的关键字搜索程序包及Summary信息

12.查看指定包所依赖的capabilities

yum deplist package1 [package2] [...]

13.查看yum事务历史

yum history [info|list|packages-list|packages-info|summary|addon-info|redo|undo|rollback|new|sync|stats]

14.安装及升级本地程序包

yum localinstall rpmfile1 [rpmfile2] [...]
   (maintained for legacy reasons only - use install)
yum localupdate rpmfile1 [rpmfile2] [...]
   (maintained for legacy reasons only - use update)

15.包组管理的相关命令

yum groupinstall group1 [group2] [...]
yum groupupdate group1 [group2] [...]
yum grouplist [hidden] [groupwildcard] [...]
yum groupremove group1 [group2] [...]
yum groupinfo group1 [...]

16.yum的命令选项

--nogpgcheck:禁止进行gpg check;
-y: 自动回答为“yes”;
-q:静默模式;
--disablerepo=repoidglob:临时禁用此处指定的repo;
--enablerepo=repoidglob:临时启用此处指定的repo;
--noplugins:禁用所有插件;

如何使用光盘当作本地yum repository?

(1)挂载光盘至某目录,例如/media/cdrom

[root@localhost yum.repos.d]# mount -r /dev/sr0 /media/cdrom/
[root@localhost yum.repos.d]# 
[root@localhost yum.repos.d]# df
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/mapper/cl-root  17811456 2824736  14986720  16% /
devtmpfs               489004       0    489004   0% /dev
tmpfs                  499980       0    499980   0% /dev/shm
tmpfs                  499980    6728    493252   2% /run
tmpfs                  499980       0    499980   0% /sys/fs/cgroup
/dev/sda1             1038336  123500    914836  12% /boot
tmpfs                  100000       0    100000   0% /run/user/0
/dev/sr0              4276440 4276440         0 100% /media/cdrom
 
(2)创建配置文件
先把本地的CentOS-Base.repo备份一下
 
[root@localhost yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak
[root@localhost yum.repos.d]# vim CentOS-Local.repo
[CentOS7]
name=CentOS7 rpm from disk
baseurl=file:///media/cdrom/
gpgcheck=0
[root@localhost cdrom]# yum repolist #显示可用的仓库列表
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                       repo name                                                    status
CentOS7                                       CentOS7 rpm from disk                                        3,831
repolist: 3,831
[root@localhost cdrom]# 

如何创建yum仓库?

前面写到baseurl指向的yum repository的下必须有repodata目录,repodata目录下存放着元数据和特征码等信息,那么如何生存呢?

我们现在/xfs目录下放了一些关于xfs的rpm包:

[root@localhost xfs]# ll
total 3980
-rw-r--r--. 1 root root 942156 Sep 10 10:41 xfsprogs-4.5.0-10.el7_3.i686.rpm
-rw-r--r--. 1 root root 916492 Sep 10 10:41 xfsprogs-4.5.0-10.el7_3.x86_64.rpm
-rw-r--r--. 1 root root 942328 Sep 10 10:41 xfsprogs-4.5.0-9.el7_3.i686.rpm
-rw-r--r--. 1 root root 916296 Sep 10 10:41 xfsprogs-4.5.0-9.el7_3.x86_64.rpm
-rw-r--r--. 1 root root  68316 Sep 10 10:41 xfsprogs-devel-4.5.0-10.el7_3.i686.rpm
-rw-r--r--. 1 root root  68264 Sep 10 10:41 xfsprogs-devel-4.5.0-10.el7_3.x86_64(1).rpm
-rw-r--r--. 1 root root  68264 Sep 10 10:41 xfsprogs-devel-4.5.0-10.el7_3.x86_64.rpm
-rw-r--r--. 1 root root  68184 Sep 10 10:41 xfsprogs-devel-4.5.0-9.el7_3.i686.rpm
-rw-r--r--. 1 root root  68148 Sep 10 10:41 xfsprogs-devel-4.5.0-9.el7_3.x86_64.rpm

使用createrepo来生成:

[root@localhost ~]# createrepo /xfs/
Spawning worker 0 with 5 pkgs
Spawning worker 1 with 4 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@localhost ~]# ll /xfs/
total 3984
drwxr-xr-x. 2 root root   4096 Sep 10 10:46 repodata
-rw-r--r--. 1 root root 942156 Sep 10 10:41 xfsprogs-4.5.0-10.el7_3.i686.rpm
-rw-r--r--. 1 root root 916492 Sep 10 10:41 xfsprogs-4.5.0-10.el7_3.x86_64.rpm
-rw-r--r--. 1 root root 942328 Sep 10 10:41 xfsprogs-4.5.0-9.el7_3.i686.rpm
-rw-r--r--. 1 root root 916296 Sep 10 10:41 xfsprogs-4.5.0-9.el7_3.x86_64.rpm
-rw-r--r--. 1 root root  68316 Sep 10 10:41 xfsprogs-devel-4.5.0-10.el7_3.i686.rpm
-rw-r--r--. 1 root root  68264 Sep 10 10:41 xfsprogs-devel-4.5.0-10.el7_3.x86_64(1).rpm
-rw-r--r--. 1 root root  68264 Sep 10 10:41 xfsprogs-devel-4.5.0-10.el7_3.x86_64.rpm
-rw-r--r--. 1 root root  68184 Sep 10 10:41 xfsprogs-devel-4.5.0-9.el7_3.i686.rpm
-rw-r--r--. 1 root root  68148 Sep 10 10:41 xfsprogs-devel-4.5.0-9.el7_3.x86_64.rpm
[root@localhost ~]# ll /xfs/repodata/
total 32
-rw-r--r--. 1 root root 1615 Sep 10 10:46 048072895365ec38b370d70bc209f3fd08a54bba535a2feb497448fa7b3d622e-other.xml.gz
-rw-r--r--. 1 root root 3885 Sep 10 10:46 29cf78ac1dcc6c2443c5a2d9a135a4a7f1591f44a1964c984c1664000ac86ee6-other.sqlite.bz2
-rw-r--r--. 1 root root 1239 Sep 10 10:46 2a83ee6cbdb7d0ca6063f91aa8e382271a05ae1e868f637beb0549758511325b-filelists.xml.gz
-rw-r--r--. 1 root root 3144 Sep 10 10:46 40f7e90c6dc338f5292d2cb1ee3d2153241fb714eca0365d2419eadfe7fda2e0-filelists.sqlite.bz2
-rw-r--r--. 1 root root 7501 Sep 10 10:46 5c4be0e2d956a133454d00f51c3833d44d13c60375937bf59bce4258210d9075-primary.sqlite.bz2
-rw-r--r--. 1 root root 2376 Sep 10 10:46 5c7e3e89ad6e3210fee5ca5519fcd26bb60f4356d9035da5e273bb69521b8be3-primary.xml.gz
-rw-r--r--. 1 root root 2975 Sep 10 10:46 repomd.xml #特征码

 然后在配置文件里面添加就行了,这样本地创建的yum repository就生效了:

[root@localhost yum.repos.d]# vim CentOS-Local.repo
[CentOS7]
name=CentOS7 rpm from disk
baseurl=file:///media/cdrom/
gpgcheck=0
enable=1

[xfs]
name=xfs for centos7
baseurl=file:///xfs/
gpgcheck=0
enable=1

更多详情见请继续阅读下一页的精彩内容http://www.linuxidc.com/Linux/2017-09/146903p2.htm

linux
相关资讯       yum配置  Linux yum 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款