yum工具的使用.md

yum工具的使用

1.rpm的弊端

其弊端是显而易见的,当用rpm安装软件时,若遇到有依赖关系的软件,必须先安装依赖的软件才能继续安装我们要安装的软件,当依赖关系很复杂的情况下,这种安装方式就很让人头疼

2.yum的优劣势

yum有什么优势呢?yum最大的优势就是能够解决rpm的依赖问题,yum能够自动解决软件安装时的依赖关系。
当然了,有优势就有劣势,人无完人嘛,软件也是一样的,yum的缺陷就是如果在未完成安装的情况下强行中止安装过程,下次再安装时将无法解决依赖关系,Fedora22+、redhat7和centos7等可以通过手动安装dnf工具来解决此问题。
dnf是redhat7上用来代替yum的一个工具,其存在的意义就是处理yum的缺陷,但其用法与yum是完全一样的,甚至连选项都是一样的,你可以理解为dnf就是yum,只是换了个名字而已。所以说只要学会了yum就自然会dnf,大家不用担心学了yum又要去学dnf之类的问题

3.什么是yum及其作用

那么什么是yum呢?yum是yellowdog update manager的简称,它能够实现rpm管理的所有操作,并能够自动解决各rpm包之间的依赖关系。yum是rpm的前端工具,是基于rpm来实现软件的管理的一个工具。
你不能用yum去管理windows的exe程序包,也不能用yum去管理ubuntu的deb程序包,只能用yum来管理redhat系列的rpm包

4.挂载光盘

插入光盘
执行以下命令

[root@localhost ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost ~]# ls /mnt/
addons EULA GPL isolinux media.repo repodata RPM-GPG-KEY-redhat-release
EFI extra_files.json images LiveOS Packages RPM-GPG-KEY-redhat-beta TRANS.TBL

5.yum的原理

yum的工作需要两部分来合作,一部分是yum服务器,另一部分就是client的yum工具。下面分别介绍两部分工作原理。

yum服务器端工作原理
所有要发行的rpm包都放在yum服务器上以提供别人来下载,rpm包根据kernel的版本号,cpu的版本号分别编译发布。yum服务器只要提供简单的下载就可以了,ftp或者http的形式都可以。yum服务器有一个最重要的环节就是整理出每个rpm包的基本信息,包括rpm包对应的版本号、conf文件、binary信息,以及很关键的依赖信息。在yum服务器上提供了createrepo工具,用于把rpm包的基本概要信息做成一张“清单”,这张“清单”就是描述每个rpm包的spec文件中信息。

client端工作原理
client每次调用yum install或者search的时候,都会去解析/etc/yum.repos.d下面所有以.repo结尾的配置文件,这些配置文件指定了yum服务器的地址。yum会定期去更新yum服务器上的rpm包清单,然后把清单下载保存到yum客户端自己的cache里面,根据/etc/yum.conf里配置(默认是/var/cache/yum下面),每次调用yum装包的时候都会去这个cache目录下去找清单,根据清单里的rpm包描述从而来确定安装包的名字、版本号、所需要的依赖包等,然后再去yum服务器下载rpm安装。(前提是不存在rpm包的cache)

6.yum的元数据

存放位置(repodata目录)
包含的文件及其对应的功能
primary.xml.gz
当前仓库所有rpm包的列表;
依赖关系;
每个rpm包安装生成的文件列表
filelists.xml.gz
当前仓库所有rpm包的所有文件列表
other.xml.gz
额外信息,rpm包的修改日志
repomd.xml
记录的是primary.xml.gz、filelists.xml.gz、other.xml.gz这三个文件的时间戳和校验和
comps*.xml
rpm包分组信息

7.yum的配置文件

配置文件有哪些:
/etc/yum.conf 作用:为所有仓库提供公共配置
/etc/yum.repos.d/*.repo 作用:为仓库的指向提供配置

yum的repo配置文件中可用的变量:
$releaseversion:当前OS的发行版的主版本号
$arch:平台类型
$basearch:基础平台
为yum定义repo文件:

[Repo_Name]:仓库名称
name:描述信息
baseurl:仓库的具体路径,接受以下三种类型
    ftp://
    http://
    file:///
enabled:可选值{1|0},1为启用此仓库,0为禁用此仓库
gpgcheck:可选值{1|0},1为检查软件包来源合法性,0为不检查来源
    如果gpgcheck设为1,则必须用gpgkey定义密钥文件的具体路径
    gpgkey=/PATH/TO/KEY
vim /etc/yum.conf
cachedir=/var/cache/yum/$basearch/$releasever   //缓存目录
keepcache=0     //缓存软件包, 1启动 0 关闭
debuglevel=2    //调试级别
logfile=/var/log/yum.log    //日志记录位置
exactarch=1     //检查平台是否兼容
obsoletes=1     //检查包是否废弃
gpgcheck=1      //检查来源是否合法,需要有制作者的公钥信息
plugins=1       //是否启用插件
tolerant={1|0}  //容错功能,1为开启,0为关闭,当设为0时,如果用yum安装多个软件包且其中某个软件包已经安装过就会报错;当设为1时,当要安装的软件已经安装时自动忽略
installonly_limit=5
bugtracker_url
# metadata_expire=90m //每小时手动检查元数据
# in /etc/yum.repos.d   //包含repos.d目录 

8.yum仓库管理

8.1.yum本地仓库

复制光盘内容到yum服务器

[root@localhost ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost ~]# ls /mnt/
addons EULA GPL isolinux media.repo repodata RPM-GPG-KEY-redhat-release
EFI extra_files.json images LiveOS Packages RPM-GPG-KEY-redhat-beta TRANS.TBL
[root@localhost ~]# mkdir /opt/myrepo
[root@localhost ~]# cp -r /mnt/* /opt/myrepo/
^[[A^[[B^C
[root@localhost ~]# ls /opt/myrepo/
addons EFI EULA extra_files.json GPL images isolinux LiveOS media.repo Packages

配置repo文件

[root@localhost ~]# ls /etc/yum.repos.d/
redhat.repo
[root@localhost ~]# vi /etc/yum.repos.d/myrepo.repo
[root@localhost ~]# cat /etc/yum.repos.d/myrepo.repo
[myrepo]
name=myrepo
baseurl=file:///opt/myrepo
gpgcheck=0
enabled=1

清空yum本地缓存

[root@localhost ~]# yum clean all
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在清理软件源: myrepo
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos

检验yum本地仓库

[root@localhost ~]# yum list all
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
file:///opt/myrepo/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /opt/myrepo/repodata/repomd.xml"
正在尝试其它镜像。


 One of the configured repositories failed (myrepo),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=myrepo ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable myrepo
        or
            subscription-manager repos --disable=myrepo

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=myrepo.skip_if_unavailable=true

failure: repodata/repomd.xml from myrepo: [Errno 256] No more mirrors to try.
file:///opt/myrepo/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /opt/myrepo/repodata/repomd.xml"

8.2.yum网络仓库

官方网络yum仓库(国外)
阿里云yum仓库
163yum仓库
xx大学yum仓库
epel源

[root@localhost ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  % Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload Upload Total Spent Left Speed
100 664 100 664 0 0 2206 0 --:--:-- --:--:-- --:--:-- 2213
[root@localhost ~]# ls /etc/yum.repos.d/
epel.repo myrepo.repo redhat.repo

8.3.软件官方仓库

// 源查找方式基本一致,zabbix,mysql,saltstack,openstack等等,上官网找

8.4 redhat7使用centos7的yum源

1.卸载红帽yum源
[root@localhost ~]# rpm -e $(rpm -qa|grep yum) --nodeps

2.删除所有repo相关文件
[root@localhost ~]# rm -f /etc/yum.conf
[root@localhost ~]# rm -rf /etc/yum.repos.d/
[root@localhost ~]# rm -rf /var/cache/yum

3.下载centos相关yum组件
[root@localhost ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-3.4.3-161.el7.centos.noarch.rpm
[root@localhost ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
[root@localhost ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-50.el7.noarch.rpm
[root@localhost ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-updateonboot-1.1.31-50.el7.noarch.rpm
[root@localhost ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-utils-1.1.31-50.el7.noarch.rpm

//如果没有wget命令则使用curl命令
[root@localhost ~]# curl -o yum-utils-1.1.31-50.el7.noarch.rpm  http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-utils-1.1.31-50.el7.noarch.rpm
[root@localhost ~]# curl -o yum-3.4.3-161.el7.centos.noarch.rpm  http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-3.4.3-161.el7.centos.noarch.rpm
[root@localhost ~]# curl -o yum-metadata-parser-1.1.4-10.el7.x86_64.rpm  http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
[root@localhost ~]# curl -o yum-plugin-fastestmirror-1.1.31-50.el7.noarch.rpm   http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-50.el7.noarch.rpm
[root@localhost ~]# curl -o yum-updateonboot-1.1.31-50.el7.noarch.rpm http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-updateonboot-1.1.31-50.el7.noarch.rpm

3.安装所有相关组件
[root@localhost ~]# rpm -ivh yum-* --nodeps

4.下载base和epel仓库
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# sed -i 's#\$releasever#7#g' /etc/yum.repos.d/CentOS-Base.repo

[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

9.yum管理软件

9.1.yum命令语法:

yum [options] [command] [package ...]

9.2.常用的options:

--nogpgcheck                //如果从网上下载包有时会检查gpgkey,此时可以使用此命令跳过gpgkey的检查
-y                          //自动回答为"yes"
-q                          //静默模式,安装时不输出信息至标准输出
--disablerepo=repoidglob    //临时禁用此处指定的repo
--enablerepo=repoidglob     //临时启用此处指定的repo
--noplugins                 //禁用所有插件
[root@localhost ~]# yum -y install vim
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
epel | 5.4 kB 00:00:00     
myrepo | 4.1 kB 00:00:00     
(1/5): epel/x86_64/group_gz | 88 kB 00:00:00     
(2/5): epel/x86_64/updateinfo | 1.0 MB 00:00:00     
(3/5): myrepo/group_gz | 137 kB 00:00:00     
(4/5): myrepo/primary_db | 4.0 MB 00:00:00     
(5/5): epel/x86_64/primary_db | 6.8 MB 00:00:00     
正在解决依赖关系
--> 正在检查事务
---> 软件包 vim-enhanced.x86_64.2.7.4.160-2.el7 将被 安装
--> 正在处理依赖关系 vim-common = 2:7.4.160-2.el7,它被软件包 2:vim-enhanced-7.4.160-2.el7.x86_64 需要
--> 正在处理依赖关系 perl(:MODULE_COMPAT_5.16.3),它被软件包 2:vim-enhanced-7.4.160-2.el7.x86_64 需要
--> 正在处理依赖关系 libgpm.so.2()(64bit),它被软件包 2:vim-enhanced-7.4.160-2.el7.x86_64 需要
--> 正在处理依赖关系 libperl.so()(64bit),它被软件包 2:vim-enhanced-7.4.160-2.el7.x86_64 需要
--> 正在检查事务

9.3.常用的command:

list            //列表
    all         //默认项
    available   //列出仓库中有的,但尚未安装的所有可用的包
    installed   //列出已经安装的包
    updates     //可用的升级
clean           //清理缓存
    packages
    headers
    metadata
    dbcache
    all
    
repolist        //显示repo列表及其简要信息
    all
    enabled     //默认项
    disabled
    
install         //安装
    yum install packages [...]
    
update          //升级
    yum update packages [...]
update_to       //升级为指定版本

downgrade package1 [package2 ...]   //降级

remove|erase    //卸载

info    //显示rpm -qi package的结果
    yum info packages
    
provides|whatprovides   //查看指定的文件或特性是由哪个包安装生成的

search string1 [string2 ...]    //以指定的关键字搜索程序包名及summary信息

deplist package [package2 ...]  //显示指定包的依赖关系

history     //查看yum的历史事务信息

localinstall    //安装本地rpm包,自动解决依赖关系

grouplist       //列出可用的组

groupinstall "group name"   //安装一组软件
        
createrepo命令    //创建yum仓库的元数据信息

9.4.删除软件包

[root@localhost ~]# yum -y remove httpd
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-67.el7 将被 删除
--> 解决依赖关系完成

猜你喜欢

转载自www.cnblogs.com/liping0826/p/11598146.html