zoukankan      html  css  js  c++  java
  • Linux学习38 Linux软件包管理-rpm命令介绍及实战应用

    一、rpm

      1、安装

        见上节

      2、升级

        a、命令

          rpm {-U|--upgrade} [install-options] PACKAGE_FILE ...

          -U:升级或安装;

          -F:升级,不支持安装,如果拥有老版本的包时才会升级,否则不会安装

          rpm -Uvh PACKAGE_FILE ...

          rpm -Fvh PACKAGE_FILE ...

          相应选项和安装是通用的,接下来说几个额外选项

            --oldpackage:降级;

            --force:强制升级;

          注意:

            (1)、不要对内核做升级操作;Linux支持多内核版本并存,因此,直接 安装新版本内核即可。

            (2)、如果某源程序包的配置文件安装后曾被修改过,升级时,新版本的程序提供的同一个配置文件不会覆盖原有版本的配置文件,而是把新版本的配置文件重命名(FILENAME.rpmnew)后提供。

      3、卸载

        a、命令 

          rpm {-e|--erase} [--allmatches] [--justdb] [--nodeps] [--noscripts]
          [--notriggers] [--test] PACKAGE_NAME ...

    [root@localhost ~]# rpm -e zsh
    [root@localhost ~]#

          --allmatches:卸载所有匹配的指定名称的程序包的各版本

          --nodeps:忽略依赖关系

          --test:测试卸载,dry run 模式

      4、查询

        a、命令

          rpm {-q|--query} [select-options] [query-options]

           (1)、[select-options] 

            PACKAGE_NAME:查询指定的程序包是否已经安装,及其版本

    [root@localhost ~]# rpm -q zsh
    package zsh is not installed
    [root@localhost ~]# rpm -ivh /mnt/Packages/zsh-5.0.2-28.el7.x86_64.rpm 
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:zsh-5.0.2-28.el7                 ################################# [100%]
    [root@localhost ~]# rpm -q zsh
    zsh-5.0.2-28.el7.x86_64

            -a,--all 查询所有已经安装的包

    [root@localhost ~]# rpm -qa|grep zsh
    zsh-5.0.2-28.el7.x86_64

            -f FILE:查询指定的文件由哪个程序包安装生成

    [root@localhost ~]# rpm -qf /etc/fstab 
    setup-2.8.71-9.el7.noarch

            -p,--package PACKAGE_FILE:用于实现对未安装的程序包执行查询操作。

    [root@localhost ~]# rpm -ql /mnt/Packages/zsh-5.0.2-28.el7.x86_64.rpm
    package /mnt/Packages/zsh-5.0.2-28.el7.x86_64.rpm is not installed
    [root@localhost ~]# rpm -qpl /mnt/Packages/zsh-5.0.2-28.el7.x86_64.rpm |wc -l
    988

            --whatprovides CAPABILITY:查询指定的CAPABILITY由哪个程序包提供

    [root@localhost ~]# rpm -q --whatprovides 'config(bash)'
    bash-4.2.46-30.el7.x86_64

            --whatrequires CAPABILITY:查询指定的CAPABILITY被哪个包所依赖

    [root@localhost ~]# rpm -q --whatrequires bash
    dracut-033-535.el7.x86_64
    initscripts-9.49.41-1.el7.x86_64
    lvm2-2.02.177-4.el7.x86_64
    rsyslog-8.24.0-16.el7.x86_64
    kpatch-0.4.0-3.el7.noarch
    bash-completion-2.1-6.el7.noarch

          (2)、[query-options]

            --changelog:查询rpm包的changelog,这里只是rpm包的changelog而不是源码包的changelog

    [root@localhost ~]# rpm -q --changelog zsh |more

            -l,--list:程序包安装生成的所有文件列表

    [root@localhost ~]# rpm -ql bash|head -4
    /etc/skel/.bash_logout
    /etc/skel/.bash_profile
    /etc/skel/.bashrc
    /usr/bin/alias

            -i,--info:程序包相关的信息,版本号,大小,所属的包组等等

    [root@localhost ~]# rpm -qi bash
    Name        : bash
    Version     : 4.2.46
    Release     : 30.el7
    Architecture: x86_64
    Install Date: Wed 24 Oct 2018 05:19:15 AM CST
    Group       : System Environment/Shells
    Size        : 3667709
    License     : GPLv3+
    Signature   : RSA/SHA256, Wed 25 Apr 2018 06:54:19 PM CST, Key ID 24c6a8a7f4a80eb5
    Source RPM  : bash-4.2.46-30.el7.src.rpm
    Build Date  : Wed 11 Apr 2018 08:55:22 AM CST
    Build Host  : x86-01.bsys.centos.org
    Relocations : (not relocatable)
    Packager    : CentOS BuildSystem <http://bugs.centos.org>
    Vendor      : CentOS
    URL         : http://www.gnu.org/software/bash
    Summary     : The GNU Bourne Again shell
    Description :
    The GNU Bourne Again shell (Bash) is a shell or command language
    interpreter that is compatible with the Bourne shell (sh). Bash
    incorporates useful features from the Korn shell (ksh) and the C shell
    (csh). Most sh scripts can be run by bash without modification.
    You have new mail in /var/spool/mail/root

            -c,--configfiles:查询指定的程序包提供的配置文件

    [root@localhost ~]# rpm -qc bash
    /etc/skel/.bash_logout
    /etc/skel/.bash_profile
    /etc/skel/.bashrc

            -d,--dockfiles:查询指定的程序包提供的文档

    [root@localhost ~]# rpm -qd bash

            --provides:列出指定的程序包提供的所有CAPABILITY

    [root@localhost ~]# rpm -q --provides bash
    /bin/bash
    /bin/sh
    bash = 4.2.46-30.el7
    bash(x86-64) = 4.2.46-30.el7
    config(bash) = 4.2.46-30.el7
    [root@localhost ~]# rpm -q --whatprovides 'config(bash)'
    bash-4.2.46-30.el7.x86_64
    [root@localhost ~]# rpm -q --whatrequires bash
    dracut-033-535.el7.x86_64
    initscripts-9.49.41-1.el7.x86_64
    lvm2-2.02.177-4.el7.x86_64
    rsyslog-8.24.0-16.el7.x86_64
    kpatch-0.4.0-3.el7.noarch
    bash-completion-2.1-6.el7.noarch

            -R,--requires:查询指定的程序包的依赖关系

    [root@localhost ~]# rpm -q -R bash
    /bin/sh
    config(bash) = 4.2.46-30.el7
    libc.so.6()(64bit)
    libc.so.6(GLIBC_2.11)(64bit)
    libc.so.6(GLIBC_2.14)(64bit)
    libc.so.6(GLIBC_2.15)(64bit)
    libc.so.6(GLIBC_2.2.5)(64bit)
    libc.so.6(GLIBC_2.3)(64bit)
    libc.so.6(GLIBC_2.3.4)(64bit)
    libc.so.6(GLIBC_2.4)(64bit)
    libc.so.6(GLIBC_2.8)(64bit)
    libdl.so.2()(64bit)
    libdl.so.2(GLIBC_2.2.5)(64bit)
    libtinfo.so.5()(64bit)
    rpmlib(BuiltinLuaScripts) <= 4.2.2-1
    rpmlib(CompressedFileNames) <= 3.0.4-1
    rpmlib(FileDigests) <= 4.6.0-1
    rpmlib(PayloadFilesHavePrefix) <= 4.0-1
    rtld(GNU_HASH)
    rpmlib(PayloadIsXz) <= 5.2-1

            --scripts:查看程序包自带的脚本片段

    [root@localhost ~]# rpm -q --scripts bash
    postinstall scriptlet (using <lua>):  #安装前
    nl        = '
    '
    sh        = '/bin/sh'..nl
    bash      = '/bin/bash'..nl
    f = io.open('/etc/shells', 'a+')
    if f then
      local shells = nl..f:read('*all')..nl
      if not shells:find(nl..sh) then f:write(sh) end
      if not shells:find(nl..bash) then f:write(bash) end
      f:close()
    end
    postuninstall scriptlet (using <lua>):  #卸载前
    -- Run it only if we are uninstalling
    if arg[2] == "0"
    then
      t={}
      for line in io.lines("/etc/shells")
      do
        if line ~= "/bin/bash" and line ~= "/bin/sh"
        then
          table.insert(t,line)
        end
      end
    
      f = io.open("/etc/shells", "w+")
      for n,line in pairs(t)
      do
        f:write(line.."
    ")
      end
      f:close()
    end

        b、用法

          -qi PACKAGE | -qpi PACKAGE

          -qf FILE | -qpf FILE

          -qc PACKAGE | -qpc PACKAGE

          -ql PACKAGE | -qpl PACKAGE

          -qd PACKAGE | -qpd PACKAGE

      5、校验

        a、命令

          rpm {-V|--verify} [select-options] [verify-options]

          首先我们来校验我们的zsh包是否有问题

    [root@localhost ~]# rpm -V zsh

          我们现在来修改某个配置文件后再查看

    [root@localhost ~]# rpm -V zsh
    S.5....T.     /usr/share/zsh/5.0.2/functions/zfput

            前面的字母分别代表的意义为:

              S file Size differs  #文件大小发生改变
              M Mode differs (includes permissions and file type) #权限发生改变
              5 digest (formerly MD5 sum) differs #文件的md5码改变了
              D Device major/minor number mismatch #主次设备号不匹配
              L readLink(2) path mismatch #
              U User ownership differs #属主改了
              G Group ownership differs #属组改了
              T mTime differs #最近一次的时间戳改了
              P caPabilities differ #功能发生改变了

      6、包来源合法性验证和完整性验证

        a、来源合法性验证:

        b、完整性验证

          数字签名

          现在我们制作者制作出了一个rpm包,现在第一步他使用单项加密算法提取出这个包的特征码来,特征码是定长的。第二步他会用自己的私钥去加密这段特征码,把特征码加密后附加在这个文件的后面。这就叫数字签名;然后这个包就可以扔到互联网上去了,然后有一个使用者拿到这个包了,拿到后他不能上来就安装的,万一这个包被篡改过就惨了,因此要验证包来源合法性。首先我们需要拿到制作者的公钥,然后去解密这段特征码,如果能解密,我们就认为这是制作者加密的,因为他的私钥只有他一个人有,也就意味着加密只能他一个人做,这样来源合法性就验证了。那么这个包有没有被改过呢?然后我们可以用一样的单项加密算法对包做计算,这样也可以得到一个特征码。刚刚我们解密也有一个特征码,二者相比较,只要文件数据没改变那么其特征码一定是一样的。如果二者一样就说明完整性得到了验证。那么我们怎么才能拿到对方的公钥呢?就需要用到我们的三方机构CA。

            

         c、获取并导入信任的包制作者的密钥

          对于CentOS发行版来说:

            (1)、我们对rpm包做验证比较简单,只需要把rpm包的制作者公钥导入即可。而对于我们光盘中来讲,我们rpm制作者的公钥就在光盘上

    [root@localhost ~]# ls /mnt/RPM-GPG-KEY-CentOS-7 
    /mnt/RPM-GPG-KEY-CentOS-7

            (2)、现在我们将公钥导入进去,然后就可以进行包安装了,也就不会报错了

    [root@localhost ~]# rpm --import /mnt/RPM-GPG-KEY-CentOS-7 
    [root@localhost ~]# 

            (3)、我们也可以手动验证

    [root@localhost ~]# rpm -K /mnt/Packages/zsh-5.0.2-28.el7.x86_64.rpm 
    /mnt/Packages/zsh-5.0.2-28.el7.x86_64.rpm: rsa sha1 (md5) pgp md5 OK

            (4)、我们其实也可以在另一个路径下找到相应的公钥文件

    [root@localhost ~]# ls /etc/pki/rpm-gpg/
    RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Debug-7  RPM-GPG-KEY-CentOS-Testing-7

        d、验证

          (1)、安装此组织签名的程序时,会自动验证

          (2)、手动验证:rpm -K PACKAGE_FILE

      7、数据库重建

        a、数据库路径为 /var/lib/rpm

    [root@localhost ~]# ls /var/lib/rpm
    Basenames  Conflictname  __db.001  __db.002  __db.003  Dirnames  Group  Installtid  Name  Obsoletename  Packages  Providename  Requirename  Sha1header  Sigmd5  Triggername

          查询操作:通过此处的数据库进行

        b、获取帮助

          CentOS 6 : man rpm 

          CentOS 7 : man rpmdb

          rpm {--initdb|--rebuilddb} [--dbpath DIRECTORY] [--root DIRECTORY]

            --initdb:初始化数据库,当前无任何数据库可初始化创建一个新的;当前有时不执行任何操作

            --rebuilddb:重新构建,通过读取当前系统上所有已经安装过的程序包进行重新创建

    [root@localhost ~]# mkdir /tmp/rpm
    [root@localhost ~]# rpm --initdb --dbpath=/tmp/rpm/
    [root@localhost ~]# ls /tmp/rpm/
    Basenames  Conflictname  __db.001  __db.002  __db.003  Dirnames  Group  Installtid  Name  Obsoletename  Packages  Providename  Requirename  Sha1header  Sigmd5  Triggername
    [root@localhost ~]# rpm --rebuilddb --dbpath=/tmp/rpm/
    [root@localhost ~]# ls /tmp/rpm/
    Basenames  Conflictname  Dirnames  Group  Installtid  Name  Obsoletename  Packages  Providename  Requirename  Sha1header  Sigmd5  Triggername
  • 相关阅读:
    一套简单通用的Java后台管理系统(SpringBoot +Security + Layui实现一套权限管理后台模板)
    org.thymeleaf.exceptions.TemplateInputException: Error resolving template [cart/getCurrentUserCartItem], template might not exist or might not be accessible by any of the configured Template Resolvers
    ARTS-WEEK-009
    ARTS-WEEK-008
    从一次外卖到对oauth2.0的思考
    网络滴神,TCP!
    浪里来浪里去!网络协议如何成就网上冲浪?
    IO、NIO实现简单聊天室,附带问题解析
    IntelliJ IDEA 中使用 Lambok (注解无效问题的解决)
    M5310-A 版本
  • 原文地址:https://www.cnblogs.com/Presley-lpc/p/12347026.html
Copyright © 2011-2022 走看看