一、软件包管理核心功能
1、软件包制作
2、包管理器:打包,安装、升级、卸载、查询及校验
3、工具:rpm 、deb
4、程序包的组成部分:
二进制程序:/bin, /sbin,/ /usr/bin, /usr/sbin,
库文件:/lib64, /usr/lib64
配置文件:/etc
帮助文件:manual, info
5、rpm包管理
rpm:数据库 /var/lib/rpm
rpmbuild:建立软件管理数据库
rpm包默认为二进制格式,有rpm包作者下载源码程序,编译完成后,制作成rpm包
rpm包命名
主包:bind-9.7.1-1.el5.i586.rpm
子包:bind-libs-9.7.1-1.el5.i586.rpm;bind-utils-9.7.1-1.el5.i586.rpm
包名格式:name-VERSION-RELEASE.ARCH.rpm(bind-major.minor.release-release.arch.rpm)
依赖关系:x--->y--->z
6、如何获取rpm包
(1)发行版提供的程序包
(2)Fedora-EPEL
(3)项目的官方站点
(4)搜索引擎
http://rpmfind.net
http://rpm.pbone.net
http://pkgs.org
二、基于rpm命令实现程序包管理
1、安装
rpm {-i|--install} [install-options] PACKAGE_FILE ...
-i:
-v:显示详细过程(vv更详细)
-h:以#显示进度;每个#表示2%
默认使用 rpm -ivh /path/to/packagefile
--nodeps:忽略依赖关系
--replacepkgs:重新安装,替换原有安装
--force:强行安装,可以实现重装或降级
[root@cwc01 Packages]# rpm -ivh zsh-html-4.3.10-7.el6.x86_64.rpm Preparing... ########################################### [100%] 1:zsh-html ########################################### [100%]
2、升级
rpm -Uvh /path/to/New_Package_File:如果装有老版本的包,则升级,否则,则安装
rpm -Fvh /path/to/New_Package_File:如果装有老版本的包,则升级,否则,退出
--oldpackage:降级
[root@cacti Packages]# rpm -Uvh /mnt/cdrom/Packages/zsh-4.3.10-5.el6.x86_64.rpm Preparing... ########################################### [100%] 1:zsh ########################################### [100%] [root@cacti Packages]# rpm -Uvh /mnt/cdrom/Packages/zsh-4.3.10-5.el6.x86_64.rpm Preparing... ########################################### [100%] package zsh-4.3.10-5.el6.x86_64 is already installed [root@cacti Packages]# rpm -Fvh /mnt/cdrom/Packages/zsh-4.3.10-5.el6.x86_64.rpm
3、卸载
rpm -e {--nodeps} Package_Name
[root@cacti Packages]# rpm -q zsh zsh-4.3.10-5.el6.x86_64 [root@cacti Packages]# rpm -e zsh [root@cacti Packages]# rpm -q zsh package zsh is not installed
4、查询
rpm -q Package_Name:查询指定的包是否已经安装
rpm -qa :查询已经安装的所有包
rpm -qi Package_Name:查询指定包的说明信息
[root@cacti Packages]# rpm -q zsh zsh-4.3.10-5.el6.x86_64 [root@cacti Packages]# rpm -qi zsh Name : zsh Relocations: (not relocatable) Version : 4.3.10 Vendor: CentOS Release : 5.el6 Build Date: Sat 23 Jun 2012 07:04:50 AM CST Install Date: Tue 15 Sep 2015 04:32:34 PM CST Build Host: m4500n Group : System Environment/Shells Source RPM: zsh-4.3.10-5.el6.src.rpm Size : 5009102 License: BSD Signature : RSA/SHA1, Sat 23 Jun 2012 09:16:42 PM CST, Key ID 0946fca2c105b9de Packager : CentOS BuildSystem <http://bugs.centos.org> URL : http://zsh.sunsite.dk/ Summary : A powerful interactive shell Description : The zsh shell is a command interpreter usable as an interactive login shell and as a shell script command processor. Zsh resembles the ksh shell (the Korn shell), but includes many enhancements. Zsh supports command line editing, built-in spelling correction, programmable command completion, shell functions (with autoloading), a history mechanism, and more.
rpm -ql Package_Name:查询指定包安装后生成的文件列表
[root@cacti Packages]# rpm -ql zsh /bin/zsh /etc/skel/.zshrc /etc/zlogin /etc/zlogout /etc/zprofile /etc/zshenv /etc/zshrc /usr/lib64/zsh /usr/lib64/zsh/4.3.10 /usr/lib64/zsh/4.3.10/zsh /usr/lib64/zsh/4.3.10/zsh/attr.so /usr/lib64/zsh/4.3.10/zsh/cap.so /usr/lib64/zsh/4.3.10/zsh/clone.so /usr/lib64/zsh/4.3.10/zsh/compctl.so /usr/lib64/zsh/4.3.10/zsh/complete.so
rpm -qc Package_Name:查询指定包安装生成的配置文件
[root@cacti Packages]# rpm -qc zsh /etc/skel/.zshrc /etc/zlogin /etc/zlogout /etc/zprofile /etc/zshenv /etc/zshrc
rpm -qd Package_Name:查询指定包安装生成的帮助文档
rpm -qc --scripts Package_Name:查询指定包中包含的脚本
preinstall: 安装前脚本
postinstall:安装后脚本
preuninstall: 卸载前脚本
postuninstall: 卸载后脚本
[root@cacti Packages]# rpm -q --scripts zsh postinstall scriptlet (using /bin/sh): if [ ! -f /etc/shells ] ; then echo "/bin/zsh" > /etc/shells else grep -q "^/bin/zsh$" /etc/shells || echo "/bin/zsh" >> /etc/shells fi if [ -f /usr/share/info/zsh.info.gz ]; then # This is needed so that --excludedocs works. /sbin/install-info /usr/share/info/zsh.info.gz /usr/share/info/dir \ --entry="* zsh: (zsh). An enhanced bourne shell." fi : preuninstall scriptlet (using /bin/sh): if [ "$1" = 0 ] ; then if [ -f /usr/share/info/zsh.info.gz ]; then # This is needed so that --excludedocs works. /sbin/install-info --delete /usr/share/info/zsh.info.gz /usr/share/info/dir \ --entry="* zsh: (zsh). An enhanced bourne shell." fi fi : postuninstall scriptlet (using /bin/sh): if [ "$1" = 0 ] ; then if [ -f /etc/shells ] ; then TmpFile=`/bin/mktemp /tmp/.zshrpmXXXXXX` grep -v '^/bin/zsh$' /etc/shells > $TmpFile cp -f $TmpFile /etc/shells rm -f $TmpFile fi fi
rpm -qf /path/to/somefile:查询指定的文件是由哪个rpm包安装生成的
[root@cacti sbin]# rpm -qf /usr/sbin/httpd httpd-2.2.15-39.el6.centos.x86_64
rpm -qpi /path/to/Package_Name:如果某rpm包未安装,我们需要查询其说明信息、安装后生成的文件
[root@cacti Packages]# rpm -qpi /mnt/cdrom/Packages/xz-4.999.9-0.3.beta.20091007git.el6.x86_64.rpm Name : xz Relocations: (not relocatable) Version : 4.999.9 Vendor: CentOS Release : 0.3.beta.20091007git.el6 Build Date: Mon 23 Aug 2010 01:12:38 AM CST Install Date: (not installed) Build Host: c6b6.bsys.dev.centos.org Group : Applications/File Source RPM: xz-4.999.9-0.3.beta.20091007git.el6.src.rpm Size : 487887 License: LGPLv2+ Signature : RSA/8, Sun 03 Jul 2011 01:06:47 PM CST, Key ID 0946fca2c105b9de Packager : CentOS BuildSystem <http://bugs.centos.org> URL : http://tukaani.org/xz/ Summary : LZMA compression utilities Description : XZ Utils are an attempt to make LZMA compression easy to use on free (as in freedom) operating systems. This is achieved by providing tools and libraries which are similar to use than the equivalents of the most popular existing compression algorithms. LZMA is a general purpose compression algorithm designed by Igor Pavlov as part of 7-Zip. It provides high compression ratio while keeping the decompression speed fast.
5、校验
(1)来源合法性验证:采用非对称加密技术
(2)完成性验证:采用单向加密技术
(3)导入密钥:rpm --import /PATH/TO/KEY_FILE
[root@cacti ~]# ls /etc/pki/rpm-gpg/ RPM-GPG-KEY-CentOS-6 RPM-GPG-KEY-CentOS-Security-6 RPM-GPG-KEY-CentOS-Debug-6 RPM-GPG-KEY-CentOS-Testing-6
(4)校验:rpm -K /path/to/rpm-file
[root@cacti ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 [root@cacti ~]# rpm -K /mnt/cdrom/Packages/zsh-4.3.10-5.el6.x86_64.rpm /mnt/cdrom/Packages/zsh-4.3.10-5.el6.x86_64.rpm: rsa sha1 (md5) pgp md5 OK
[root@cacti Packages]# rpm -V zsh [root@cacti Packages]# vim /usr/share/zsh/4.3.10/functions/zfls # function zfls { emulate -L zsh [[ $curcontext = :zf* ]] || local curcontext=:zfls # directory hack, see zfcd if [[ $1 = $HOME || $1 = $HOME/* ]]; then 1="~${1#$HOME}" fi zfautocheck -d zftp ls $* # } # # test [root@cacti Packages]# rpm -V zsh S.5....T. /usr/share/zsh/4.3.10/functions/zfls
rpm重建数据库
rpm --rebuilddb:重建数据库,必须执行
rpm --initdb:初始化数据库,没有才会建立,有就不会创建
[root@cacti ~]# cd /var/lib/rpm/ [root@cacti rpm]# ls Basenames __db.002 Dirnames Installtid Packages Pubkeys Sha1header Conflictname __db.003 Filedigests Name Providename Requirename Sigmd5 __db.001 __db.004 Group Obsoletename Provideversion Requireversion Triggername [root@cacti rpm]# rpm --rebuilddb
三、基于yum的程序包管理
1、YUM运行原理
(1)yum服务器:所有发型的rpm包发在yum服务器上提供别人下载,rpm包根据kernel,cpu各自的版本号分别编译发布,yum服务器提供ftp、httpd、或者file 的形式提供下载,yum服务器的重要环节是整理出每个rpm包的基本信息,包括rpm包对应的版本号,conf文件,二进制文件信息,还有关键的依赖信息,在yum服务器上提供了createrepo工具,用于把rpm包的基本信息做成一张“清单”,这张“清单”就是描述每个rpm包的说明文件信息
(2)yum客户端:client 每次调用yum install或者search的时候,都会去解析/etc/yum.repos.d下面所有以.repo结尾的配置文件,这些配置文件定义了yum服务器的地址,yum会定期去“更新”yum服务器上的rpm包“清单”,然后把“清单”下载保存到yum自己的cache里,根据 /etc/yum.conf里配置(默认/car/cache/yum下面),每次调用yum装包的时候都会去这个cache目录下去寻找“清单”,根据“清单”里的rpm包描述从而来确定安装包的名字,版本号,所需要依赖包,然后再去yum服务器下载rpm包安装,(前提是不存在rpm包的cache)
2、yum配置文件解释
/etc/yum.conf:全局配置,对所有仓库都使用的配置
[main] cachedir=/var/cache/yum/$basearch/$releasever #yum缓存目录,yum在此存储下载的rpm包和数据库,yum服务器生成的包含rpm包相关信息的“清单” keepcache=0 #让缓存一直为空 debuglevel=2 #调试等级,默认为2 logfile=/var/log/yum.log #日志文件 exactarch=1 #是否只升级和安装软件包、cpu体系一致的包,1为是,0为否 obsoletes=1 #update参数,允许更新陈旧的RPM包 gpgcheck=1 #是否进行gpg校验 plugins=1 #是否允许使用插件,默认0不允许,但我们一般会用yum-fastestmirror这个插件 installonly_limit=5 #允许保留多少个内核包 bugtracker_url=http://bugs.centos.org/set_project.php?project_id=16&ref=http://bugs.centos.org/bug_report_page.php?category=yum distroverpkg=centos-release
/etc/yum.repos.d/*.repo:一个文件通常用于一个或一组功能相近或相近的仓库
[base] name=CentOS-$releasever - Base #对repository(软件仓库)的名称描述,并支持$releasever $basearch变量 mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os #查找更新源的地址 #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/ #更新源的直接路径,支持http://ftp://file:// 三种,baseurl只能有一个,后面可跟多个URL gpgcheck=1 #标时是否启用gpg验证,下面提供gpgkey的路径 gpgkey= enable=0 #表示是否应用(0表示不启用,1表示启用)
可用变量说明
$releasever: 引用当前系统的发行版本号;
$arch : CPU 体系,如i386,athlon等
$basearch:cpu的基本体系组,如i686和athlon同属i386,alpha和alphaev6同属alpha
3、解析YUM本地缓存文件
文件路径:/var/cache/yum/x86_64/6
[root@lamp 6]# tree . ├── base │ ├── 0bd8768166bc742b8ba814f3580b3d7121605662bfd2d05828d25f3c3aa5a197-filelists.sqlite │ ├── 11bcf3233241e37e1a0e04d2fde8a609215059b5b0319fd660f796d462bbe6a8-primary.sqlite │ ├── 2b165aac44ec8951578c7b2a29a5d391f1c66e033d79923ebd60ccc285d21c12-other.sqlite │ ├── 486d3ee62873814293a1be370640ae9c71863a35c7299eb76739e03369ad0c3f-c6-x86_64-comps.xml.gz │ ├── cachecookie │ ├── gen │ │ └── groups.xml │ ├── mirrorlist.txt │ ├── packages │ └── repomd.xml ├── epel │ ├── metalink.xml.tmp │ └── packages ├── extras │ ├── 11012b790366ef4d505b821aa6a02f3ee99adf27d45917444e5a66dccdb5c283-filelists.sqlite │ ├── 3b29cce790fa94e697f2c664a0284d0e2f59d5e8e5a415de2d4b838a45914eb4-primary.sqlite │ ├── cachecookie │ ├── mirrorlist.txt │ ├── packages │ └── repomd.xml ├── timedhosts.txt └── updates ├── a4fc756a63f1c3ba2be04555f4aae3e5698c810cda76d9e7e82e2ae727f93879-primary.sqlite ├── cachecookie ├── d7d81490d5d24d821b806adafafa2c0eb402ade84c2142b470918bc777792059-filelists.sqlite ├── mirrorlist.txt ├── packages └── repomd.xml
说明:
(1)*primary.sqlite:yum服务器上的“清单”:所有RPM包的列表,依赖关系,每个RPM包安装生成的文件列表,以sqlite方式存储
(2)*filelists.sqlite:记录了所有rpm包列表,版本号,配置文件等
(3)*other.sqlite:RPM包中额外信息,如修改日志
(4)repomd.xml:包含RPM包分组信息,同时记录上面三个文件的校验值
查看primary表里面的相关信息
[root@lamp base]# sqlite3 11bcf3233241e37e1a0e04d2fde8a609215059b5b0319fd660f796d462bbe6a8-primary.sqlite SQLite version 3.6.20 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> .table conflicts db_info files obsoletes packages provides requires sqlite> select * from packages;
每次yum装包或者卸载的时候都会来查询这个sqlite的DB,然后做出相应的操作
4、yum相关命令
(1)安装、卸载、升级
yum install | remove | update rpm_package
[root@lamp 6]# yum install tree Loaded plugins: fastestmirror, refresh-packagekit Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.btte.net * extras: mirrors.btte.net * updates: mirrors.btte.net base | 3.7 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package tree.x86_64 0:1.5.3-3.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================== Package Arch Version Repository Size =================================================================================================== Installing: tree x86_64 1.5.3-3.el6 base 36 k Transaction Summary =================================================================================================== Install 1 Package(s) Total download size: 36 k Installed size: 65 k Is this ok [y/N]: y Downloading Packages: tree-1.5.3-3.el6.x86_64.rpm | 36 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : tree-1.5.3-3.el6.x86_64 1/1 Verifying : tree-1.5.3-3.el6.x86_64 1/1 Installed: tree.x86_64 0:1.5.3-3.el6 Complete!
[root@lamp base]# yum remove tree Loaded plugins: fastestmirror, refresh-packagekit Setting up Remove Process Resolving Dependencies --> Running transaction check ---> Package tree.x86_64 0:1.5.3-3.el6 will be erased --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================== Package Arch Version Repository Size ================================================================================================================== Removing: tree x86_64 1.5.3-3.el6 @base 65 k Transaction Summary ================================================================================================================== Remove 1 Package(s) Installed size: 65 k Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Erasing : tree-1.5.3-3.el6.x86_64 1/1 Verifying : tree-1.5.3-3.el6.x86_64 1/1 Removed: tree.x86_64 0:1.5.3-3.el6 Complete!
(2)查询
yum serach xx {all }查询
yum provides /path/to/somefile:查询文件是由哪个文件安装产生
[root@cacti yum.repos.d]# yum search httpd Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.btte.net * c6-media: * extras: mirrors.btte.net * updates: mirrors.btte.net =============================================== N/S Matched: httpd =============================================== libmicrohttpd-devel.i686 : Development files for libmicrohttpd libmicrohttpd-devel.x86_64 : Development files for libmicrohttpd libmicrohttpd-doc.noarch : Documentation for libmicrohttpd httpd.x86_64 : Apache HTTP Server httpd-devel.i686 : Development interfaces for the Apache HTTP server httpd-devel.x86_64 : Development interfaces for the Apache HTTP server httpd-manual.noarch : Documentation for the Apache HTTP server httpd-tools.x86_64 : Tools for use with the Apache HTTP Server libmicrohttpd.i686 : Lightweight library for embedding a webserver in applications libmicrohttpd.x86_64 : Lightweight library for embedding a webserver in applications mod_auth_mellon.x86_64 : A SAML 2.0 authentication module for the Apache Httpd Server mod_dav_svn.x86_64 : Apache httpd module for Subversion server mod_dnssd.x86_64 : An Apache HTTPD module which adds Zeroconf support Name and summary matches only, use "search all" for everything.
[root@cacti ~]# yum provides /usr/bin/tree Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.btte.net * c6-media: * extras: mirrors.btte.net * updates: mirrors.btte.net tree-1.5.3-3.el6.x86_64 : File system tree viewer Repo : base Matched from: Filename : /usr/bin/tree tree-1.5.3-2.el6.x86_64 : File system tree viewer Repo : c6-media Matched from: Filename : /usr/bin/tree tree-1.5.3-3.el6.x86_64 : File system tree viewer Repo : installed Matched from: Other : Provides-match: /usr/bin/tree
(3)重新安装与降级
yum reinstall | downgrade
(4)清理缓存clean
makecache:生成元数据缓存
yum clean packages 清理/var/cacheyum下的软件包
yum clean headers 清理/var/cacheyum下的软件包包头(包头含有依赖信息)
yum clean metadata 清理yum用于确定软件包可用的元数据,使用这个选项会强制让yum下次运行的重新下载元数据
yum clean dbcache 清理sqlite缓存,这个缓存用来以很快的速递访问元数据,yum下次运行时会重新创建
yum clean all 清理以上所有
(5)group包组管理
groupinfo 显示关于包组的细节
groupinstall 从组里安装软件包到你的系统
grouplist 列出当前的组包信息
groupremove 移除系统中一个组中的包
[root@lamp sbin]# yum grouplist Loaded plugins: fastestmirror, refresh-packagekit Setting up Group Process Loading mirror speeds from cached hostfile * base: mirrors.btte.net * extras: mirrors.btte.net * updates: mirrors.btte.net Installed Groups: Additional Development Compatibility libraries Desktop Desktop Debugging and Performance Tools Dial-up Networking Support Directory Client E-mail server FCoE Storage Client Fonts General Purpose Desktop Graphical Administration Tools KDE Desktop Legacy UNIX compatibility Legacy X Window System compatibility
(6)yum仓库管理
repolist {all | enable| disable },默认为enable
[root@lamp sbin]# yum repolist Loaded plugins: fastestmirror, refresh-packagekit Loading mirror speeds from cached hostfile * base: mirrors.btte.net * extras: mirrors.btte.net * updates: mirrors.btte.net repo id repo name status base CentOS-6 - Base 6,575 extras CentOS-6 - Extras 35 updates CentOS-6 - Updates 298 repolist: 6,908
四、实例演示使用光盘当本地仓库
步骤:(1)挂载光盘到本地目录
(2)定义仓库,指定更新源文件路径(file:///)
[root@cacti ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 39G 5.6G 31G 16% / tmpfs 499M 80K 499M 1% /dev/shm /dev/sda1 194M 29M 156M 16% /boot /dev/sda3 6.8G 3.9G 2.6G 61% /usr /dev/sr0 4.1G 4.1G 0 100% /media/CentOS_6.4_Final /dev/sr0 4.1G 4.1G 0 100% /mnt/cdrom
[root@cacti ~]# vim /etc/yum.repos.d/CentOS-Media.repo # CentOS-Media.repo [c6-media] name=CentOS-$releasever - Media baseurl=file:///mnt/cdrom/ file:///media/CentOS/ file:///media/cdrecorder/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
[root@cacti ~]# yum repolist Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.btte.net * c6-media: * extras: mirrors.btte.net * updates: mirrors.btte.net repo id repo name status base CentOS-6 - Base 6,575 c6-media CentOS-6 - Media 6,381 extras CentOS-6 - Extras 35 updates CentOS-6 - Updates 298 repolist: 13,289