天天看點

Linux系統程式包管理之rpm

Linux系統程式包管理之rpm

==============================================================================

概述:

  本章内容:軟體的運作環境,軟體包基礎,rpm包管理,yum管理,定制yum倉庫,編譯安裝

軟體運作環境

★API:Application Programming Interface (應用程式開發接口)

☉POSIX:Portable OS(國際标準)

  • 程式源代碼--> 預處理--> 編譯--> 彙編--> 連結

★ABI:Application Binary Interface(應用二進制程式接口)

☉Windows與Linux不相容

  • linux:檔案格式:ELF(Executable and Linkable Format)
  • win:檔案格式:exe,msl PE(Portable Executable)
☉庫級别虛拟化
  • Linux:WINE(提供模拟windows的庫,運作win環境)
  • Windows:Cywin(提供linux的運作環境)

★開發語言:

☉系統級開發:

  • C/C++(依賴于c庫);
  • httpd,nginx,vsftpd,go
☉應用級開發:
  • java(依賴于jvm虛拟機)
  • Python(openstack雲站),php,perl(依賴于per解釋器),ruby

★程式格式

☉c/c++

  • 源代碼:文本格式的程式代碼;
  • 編譯開發環境:編譯器,頭檔案,開發庫
  • 二進制格式:文本格式的程式代碼-->編譯器-->二進制格式(二進制程式、庫檔案、配置檔案、幫助檔案)、
☉java/Python
  • 源代碼:編譯能夠在其虛拟機(jvm/pvm)運作的格式;
  • 開發環境:編譯器、開發庫;
  • 二進制
★項目建構工具
  • c/c++:make
  • jave:maven

包管理

 1.概述:

★二進制應用程式的組成部分:
  • 二進制檔案、庫檔案、配置檔案、幫助檔案
★程式包管理:
  • 源代碼-->目标二進制格式(二進制程式、庫檔案、配置檔案、幫助檔案)-->組織成為一個或有限幾個“封包件”(安裝,更新,解除安裝,查詢,校驗)
☉程式包管理器:
  • deblan:dpt,dpkg  以".deb"字尾;
  • redhat:rpm (redhat package manager) 以 ".rpm"字尾 ;
  • S.u.S.E:rpm, ".rpm"指令

★包命名格式

☉源代碼:name-VERSION.tar.gz|bz2|xz

  • ERSION: major(主版本号).minor(次版本号).release(發行号)
☉rpm包命名格式:name-VERSION-release.arch.rpm
  • VERSION:major.minor.release.arc
  • release:rpm包發行号
  • changelog文檔(内容修複改進的相關文檔)
  • 常見的arch:

      x86: i386, i486, i586, i686

      x86_64: x64, x86_64, amd64powerpc: ppc

      跟平台無關:noarch (适用于所有平台)

Linux系統程式包管理之rpm

示例:

bash-4.2.46-19.el7.x86_64.rpm

release:release.OS

★包:分類和拆包

  • Application-VERSION-ARCH.rpm:主包
  • Application-devel-VERSION-ARCH.rpm:開發子包
  • Application-utils-VERSION-ARHC.rpm:其它子包
  • Application-libs-VERSION-ARHC.rpm:其它子包
Linux系統程式包管理之rpm

☉包之間:可能存在依賴關系,甚至循環依賴

     自動解決依賴包管理前端工具:

  • yum:rpm包管理器的前端工具;
  • apt-get:deb包管理器前端工具;
  • zypper:suse上的rpm前端管理工具;
  • dnf:Fedora 18+ rpm包管理器前端管理工具

 2.庫檔案:

★檢視二進制程式所依賴的庫檔案:
  • ldd /PATH/TO/BINARY_FILE
★管理及檢視本機裝載的庫檔案:
  • ldconfig;
  • /sbin/ldconfig-p: 顯示本機已經緩存的所有可用庫檔案名及檔案路徑映射關系
  • 配置檔案:/etc/ld.so.conf, /etc/ld.so.conf.d/*.conf
  • 緩存檔案:/etc/ld.so.cache

 3.包管理器:

★程式包管理器:

☉功能:

  • 将編譯好的應用程式的各組成檔案打包一個或幾個程式封包件,進而友善快捷地實作程式包的安裝、解除安裝、查詢、更新和校驗等管理操作
☉程式包的組成清單:(每個程式包單獨實作)
  • RPM包内的檔案清單
  • RPM的中繼資料,如名稱,版本,依賴性,描述等
  • 安裝或解除安裝時運作的腳本

☉資料庫(公共)

    路徑:/var/lib/rpm

  • 程式包名稱及版本;
  • 依賴關系;
  • 功能說明;
  • 包安裝後生成的各檔案路徑及校驗碼資訊

 3.程式包的來源:

★管理程式包的方式:
  • 使用包管理器:rpm
  • 使用前端工具:yum, dnf

★擷取程式包的途徑:

☉系統開發版的CD光牒或官方的伺服器(或CentOS鏡像站點)

  • https://www.centos.org/download/
  • http://mirrors.aliyun.com
  • http://mirrors.sohu.com
  • http://mirrors.163.com

☉項目官方站點

☉第三方組織:

Fedora-EPEL:Extra Packages for Enterprise Linux

  • Rpmforge:RHEL推薦,包很全
搜尋引擎:
  • http://pkgs.org;
  • http://rpmfind.net;
  • http://rpm.pbone.net;
  • https://sourceforge.net/
☉自己制作
  • 注意:檢查其合法性:來源合法性,程式包的完整性

rpm 指令管理程式包詳解

 1.rpm文法及選項概述:

★rpm:
  • 安裝、更新、解除安裝、查詢和校驗、資料庫維護(都是通過rpm指令來實作的)
☉文法:
  • rpm [OPTIONS] [PACKAGE_FILE]
☉選項:
  • -i,--install:安裝
  • -U,--update,-F,--freshen:更新
  • -e,--erase:解除安裝
  • -q,--query:查詢
  • -V,--verify:校驗
  • --builddb,--initdb:資料庫維護

 2.安裝及子選項:

★文法:
  • rpm {-i|--install} [install-options] PACKAGE_FILE…
  • -v:verbose 顯示詳細資訊;
  • -vv:更詳細的輸出;
  • -h: hash marks 輸出進度條;每個#号表示2%的進度
  • --test:測試安裝,檢查并報告依賴關系及沖突關系,但不真正執行安裝;稱為dry run(幹跑)模式;
  • --nodeps:忽略依賴關系,不建議;
  • --replacepkgs:替代原來的包,重新安裝(先把由原來的配置檔案删除再重裝)
  • --nosignature:不檢查包簽名資訊,不檢查來源合法性
  • --nodigest:不檢查包完整性

     注意:rpm自帶四類腳本(--noscripts)

       %pre:安裝前腳本;   --nopre

       %post:安裝後腳本;  --nopost

       %preun:解除安裝前腳本; --nopreun

       %postun:解除安裝後腳本;--nopostun

☉真正安裝執行操作使用的指令
  • rpm -ivh PACKAGE_FILE

 3.更新及子選項:

  • rpm {-U|--upgrade} [install-options] PACKAGE_FILE...
  • rpm {-F|--freshen} [install-options] PACKAGE_FILE...
注意:
  • -U(upgrade):安裝有舊版程式包,則“更新”如果不存在舊版程式包,則“安裝”
  • -F(freshen):安裝有
  • 舊版程式包,則“更新”如果不存在舊版程式包,則不執行更新操作
★更新指令:
  • rpm -Uvh PACKAGE_FILE ...
  • rpm -Fvh PACKAGE_FILE ...
  • --oldpackage:降級(復原操作)
  • --force: 強行更新
  • 不要對核心做更新操作;Linux支援多核心版本并存,是以,可以直接安裝新版本核心;
  • 如果源程式包的配置檔案安裝後曾被修改,更新時,新版本的提供的同一個配置檔案并不會直接覆寫老版本的配置檔案,而把新版本的檔案重命名(FILENAME.rpmnew)後保留。

 4.解除安裝及子選項:

★作用:
  • 移除已安裝的程式包
  • rmp {-e|--erase}[--allmatches][--nodeps][--test] PACKAGE_NAME
  • 解除安裝和查詢是包的名,因為包已經存在了;而安裝和更新,包是不存在的,是以是包的檔案路徑
★簡單用法
  • rpm -e PACKAGE_NAME...
  • --allmatches:解除安裝所有比對名稱的程式包各版本
  • --nodeps:忽略依賴關系
  • --test:測試解除安裝,dry run 模式

 5.查詢:

  • rpm {-q|--query} [select-options] [query-options]
☉[select-options] 挑選選項
  • -q PACKAGE_NAME:查詢某包或某些包是否安裝
  • -qa:查詢已安裝的所有包
  • -qf FILE:檢視指定的檔案由哪個程式包安裝生成
  • -p PACKAGE_FILE:針對尚未安裝的程式封包件做查詢操作;
  • --whatprovides CAPABILITY(能力):查詢指定的CAPABILITY由哪個包所提供
  • --whatrequires CAPABILITY:查詢指定的CAPABILITY被哪個包所依賴
  • rpm2cpio 封包件|cpio–itv預覽包内檔案
  • rpm2cpio 封包件|cpio–id “*.conf”釋放包内檔案
☉[query-options] 查詢選項
  • --changelog:查詢rpm包的changelog(變更記錄)
  • -c:查詢指定的程式包的配置檔案
  • -d:查詢程式包安裝完成後所生成的幫助文檔
  • -qi,-info:程式包相關的資訊,版本号,大小,所屬的包組等
  • -l,--list:檢視指定的程式包安裝後生成的所有檔案清單;
  • --scripts:檢視程式包自帶的腳本片斷
  • -R,--requires:查詢指定的程式包所依賴的CAPABILITY;
  • --provides:列出指定程式包所提供的CAPABILITY
☉常用組合用法:
  • -qi PACKAGE;-qf FILE;-qc PACJAGE;-ql PACKAGE;-qd PACKAGE ;-qpi PACKAGE_FILE;-qpl PACKAGE_FILE;-qpc PACKAGE_FILE,.. 

示範:

[root@centos7 ~]# rpm -q tree
tree-1.6.0-10.el7.x86_64

[root@centos7 ~]# rpm -ql tree
/usr/bin/tree
/usr/share/doc/tree-1.6.0
/usr/share/doc/tree-1.6.0/LICENSE
/usr/share/doc/tree-1.6.0/README
/usr/share/man/man1/tree.1.gz

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

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

[root@centos7 ~]# rpm -qi bash
Name        : bash
Version     : 4.2.46
Release     : 19.el7
Architecture: x86_64
Install Date: 2016年11月06日 星期日 18時31分30秒
Group       : System Environment/Shells
Size        : 3663618
License     : GPLv3+
Signature   : RSA/SHA256, 2015年11月25日 星期三 22時14分53秒, Key ID 24c6a8a7f4a80eb5
Source RPM  : bash-4.2.46-19.el7.src.rpm
Build Date  : 2015年11月20日 星期五 13時04分53秒
Build Host  : worker1.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.

[root@centos7 ~]# rpm -qc httpd
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf.modules.d/00-base.conf
/etc/httpd/conf.modules.d/00-dav.conf
/etc/httpd/conf.modules.d/00-lua.conf
/etc/httpd/conf.modules.d/00-mpm.conf
/etc/httpd/conf.modules.d/00-proxy.conf
/etc/httpd/conf.modules.d/00-systemd.conf
/etc/httpd/conf.modules.d/01-cgi.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/magic
/etc/logrotate.d/httpd
/etc/sysconfig/htcacheclean
/etc/sysconfig/httpd

[root@centos7 ~]# rpm -qd httpd
/usr/share/doc/httpd-2.4.6/ABOUT_APACHE
/usr/share/doc/httpd-2.4.6/CHANGES
/usr/share/doc/httpd-2.4.6/LICENSE
/usr/share/doc/httpd-2.4.6/NOTICE
/usr/share/doc/httpd-2.4.6/README
/usr/share/doc/httpd-2.4.6/VERSIONING
/usr/share/doc/httpd-2.4.6/httpd-dav.conf
/usr/share/doc/httpd-2.4.6/httpd-default.conf
/usr/share/doc/httpd-2.4.6/httpd-info.conf
/usr/share/doc/httpd-2.4.6/httpd-languages.conf
/usr/share/doc/httpd-2.4.6/httpd-manual.conf
/usr/share/doc/httpd-2.4.6/httpd-mpm.conf
/usr/share/doc/httpd-2.4.6/httpd-multilang-errordoc.conf
/usr/share/doc/httpd-2.4.6/httpd-vhosts.conf
/usr/share/doc/httpd-2.4.6/proxy-html.conf
/usr/share/man/man8/apachectl.8.gz
/usr/share/man/man8/fcgistarter.8.gz
/usr/share/man/man8/htcacheclean.8.gz
/usr/share/man/man8/httpd.8.gz
/usr/share/man/man8/rotatelogs.8.gz
/usr/share/man/man8/suexec.8.gz      

 6.校驗:

  • rpm {-V|--verify} [select-options] [verify-options]
☉常見用法:
  • rpm -V PACHAGE_NAME
當沒有輸出結果時表示軟體包完整ok,當有相應的結果輸出表明對應的内容有修改,及具體如下:
  • S:file Size differs(檔案的容量大小是否被改變);
  • M:Mode differs (includes permissions and file type)檔案的類型或屬性是否被改變;
  • 5:digest (formerly MD5 sum) differs  這是一種指紋内容已經不同;
  • D:Device major/minor number mismatch 裝置的主/次代碼已經改變;
  • L:readLink(2) path mismatch  路徑已被改變
  • U: User ownership differs  檔案的屬主已被改變
  • G:Group ownership differs  檔案的屬組已被改變;
  • T:mTimediffers 檔案的建立時間已被改變;
  • P:capabilities differ 
★包來源合法性驗正及完整性驗正:
  • 完整性驗正:SHA256
  • 來源合法性驗正:RSA
☉擷取并導入信任的包制作者的秘鑰
  • 對于CentOS發行版來說:rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 
◆驗證:
  • 安裝此組織簽名的程式時,會自動執行驗證;
  • 手動驗證:rpm -K PACKAGE_FILE
◆加密方法:  
  • 公鑰加密:
  • 對稱加密:加密、解密使用同一密鑰;
  • 非對稱加密:密鑰是成對兒的

      public key:公鑰,公開所有人

      secret key:私鑰, 不能公開

◆導入所需要的公鑰:  
  • rpm -K |checksigrpmfile:檢查包的完整性和簽名
  • rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  • CentOS 7發行版CD光牒提供:RPM-GPG-KEY-CentOS-7
  • rpm -qagpg-pubkey*
[root@centos7 ~]# rpm -V httpd
遺漏   c /etc/httpd/conf.d/welcome.conf
S.5....T.  c /etc/httpd/conf/httpd.conf      

 7.資料庫重建:

★rpm資料庫路經:
  • /var/lib/rpm
  • 查詢操作:通過此處得到資料庫進行
★用法:
  • rpm {--initdb|--rebuilddb}
  • --initdb: 初始化資料庫。目前無任何資料庫,則建立之;目前有時不執行任何操作。
  • --rebuilddb:重新建構。無論目前存在與否,都會直接重新建立資料庫。
☉擷取幫助
  • CentOS6 man rpm
  • CentOS7 man rpmdb 

繼續閱讀