27.第八章 软件包管理(一)

1.软件运行和编译

1.1 软件相关概念

1.1.1 ABI

ABI : Application Binary Interface

Windows与Linux不兼容

  • ELF(Executable and Linkable Format)
  • PE(Portable Executable)

库级别的虚拟化:

  • Linux: WINE
  • Windows: Cygwin

1.1.2 API

API即Application Programming Interface,API可以在各种不同的操作系统上实现给应用程序提供完全相同的接口,而它们本身在这些系统上的实现却可能迥异,主流的操作系统有两种,一种是Windows系统,另一种是Linux系统。由于操作系统的不同,API又分为Windows API和Linux API。在Windows平台开发出来的软件在Linux上无法运行,在Linux上开发的软件在Windows上又无法运行,这就导致了软件移植困难,POSIX 标准的出现就是为了解决这个问题

POSIX:Portable Operating System Interface 可移植操作系统接口,定义了操作系统应该为应用程序提供的接口标准,是IEEE为要在各种UNIX操作系统上运行的软件而定义的一系列API标准的总称。
Linux和windows都要实现基本的posix标准,程序就在源代码级别可移植了

1.1.3 开发语言

系统级开发

  • 汇编语言
  • C
  • C++

应用级开发

  • java
  • Python
  • go
  • php
  • perl
  • delphi
  • basic
  • ruby
  • bash

1.2 C 语言程序编译过程

C 程序源代码 --> 预处理 --> 编译 --> 汇编 --> 链接

C语言的程序编译主要经过四个过程:
27.第八章 软件包管理(一)

  • 预处理(Pre-Processing)

    1)将所有的#define删除,并且展开所有的宏定义

    2)处理所有的条件预编译指令,比如#if #ifdef #elif #else #endif等

    3)处理#include 预编译指令,将被包含的文件插入到该预编译指令的位置。

    4)删除所有注释 “//”和”/* */”.

    5)添加行号和文件标识,以便编译时产生调试用的行号及编译错误警告行号。

    6)保留所有的#pragma编译器指令,因为编译器需要使用它们

  • 编译 (Compiling)

    编译过程就是把预处理完的文件进行一系列的词法分析,语法分析,语义分析及优化后,最后生成相应的汇编代码

  • 汇编 (Assembling)
    汇编器是将汇编代码转变成机器可以执行的命令,每一个汇编语句几乎都对应一条机器指令。汇编相对于编译过程比较简单,根据汇编指令和机器指令的对照表一一翻译即可

  • 链接 (Linking)
    通过调用链接器ld来链接程序运行需要的一大堆目标文件,以及所依赖的其它库文件,最后生成可执行文件

范例:gcc 编译过程

#分步骤编译运行
gcc -E hello.c -o hello.i 对hello.c文件进行预处理,生成了hello.i 文件
gcc -S hello.i -o hello.s 对预处理文件进行编译,生成了汇编文件
gcc -c hello.s -o hello.o 对汇编文件进行编译,生成了目标文件
gcc hello.o -o hello 对目标文件进行链接,生成可执行文件

#一步实现编译过程
gcc hello.c -o hello 直接编译链接成可执行目标文件

范例:

[root@rocky8 ~]# cat hello.c 
#include <stdio.h>

int main(void)
{
    printf("Hello,world\n");
}

[root@rocky8 ~]# gcc
-bash: gcc: command not found
[root@rocky8 ~]# dnf -y install gcc

[root@rocky8 ~]# gcc hello.c -o hello #编译
[root@rocky8 ~]# ll hello
-rwxr-xr-x 1 root root 12848 Oct 13 13:55 hello
[root@rocky8 ~]# file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=06a605f75e95ed2eb0c8cc38730df3fdc4fff71b, not stripped
[root@rocky8 ~]# ./hello 
Hello,world

1.3 软件模块的静态和动态链接

链接主要作用是把各个模块之间相互引用的部分处理好,使得各个模块之间能够正确地衔接,分为静态和动态链接
27.第八章 软件包管理(一)

1.3.1 静态链接

  • 把程序对应的依赖库复制一份到包
  • 生成模块文件libxxx.a
  • 嵌入程序包
  • 升级难,需重新编译
  • 占用较多空间,迁移容易

1.3.2 动态链接

  • 只把依赖加做一个动态链接
  • 生成模块文件libxxx.so
  • 连接指向
  • 占用较少空间,升级方便

1.3.3 模块(库)文件

查看二进制程序所依赖的库文件

ldd /PATH/TO/BINARY_FILE

管理及查看本机装载的库文件

#加载配置文件中指定的库文件
ldconfig

#显示本机已经缓存的所有可用库文件名及文件路径映射关系
/sbin/ldconfig –p

配置文件:

/etc/ld.so.conf,
/etc/ld.so.conf.d/*.conf

缓存文件:

/etc/ld.so.cache

范例:

[root@rocky8 ~]# ldconfig
[root@rocky8 ~]# ldconfig -p #查看内存中加载的库
333 libs found in cache `/etc/ld.so.cache'
	p11-kit-trust.so (libc6,x86-64) => /lib64/p11-kit-trust.so
	libzstd.so.1 (libc6,x86-64) => /lib64/libzstd.so.1
	libz.so.1 (libc6,x86-64) => /lib64/libz.so.1
	libyaml-0.so.2 (libc6,x86-64) => /lib64/libyaml-0.so.2
	libxtables.so.12 (libc6,x86-64) => /lib64/libxtables.so.12
	libxslt.so.1 (libc6,x86-64) => /lib64/libxslt.so.1
	libxml2.so.2 (libc6,x86-64) => /lib64/libxml2.so.2
	libxmlsec1.so.1 (libc6,x86-64) => /lib64/libxmlsec1.so.1
	libxmlsec1-openssl.so.1 (libc6,x86-64) => /lib64/libxmlsec1-openssl.so.1
	libxmlsec1-openssl.so (libc6,x86-64) => /lib64/libxmlsec1-openssl.so
	libxkbcommon.so.0 (libc6,x86-64) => /lib64/libxkbcommon.so.0
	libvmtools.so.0 (libc6,x86-64) => /lib64/libvmtools.so.0
	libvgauth.so.0 (libc6,x86-64) => /lib64/libvgauth.so.0
	libverto.so.1 (libc6,x86-64) => /lib64/libverto.so.1
	libuuid.so.1 (libc6,x86-64) => /lib64/libuuid.so.1
	libutil.so.1 (libc6,x86-64, OS ABI: Linux 3.2.0) => /lib64/libutil.so.1
	libutil.so (libc6,x86-64, OS ABI: Linux 3.2.0) => /lib64/libutil.so
	libutempter.so.0 (libc6,x86-64) => /lib64/libutempter.so.0
	libuser.so.1 (libc6,x86-64) => /lib64/libuser.so.1
	libusb-1.0.so.0 (libc6,x86-64) => /lib64/libusb-1.0.so.0
	libunistring.so.2 (libc6,x86-64) => /lib64/libunistring.so.2
	libunbound.so.2 (libc6,x86-64) => /lib64/libunbound.so.2
	libulockmgr.so.1 (libc6,x86-64) => /lib64/libulockmgr.so.1
	libudev.so.1 (libc6,x86-64) => /lib64/libudev.so.1
	libtss2-tctildr.so.0 (libc6,x86-64) => /lib64/libtss2-tctildr.so.0
	libtss2-tcti-mssim.so.0 (libc6,x86-64) => /lib64/libtss2-tcti-mssim.so.0
	libtss2-tcti-device.so.0 (libc6,x86-64) => /lib64/libtss2-tcti-device.so.0
	libtss2-sys.so.0 (libc6,x86-64) => /lib64/libtss2-sys.so.0
	libtss2-rc.so.0 (libc6,x86-64) => /lib64/libtss2-rc.so.0
	libtss2-mu.so.0 (libc6,x86-64) => /lib64/libtss2-mu.so.0
	libtss2-esys.so.0 (libc6,x86-64) => /lib64/libtss2-esys.so.0
	libtspi.so.1 (libc6,x86-64) => /lib64/libtspi.so.1
	libtirpc.so.3 (libc6,x86-64) => /lib64/libtirpc.so.3
	libtinfo.so.6 (libc6,x86-64) => /lib64/libtinfo.so.6
	libtinfo.so.5 (libc6,x86-64) => /lib64/libtinfo.so.5
	libtic.so.6 (libc6,x86-64) => /lib64/libtic.so.6
	libtic.so.5 (libc6,x86-64) => /lib64/libtic.so.5
	libthread_db.so.1 (libc6,x86-64, OS ABI: Linux 3.2.0) => /lib64/libthread_db.so.1
	libthread_db.so (libc6,x86-64, OS ABI: Linux 3.2.0) => /lib64/libthread_db.so
	libtevent.so.0 (libc6,x86-64) => /lib64/libtevent.so.0
	libteamdctl.so.0 (libc6,x86-64) => /lib64/libteamdctl.so.0
	libteam.so.5 (libc6,x86-64) => /lib64/libteam.so.5
	libtdb.so.1 (libc6,x86-64) => /lib64/libtdb.so.1
	libtasn1.so.6 (libc6,x86-64) => /lib64/libtasn1.so.6
	libtalloc.so.2 (libc6,x86-64) => /lib64/libtalloc.so.2
	libsystemd.so.0 (libc6,x86-64) => /lib64/libsystemd.so.0
	libsysfs.so.2 (libc6,x86-64) => /lib64/libsysfs.so.2
	libstdc++.so.6 (libc6,x86-64) => /lib64/libstdc++.so.6
	libsss_sudo.so (libc6,x86-64) => /lib64/libsss_sudo.so
	libsss_nss_idmap.so.0 (libc6,x86-64) => /lib64/libsss_nss_idmap.so.0
	libsss_idmap.so.0 (libc6,x86-64) => /lib64/libsss_idmap.so.0
	libsss_certmap.so.0 (libc6,x86-64) => /lib64/libsss_certmap.so.0
	libssl3.so (libc6,x86-64) => /lib64/libssl3.so
	libssl.so.1.1 (libc6,x86-64) => /lib64/libssl.so.1.1
	libssh.so.4 (libc6,x86-64) => /lib64/libssh.so.4
	libss.so.2 (libc6,x86-64) => /lib64/libss.so.2
	libsqlite3.so.0 (libc6,x86-64) => /lib64/libsqlite3.so.0
	libsolvext.so.1 (libc6,x86-64) => /lib64/libsolvext.so.1
	libsolv.so.1 (libc6,x86-64) => /lib64/libsolv.so.1
	libsoftokn3.so (libc6,x86-64) => /lib64/libsoftokn3.so
	libsnappy.so.1 (libc6,x86-64) => /lib64/libsnappy.so.1
	libsmime3.so (libc6,x86-64) => /lib64/libsmime3.so
	libsmartcols.so.1 (libc6,x86-64) => /lib64/libsmartcols.so.1
	libslapi-2.4.so.2 (libc6,x86-64) => /lib64/libslapi-2.4.so.2
	libslang.so.2 (libc6,x86-64) => /lib64/libslang.so.2
	libsigsegv.so.2 (libc6,x86-64) => /lib64/libsigsegv.so.2
	libsgutils2.so.2 (libc6,x86-64) => /lib64/libsgutils2.so.2
	libsepol.so.1 (libc6,x86-64) => /lib64/libsepol.so.1
	libsemanage.so.1 (libc6,x86-64) => /lib64/libsemanage.so.1
	libselinux.so.1 (libc6,x86-64) => /lib64/libselinux.so.1
	libsecret-1.so.0 (libc6,x86-64) => /lib64/libsecret-1.so.0
	libseccomp.so.2 (libc6,x86-64) => /lib64/libseccomp.so.2
	libsasl2.so.3 (libc6,x86-64) => /lib64/libsasl2.so.3
	librt.so.1 (libc6,x86-64, OS ABI: Linux 3.2.0) => /lib64/librt.so.1
	librt.so (libc6,x86-64, OS ABI: Linux 3.2.0) => /lib64/librt.so
...

范例:库文件破坏后,将导致依赖的程序无法正常运行

[root@rocky8 ~]# ldd hello
	linux-vdso.so.1 (0x00007ffd533ae000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f808d8f8000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f808dcbd000)
#查看软件依赖库

[root@rocky8 ~]# ls /lib64/libc.so.6
/lib64/libc.so.6
[root@rocky8 ~]# ll /bin/ls
-rwxr-xr-x. 1 root root 143408 Mar 15  2021 /bin/ls
[root@rocky8 ~]# ldd /bin/ls
	linux-vdso.so.1 (0x00007ffd0658b000)
	libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fb89285c000)
	libcap.so.2 => /lib64/libcap.so.2 (0x00007fb892656000)
	libc.so.6 => /lib64/libc.so.6 (0x00007fb892291000)
	libpcre2-8.so.0 => /lib64/libpcre2-8.so.0 (0x00007fb89200d000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007fb891e09000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fb892ca9000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fb891be9000)

[root@rocky8 ~]# mv /lib64/libc.so.6 /data
[root@rocky8 ~]# ls
ls: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory
[root@rocky8 ~]# ./hello 
./hello: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory

[root@rocky8 ~]# mv /data/libc.so.6 /lib64/
mv: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory

进入光盘救援模式

27.第八章 软件包管理(一)
按ESC键 ,选择CD-ROM Drive

27.第八章 软件包管理(一)
进入故障排除

27.第八章 软件包管理(一)
救援模式

27.第八章 软件包管理(一)
输入1

27.第八章 软件包管理(一)
提示系统被挂载到/mnt/sysimage, 回车

27.第八章 软件包管理(一)
系统根 在/mnt/sysimage

27.第八章 软件包管理(一)
mv /mnt/sysimage/data/libc.s0.6 /mnt/sysimage/lib64

把文件移动过去 ,exit重启

[root@rocky8 ~]# ls hello
hello

[root@rocky8 ~]# ll /lib64/libc.so.6
lrwxrwxrwx. 1 root root 12 May 19 04:41 /lib64/libc.so.6 -> libc-2.28.so
#这个文件就是软链接,删除了重新创建软链接就可以,恢复

2.软件包和包管理器

2.1 软件包介绍

开源软件最初只提供了.tar.gz的打包的源码文件,用户必须自已编译每个想在GNU/Linux上运行的软件。用户急需系统能提供一种更加便利的方法来管理这些软件,当Debian诞生时,这样一个管理工具dpkg也就应运而生,可用来管理deb后缀的"包"文件。从而著名的“package”概念第一次出现在GNU/Linux系统中,稍后Red Hat才开发自己的rpm包管理系统

2.1.1 软件包中的文件分类

  • 二进制文件
  • 库文件
  • 配置文件
  • 帮助文件

范例:利用 cpio工具查看包文件列表

rpm2cpio 包文件|cpio –itv 预览包内文件
rpm2cpio 包文件|cpio –id “*.conf” 释放包内文件

范例:

[root@rocky8 ~]# ls /misc
ls: cannot access '/misc': No such file or directory
[root@rocky8 ~]# ls /misc/cd
ls: cannot access '/misc/cd': No such file or directory
[root@rocky8 ~]# rpm -qa autofs
[root@rocky8 ~]# rpm -ql autofs
package autofs is not installed
[root@rocky8 ~]# dnf -y install autofs

[root@rocky8 ~]# ls /misc/cd
ls: cannot access '/misc/cd': No such file or directory
[root@rocky8 ~]# systemctl enable --now autofs
Created symlink /etc/systemd/system/multi-user.target.wants/autofs.service → /usr/lib/systemd/system/autofs.service.
[root@rocky8 ~]# ls /misc/cd
AppStream  BaseOS  EFI  images  isolinux  LICENSE  media.repo  TRANS.TBL
[root@rocky8 ~]# cd /misc/cd
[root@rocky8 cd]# ls
AppStream  BaseOS  EFI  images  isolinux  LICENSE  media.repo  TRANS.TBL
[root@rocky8 cd]# cd BaseOS/
[root@rocky8 BaseOS]# ls
Packages  repodata
[root@rocky8 BaseOS]# cd Packages/
[root@rocky8 Packages]# ls
aajohan-comfortaa-fonts-3.001-2.el8.noarch.rpm                          libnetfilter_conntrack-1.0.6-5.el8.x86_64.rpm
accel-config-2.8-1.el8.i686.rpm                                         libnetfilter_cthelper-1.0.0-15.el8.i686.rpm
accel-config-2.8-1.el8.x86_64.rpm                                       libnetfilter_cthelper-1.0.0-15.el8.x86_64.rpm
accel-config-libs-2.8-1.el8.i686.rpm                                    libnetfilter_cttimeout-1.0.0-11.el8.i686.rpm
accel-config-libs-2.8-1.el8.x86_64.rpm                                  libnetfilter_cttimeout-1.0.0-11.el8.x86_64.rpm
acl-2.2.53-1.el8.1.x86_64.rpm                                           libnetfilter_queue-1.0.4-3.el8.i686.rpm
acpica-tools-20180629-3.el8.x86_64.rpm                                  libnetfilter_queue-1.0.4-3.el8.x86_64.rpm
adcli-0.8.2-9.el8.x86_64.rpm                                            libnfnetlink-1.0.1-13.el8.i686.rpm
adcli-doc-0.8.2-9.el8.noarch.rpm                                        libnfnetlink-1.0.1-13.el8.x86_64.rpm
alsa-sof-firmware-1.6.1-2.el8.noarch.rpm                                libnfsidmap-2.3.3-41.el8.i686.rpm
alsa-sof-firmware-debug-1.6.1-2.el8.noarch.rpm                          libnfsidmap-2.3.3-41.el8.x86_64.rpm
...
#这里放了大量的rpm包

root@ubuntu1804:~# ls /misc
ls: cannot access '/misc': No such file or directory
root@ubuntu1804:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0  953M  0 part /boot
├─sda2   8:2    0 93.1G  0 part /
├─sda3   8:3    0 46.6G  0 part /data
└─sda4   8:4    0  3.7G  0 part [SWAP]
sr0     11:0    1  964M  0 rom  
root@ubuntu1804:~# mount /dev/sr0 /mnt
mount: /mnt: WARNING: device write-protected, mounted read-only.
root@ubuntu1804:~# cd /mnt/
root@ubuntu1804:/mnt# ls
boot  dists  doc  EFI  install  isolinux  md5sum.txt  pool  preseed  ubuntu
root@ubuntu1804:/mnt# cd pool/
root@ubuntu1804:/mnt/pool# ls
main
root@ubuntu1804:/mnt/pool# cd main/
root@ubuntu1804:/mnt/pool/main# ls
a  c  e  g  i  k  liba  libc  libe  libg  libi  libk  libm  libo  libq  libs  libu  libw  liby  m  o  q  s  u  w  y
b  d  f  h  j  l  libb  libd  libf  libh  libj  libl  libn  libp  libr  libt  libv  libx  libz  n  p  r  t  v  x  z
root@ubuntu1804:/mnt/pool/main# cd a
root@ubuntu1804:/mnt/pool/main/a# ls
aalib            acl      aide       alsa-utils       angular.js  apport           apr-util   aptitude   asn1crypto  at     augeas    automat
accountsservice  acpid    alabaster  amavisd-new      apache2     apport-symptoms  apt        apt-setup  aspell      attr   authbind  avahi
acct             adduser  alsa-lib   amd64-microcode  apparmor    apr              apt-clone  argon2     aspell-en   audit  autofs
root@ubuntu1804:/mnt/pool/main/a# cd aide/
root@ubuntu1804:/mnt/pool/main/a/aide# ls
aide_0.16-3_amd64.deb  aide-common_0.16-3_all.deb

[root@rocky8 Packages]# cp xz-5.2.4-3.el8.1.x86_64.rpm /data
[root@rocky8 Packages]# cd /data
[root@rocky8 data]# ls
xz-5.2.4-3.el8.1.x86_64.rpm
[root@rocky8 data]# rpm2cpio xz-5.2.4-3.el8.1.x86_64.rpm | cpio -tv #查看包里边文件
-rw-r--r--   1 root     root          162 Jun 14 22:48 ./etc/profile.d/colorxzgrep.csh
-rw-r--r--   1 root     root          183 Jun 14 22:48 ./etc/profile.d/colorxzgrep.sh
lrwxrwxrwx   1 root     root            2 Jun 14 22:50 ./usr/bin/unxz -> xz
-rwxr-xr-x   1 root     root        84128 Jun 14 22:50 ./usr/bin/xz
lrwxrwxrwx   1 root     root            2 Jun 14 22:50 ./usr/bin/xzcat -> xz
lrwxrwxrwx   1 root     root            6 Jun 14 22:50 ./usr/bin/xzcmp -> xzdiff
-rwxr-xr-x   1 root     root        16864 Jun 14 22:50 ./usr/bin/xzdec
-rwxr-xr-x   1 root     root         6632 Jun 14 22:50 ./usr/bin/xzdiff
lrwxrwxrwx   1 root     root            6 Jun 14 22:50 ./usr/bin/xzegrep -> xzgrep
lrwxrwxrwx   1 root     root            6 Jun 14 22:50 ./usr/bin/xzfgrep -> xzgrep
-rwxr-xr-x   1 root     root         5628 Jun 14 22:50 ./usr/bin/xzgrep
-rwxr-xr-x   1 root     root         1802 Jun 14 22:50 ./usr/bin/xzless
-rwxr-xr-x   1 root     root         2161 Jun 14 22:50 ./usr/bin/xzmore
drwxr-xr-x   1 root     root            0 Jun 14 22:50 ./usr/lib/.build-id
drwxr-xr-x   1 root     root            0 Jun 14 22:50 ./usr/lib/.build-id/43
lrwxrwxrwx   1 root     root           22 Jun 14 22:50 ./usr/lib/.build-id/43/059ad54ba42a7e5b29db2741a82b96cd80db82 -> ../../../../usr/bin/xz
drwxr-xr-x   1 root     root            0 Jun 14 22:50 ./usr/lib/.build-id/cd
lrwxrwxrwx   1 root     root           25 Jun 14 22:50 ./usr/lib/.build-id/cd/a20bf178f12c3ab7bb39d0291d38e2ec9d7d6a -> ../../../../usr/bin/xzdec
drwxr-xr-x   1 root     root            0 Jun 14 22:50 ./usr/share/doc/xz
-rw-r--r--   1 root     root         1043 Apr 29  2018 ./usr/share/doc/xz/AUTHORS
-rw-r--r--   1 root     root         2775 Apr 29  2018 ./usr/share/doc/xz/COPYING
-rw-r--r--   1 root     root        18092 Apr 29  2018 ./usr/share/doc/xz/COPYING.GPLv2
-rw-r--r--   1 root     root        21413 Apr 29  2018 ./usr/share/doc/xz/NEWS
-rw-r--r--   1 root     root        13526 Apr 29  2018 ./usr/share/doc/xz/README
-rw-r--r--   1 root     root         2487 Apr 29  2018 ./usr/share/doc/xz/THANKS
-rw-r--r--   1 root     root         4040 Apr 29  2018 ./usr/share/doc/xz/TODO
-rw-r--r--   1 root     root         9411 Apr 29  2018 ./usr/share/doc/xz/faq.txt
-rw-r--r--   1 root     root         7427 Apr 29  2018 ./usr/share/doc/xz/history.txt
-rw-r--r--   1 root     root         5689 Apr 29  2018 ./usr/share/doc/xz/lzma-file-format.txt
-rw-r--r--   1 root     root        43305 Apr 29  2018 ./usr/share/doc/xz/xz-file-format.txt
-rw-r--r--   1 root     root        21945 Apr 29  2018 ./usr/share/locale/cs/LC_MESSAGES/xz.mo
-rw-r--r--   1 root     root        26890 Apr 29  2018 ./usr/share/locale/de/LC_MESSAGES/xz.mo
-rw-r--r--   1 root     root        24677 Apr 29  2018 ./usr/share/locale/fr/LC_MESSAGES/xz.mo
-rw-r--r--   1 root     root        26091 Apr 29  2018 ./usr/share/locale/it/LC_MESSAGES/xz.mo
-rw-r--r--   1 root     root        25679 Apr 29  2018 ./usr/share/locale/pl/LC_MESSAGES/xz.mo
-rw-r--r--   1 root     root        27593 Apr 29  2018 ./usr/share/locale/vi/LC_MESSAGES/xz.mo
lrwxrwxrwx   1 root     root            7 Jun 14 22:50 ./usr/share/man/man1/unxz.1.gz -> xz.1.gz
-rw-r--r--   1 root     root        20347 Apr 29  2018 ./usr/share/man/man1/xz.1.gz
lrwxrwxrwx   1 root     root            7 Jun 14 22:50 ./usr/share/man/man1/xzcat.1.gz -> xz.1.gz
lrwxrwxrwx   1 root     root           11 Jun 14 22:50 ./usr/share/man/man1/xzcmp.1.gz -> xzdiff.1.gz
-rw-r--r--   1 root     root         1283 Apr 29  2018 ./usr/share/man/man1/xzdec.1.gz
-rw-r--r--   1 root     root          664 Apr 29  2018 ./usr/share/man/man1/xzdiff.1.gz
lrwxrwxrwx   1 root     root           11 Jun 14 22:50 ./usr/share/man/man1/xzegrep.1.gz -> xzgrep.1.gz
lrwxrwxrwx   1 root     root           11 Jun 14 22:50 ./usr/share/man/man1/xzfgrep.1.gz -> xzgrep.1.gz
-rw-r--r--   1 root     root          700 Apr 29  2018 ./usr/share/man/man1/xzgrep.1.gz
-rw-r--r--   1 root     root          743 Apr 29  2018 ./usr/share/man/man1/xzless.1.gz
-rw-r--r--   1 root     root          636 Apr 29  2018 ./usr/share/man/man1/xzmore.1.gz
842 blocks

[root@rocky8 data]# rpm2cpio xz-5.2.4-3.el8.1.x86_64.rpm | cpio -idv ./usr/bin/xz #解包
./usr/bin/xz
842 blocks
[root@rocky8 data]# ls
usr  xz-5.2.4-3.el8.1.x86_64.rpm
[root@rocky8 data]# cd usr/
[root@rocky8 usr]# ls
bin
[root@rocky8 usr]# cd bin/
[root@rocky8 bin]# ls
xz
[root@rocky8 bin]#  ll /bin/xz ./xz
-rwxr-xr-x. 1 root root 84128 Jun 14 22:50 /bin/xz
-rwxr-xr-x  1 root root 84128 Oct 13 18:42 ./xz

2.1.2 程序包管理器

软件包管理器功能:

将编译好的应用程序的各组成文件打包一个或几个程序包文件,利用包管理器可以方便快捷地实现程序包的安装、卸载、查询、升级和校验等管理操作

主流的程序包管理器

  • redhat:rpm文件, rpm 包管理器,rpm:Redhat Package Manager,RPM Package Manager
  • debian:deb文件, dpkg 包管理器

2.1.3 包命名

源代码打包文件命名:

name-VERSION.tar.gz|bz2|xz
VERSION: major.minor.release

范例:Linux 最新内核的C源码的行数

[root@rocky8 ~]# tar xf linux-5.14.12.tar.xz 
[root@rocky8 ~]# cd linux-5.14.12
[root@rocky8 linux-5.14.12]# for file in `find -name "*.c"`;do line=`cat $file|wc -l`;let sum+=line;done;echo $sum
20930776
[root@rocky8 linux-5.14.12]# cat `find -name "*.c"` |wc -l
20930776
[root@rocky8 linux-5.14.12]# find -name "*.c" | xargs cat |wc -l
20930776

rpm包命名方式:

name-VERSION-release.arch.rpm
VERSION: major.minor.release
release:release.OS

常见的arch:

  • x86: i386, i486, i586, i686
  • x86_64: x64, x86_64, amd64
  • powerpc: ppc
  • 跟平台无关:noarch

范例:

bash-3.2-32.el5_9.1.i386.rpm
bash-4.2.46-19.el7.x86_64.rpm
bash-4.4.19-7.el8.x86_64.rpm
bash-4.4.19-7.el8.aarch64.rpm
bash-4.4.19-7.el8.ppc64le.rpm
bc_1.07.1-2_amd64.deb
bc_1.07.1-2_s390x.deb

范例:统计rpm的架构类型及相应的包数量

[root@rocky8 bin]# cd /misc/cd/
[root@rocky8 cd]# ls
AppStream  BaseOS  EFI  images  isolinux  LICENSE  media.repo  TRANS.TBL
[root@rocky8 cd]# cd BaseOS/
[root@rocky8 BaseOS]# ls
Packages  repodata
[root@rocky8 BaseOS]# cd Packages/

[root@rocky8 Packages]# ls *.rpm |sed -rn 's/(.*)\.([^.]+)\.rpm$/\2/p' |sort |uniq -c
    401 i686
    226 noarch
   1082 x86_64

[root@rocky8 Packages]#  ls *.rpm |sed -rn 's/.*\.([^.]+)\.rpm$/\1/p' |sort |uniq -c
    401 i686
    226 noarch
   1082 x86_64

[root@rocky8 Packages]# ls *.rpm |rev |cut -d. -f2|rev|sort |uniq -c
    401 i686
    226 noarch
   1082 x86_64

2.1.4 分类和拆包

软件包为了管理和使用的便利,会将一个大的软件分类,放在不同的子包中。

包的分类

  • Application-VERSION-ARCH.rpm: 主包
  • Application-devel-VERSION-ARCH.rpm 开发子包
  • Application-utils-VERSION-ARHC.rpm 其它子包
  • Application-libs-VERSION-ARHC.rpm 其它子包

范例:

[root@rocky8 ~]# ls /misc/cd/AppStream/Packages/nginx-*
/misc/cd/AppStream/Packages/nginx-1.14.1-9.module+el8.4.0+542+81547229.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-1.16.1-2.module+el8.4.0+543+efbf198b.1.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-1.18.0-3.module+el8.4.0+544+e40554cf.1.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-all-modules-1.14.1-9.module+el8.4.0+542+81547229.noarch.rpm
/misc/cd/AppStream/Packages/nginx-all-modules-1.16.1-2.module+el8.4.0+543+efbf198b.1.noarch.rpm
/misc/cd/AppStream/Packages/nginx-all-modules-1.18.0-3.module+el8.4.0+544+e40554cf.1.noarch.rpm
/misc/cd/AppStream/Packages/nginx-filesystem-1.14.1-9.module+el8.4.0+542+81547229.noarch.rpm
/misc/cd/AppStream/Packages/nginx-filesystem-1.16.1-2.module+el8.4.0+543+efbf198b.1.noarch.rpm
/misc/cd/AppStream/Packages/nginx-filesystem-1.18.0-3.module+el8.4.0+544+e40554cf.1.noarch.rpm
/misc/cd/AppStream/Packages/nginx-mod-http-image-filter-1.14.1-9.module+el8.4.0+542+81547229.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-http-image-filter-1.16.1-2.module+el8.4.0+543+efbf198b.1.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-http-image-filter-1.18.0-3.module+el8.4.0+544+e40554cf.1.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-http-perl-1.14.1-9.module+el8.4.0+542+81547229.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-http-perl-1.16.1-2.module+el8.4.0+543+efbf198b.1.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-http-perl-1.18.0-3.module+el8.4.0+544+e40554cf.1.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-http-xslt-filter-1.14.1-9.module+el8.4.0+542+81547229.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-http-xslt-filter-1.16.1-2.module+el8.4.0+543+efbf198b.1.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-http-xslt-filter-1.18.0-3.module+el8.4.0+544+e40554cf.1.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-mail-1.14.1-9.module+el8.4.0+542+81547229.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-mail-1.16.1-2.module+el8.4.0+543+efbf198b.1.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-mail-1.18.0-3.module+el8.4.0+544+e40554cf.1.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-stream-1.14.1-9.module+el8.4.0+542+81547229.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-stream-1.16.1-2.module+el8.4.0+543+efbf198b.1.x86_64.rpm
/misc/cd/AppStream/Packages/nginx-mod-stream-1.18.0-3.module+el8.4.0+544+e40554cf.1.x86_64.rpm

2.1.5 包的依赖

软件包之间可能存在依赖关系,甚至循环依赖,即:A包依赖B包,B包依赖C包,C包依赖A包

安装软件包时,会因为缺少依赖的包,而导致安装包失败。

解决依赖包管理工具:

  • yum:rpm包管理器的前端工具
  • dnf:Fedora 18+ rpm包管理器前端管理工具,CentOS 8 版代替 yum
  • apt:deb包管理器前端工具
  • zypper:suse上的rpm前端管理工具

2.1.6 程序包管理器相关文件

1.包文件组成 (每个包独有)

  • 包内的文件
  • 元数据,如:包的名称,版本,依赖性,描述等
  • 可能会有包安装或卸载时运行的脚本

2.数据库(公共):/var/lib/rpm

  • 程序包名称及版本
  • 依赖关系
  • 功能说明
  • 包安装后生成的各文件路径及校验码信息

范例:

[root@rocky8 ~]# ll /var/lib/rpm
total 72948
-rw-r--r--. 1 root root  1822720 Oct 13 18:33 Basenames
-rw-r--r--. 1 root root     8192 Oct 13 18:33 Conflictname
-rw-r--r--  1 root root   270336 Oct 13 18:33 __db.001
-rw-r--r--  1 root root    81920 Oct 13 18:33 __db.002
-rw-r--r--  1 root root  1318912 Oct 13 18:33 __db.003
-rw-r--r--. 1 root root   622592 Oct 13 18:33 Dirnames
-rw-r--r--. 1 root root     8192 Oct  6 19:16 Enhancename
-rw-r--r--. 1 root root     8192 Oct  6 19:17 Filetriggername
-rw-r--r--. 1 root root    12288 Oct 13 18:33 Group
-rw-r--r--. 1 root root    12288 Oct 13 18:33 Installtid
-rw-r--r--. 1 root root    24576 Oct 13 18:33 Name
-rw-r--r--. 1 root root    16384 Oct 13 13:54 Obsoletename
-rw-r--r--. 1 root root 67461120 Oct 13 18:33 Packages
-rw-r--r--. 1 root root  2895872 Oct 13 18:33 Providename
-rw-r--r--. 1 root root     8192 Oct 11 19:56 Recommendname
-rw-r--r--. 1 root root   155648 Oct 13 18:33 Requirename
-rw-r--r--. 1 root root    45056 Oct 13 18:33 Sha1header
-rw-r--r--. 1 root root    32768 Oct 13 18:33 Sigmd5
-rw-r--r--. 1 root root     8192 Oct 11 21:01 Suggestname
-rw-r--r--. 1 root root     8192 Oct  6 19:16 Supplementname
-rw-r--r--. 1 root root     8192 Oct 11 21:01 Transfiletriggername
-rw-r--r--. 1 root root     8192 Oct 13 18:33 Triggername
[root@rocky8 ~]# rpm -qa #查看系统上安装的包
iptables-1.8.4-17.el8.x86_64
python3-setuptools-wheel-39.2.0-6.el8.noarch
rpm-plugin-selinux-4.14.3-13.el8.x86_64
rocky-release-8.4-26.el8.noarch
python3-configobj-5.0.6-11.el8.noarch
basesystem-11-5.el8.noarch
authselect-libs-1.2.2-2.el8.x86_64
libselinux-2.9-5.el8.x86_64
dbus-glib-0.110-2.el8.x86_64
glibc-2.28-151.el8.x86_64
libsecret-0.18.6-1.el8.x86_64
xz-libs-5.2.4-3.el8.1.x86_64
dracut-squash-049-135.git20210121.el8.x86_64
libgpg-error-1.31-1.el8.x86_64
...

2.1.7 获取程序包的途径

软件包需要事先将源码进行编译后打包形成,获取包的途径如下:

2.1.7.1 系统发版的光盘或官方网站

CentOS镜像:

https://www.centos.org/download/

http://mirrors.aliyun.com

https://mirrors.huaweicloud.com/

https://mirror.tuna.tsinghua.edu.cn/

http://mirrors.sohu.com

http://mirrors.163.com

Ubuntu 镜像:

http://cdimage.ubuntu.com/releases/

http://releases.ubuntu.com

nginx包:

http://nginx.org/packages/centos/8/x86_64/RPMS/

2.1.7.2 第三方组织提供

  • Fedora-EPEL:Extra Packages for Enterprise Linux

    https://fedoraproject.org/wiki/EPEL

    https://mirrors.aliyun.com/epel/

  • Rpmforge:官网:http://repoforge.org/, RHEL推荐,包很全,即将关闭

  • Community Enterprise Linux Repository:http://www.elrepo.org,支持最新的内核和硬件相关包

2.1.7.3 软件项目官方站点

http://yum.mariadb.org/10.4/centos8-amd64/rpms/

http://repo.mysql.com/yum/mysql-8.0-community/el/8/x86_64/

2.1.7.4 搜索引擎

http://pkgs.org

http://rpmfind.net

http://rpm.pbone.net

https://sourceforge.net/

注意:第三方包建议要检查其合法性,来源合法性,程序包的完整性

2.1.7.5 自己制作

将源码文件,利用工具,如:rpmbuild,fpm等工具制作成rpm包文件

3.包管理器 rpm

CentOS系统上使用rpm命令管理程序包

功能:

安装、卸载、升级、查询、校验、数据库维护

3.1 安装

格式:

rpm {-i|--install} [install-options] PACKAGE_FILE…

选项:

-v: verbose
-vv:
-h: 以#显示程序包管理执行进度

常用组合:

rpm -ivh PACKAGE_FILE ...

rpm包安装[install-options]

--test: 测试安装,但不真正执行安装,即dry run模式
--nodeps:忽略依赖关系
--replacepkgs | replacefiles
--nosignature: 不检查来源合法性
--nodigest:不检查包完整性
--noscripts:不执行程序包脚本
    %pre: 安装前脚本 --nopre
    %post: 安装后脚本 --nopost
    %preun: 卸载前脚本 --nopreun
    %postun: 卸载后脚本 --nopostun

范例:

[root@rocky8 ~]# rpm -ivh /misc/cd/BaseOS/Packages/dos2unix-7.4.0-3.el8.x86_64.rpm
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:dos2unix-7.4.0-3.el8             ################################# [100%]

3.2 升级和降级

rpm包升级

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

对应选项:

upgrade:安装有旧版程序包,则“升级”,如果不存在旧版程序包,则“安装”
freshen:安装有旧版程序包,则“升级”, 如果不存在旧版程序包,则不执行升级操作

--oldpackage:降级
--force: 强制安装

常用组合

rpm -Uvh PACKAGE_FILE ...
rpm -Fvh PACKAGE_FILE ...

升级注意项:

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

(2) 如果原程序包的配置文件安装后曾被修改,升级时,新版本提供的同一个配置文件不会直接覆盖老版本的配置文件,而把新版本文件重命名(FILENAME.rpmnew)后保留

3.3 包查询

rpm {-q|--query} [select-options] [query-options]
[select-options]
-a:所有包
-f:查看指定的文件由哪个程序包安装生成
-p rpmfile:针对尚未安装的程序包文件做查询操作

[query-options]
--changelog:查询rpm包的changelog
-c:查询程序的配置文件
-d:查询程序的文档
-i:information
-l:查看指定的程序包安装后生成的所有文件
--scripts:程序包自带的脚本

#和CAPABILITY相关
--whatprovides CAPABILITY:查询指定的CAPABILITY由哪个包所提供
--whatrequires CAPABILITY:查询指定的CAPABILITY被哪个包所依赖
--provides:列出指定程序包所提供的CAPABILITY
-R:查询指定的程序包所依赖的CAPABILITY

常用查询用法:

-qa
-q PACKAGE
-qi PACKAGE
-qc PACKAGE
-ql PACKAGE
-qd PACKAGE
-q --scripts PACKAGE
-qf FILE
-qpi PACKAGE_FILE
-qpl PACKAGE_FILE, ...

范例:

[root@rocky8 ~]# rpm -q dos2unix
dos2unix-7.4.0-3.el8.x86_64
#-q  查询包是否安装

[root@rocky8 ~]# rpm -qa |grep dos
dos2unix-7.4.0-3.el8.x86_64
[root@rocky8 ~]# rpm -qa "*dos*"
dos2unix-7.4.0-3.el8.x86_64
[root@rocky8 ~]# rpm -e dos2unix #-e 卸载
[root@rocky8 ~]# rpm -q dos2unix
package dos2unix is not installed

[root@rocky8 ~]# rpm -ivh https://mirrors.nju.edu.cn/rocky/8/BaseOS/x86_64/os/Packages/dos2unix-7.4.0-3.el8.x86_64.rpm
Retrieving https://mirrors.nju.edu.cn/rocky/8/BaseOS/x86_64/os/Packages/dos2unix-7.4.0-3.el8.x86_64.rpm
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:dos2unix-7.4.0-3.el8             ################################# [100%]
#网络也可以安装

[root@rocky8 ~]# rpm -ivh /misc/cd/AppStream/Packages/httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64.rpm 
error: Failed dependencies:
	/etc/mime.types is needed by httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64
	httpd-filesystem is needed by httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64
	httpd-filesystem = 2.4.37-39.module+el8.4.0+571+fd70afb1 is needed by httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64
	httpd-tools = 2.4.37-39.module+el8.4.0+571+fd70afb1 is needed by httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64
	libapr-1.so.0()(64bit) is needed by httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64
	libaprutil-1.so.0()(64bit) is needed by httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64
	mod_http2 is needed by httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64
	system-logos-httpd >= 82.0 is needed by httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64
#无法安装

[root@rocky8 ~]# rpm -qi httpd
package httpd is not installed
[root@rocky8 ~]# rpm -q tree 
package tree is not installed
[root@rocky8 ~]# dnf -y install tree
[root@rocky8 ~]# rpm -q tree 
tree-1.7.0-15.el8.x86_64
[root@rocky8 ~]# rpm -qi tree 
Name        : tree
Version     : 1.7.0
Release     : 15.el8
Architecture: x86_64
Install Date: Wed 13 Oct 2021 07:39:43 PM CST
Group       : Unspecified
Size        : 108563
License     : GPLv2+
Signature   : RSA/SHA256, Mon 12 Apr 2021 05:23:54 PM CST, Key ID 15af5dac6d745a60
Source RPM  : tree-1.7.0-15.el8.src.rpm
Build Date  : Mon 12 Apr 2021 04:38:17 PM CST
Build Host  : ord1-prod-x86build003.svc.aws.rockylinux.org
Relocations : (not relocatable)
Packager    : infrastructure@rockylinux.org
Vendor      : Rocky
URL         : http://mama.indstate.edu/users/ice/tree/
Summary     : File system tree viewer
Description :
The tree utility recursively displays the contents of directories in a
tree-like format.  Tree is basically a UNIX port of the DOS tree
utility.
#-qi  查看软件信息 

[root@rocky8 ~]# rpm -ql tree 
/usr/bin/tree
/usr/lib/.build-id
/usr/lib/.build-id/47
/usr/lib/.build-id/47/6e749c56abc75d25e3bc5c29e83507a605fd53
/usr/share/doc/tree
/usr/share/doc/tree/LICENSE
/usr/share/doc/tree/README
/usr/share/man/man1/tree.1.gz
#-ql  查看包里文件路径信息

[root@rocky8 ~]# rpm -qd tree 
/usr/share/doc/tree/LICENSE
/usr/share/doc/tree/README
/usr/share/man/man1/tree.1.gz
#-qd  查询文档

[root@rocky8 ~]# rpm -qf /etc/issue 
rocky-release-8.4-26.el8.noarch
#-qf  查询依赖软件包

[root@rocky8 ~]# which tree
/usr/bin/tree
[root@rocky8 ~]# rm -f /usr/bin/tree
[root@rocky8 ~]# rpm -qf /usr/bin/tree
tree-1.7.0-15.el8.x86_64
[root@rocky8 ~]# ll /usr/bin/tree
ls: cannot access '/usr/bin/tree': No such file or directory
#删除了还能查到

[root@rocky8 ~]# ll /var/lib/rpm
total 65308
-rw-r--r--. 1 root root  1634304 Oct 13 19:39 Basenames
-rw-r--r--. 1 root root     8192 Oct 13 19:38 Conflictname
-rw-r--r--  1 root root   270336 Oct 13 19:42 __db.001
-rw-r--r--  1 root root    81920 Oct 13 19:42 __db.002
-rw-r--r--  1 root root  1318912 Oct 13 19:42 __db.003
-rw-r--r--. 1 root root   593920 Oct 13 19:39 Dirnames
-rw-r--r--. 1 root root     8192 Oct  6 19:16 Enhancename
-rw-r--r--. 1 root root     8192 Oct  6 19:17 Filetriggername
-rw-r--r--. 1 root root    12288 Oct 13 19:39 Group
-rw-r--r--. 1 root root    12288 Oct 13 19:39 Installtid
-rw-r--r--. 1 root root    24576 Oct 13 19:39 Name
-rw-r--r--. 1 root root    16384 Oct  6 19:22 Obsoletename
-rw-r--r--. 1 root root 59822080 Oct 13 19:39 Packages
-rw-r--r--. 1 root root  2887680 Oct 13 19:39 Providename
-rw-r--r--. 1 root root     8192 Oct  6 19:17 Recommendname
-rw-r--r--. 1 root root   151552 Oct 13 19:39 Requirename
-rw-r--r--. 1 root root    40960 Oct 13 19:39 Sha1header
-rw-r--r--. 1 root root    24576 Oct 13 19:39 Sigmd5
-rw-r--r--. 1 root root     8192 Oct  6 19:17 Suggestname
-rw-r--r--. 1 root root     8192 Oct  6 19:16 Supplementname
-rw-r--r--. 1 root root     8192 Oct  6 19:17 Transfiletriggername
-rw-r--r--. 1 root root     8192 Oct 13 19:38 Triggername
#因为他在这个目录有数据库记录了安装信息

[root@rocky8 ~]# mv /var/lib/rpm /opt
[root@rocky8 ~]# rpm -qf /usr/bin/tree
error: file /usr/bin/tree: No such file or directory
#移走就不能查了

[root@rocky8 ~]# rpm -qa
[root@rocky8 ~]# rpm -ivh /misc/cd/BaseOS/Packages/dos2unix-7.4.0-3.el8.x86_64.rpm
warning: /misc/cd/BaseOS/Packages/dos2unix-7.4.0-3.el8.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 6d745a60: NOKEY
error: Failed dependencies:
	libc.so.6()(64bit) is needed by dos2unix-7.4.0-3.el8.x86_64
	libc.so.6(GLIBC_2.2.5)(64bit) is needed by dos2unix-7.4.0-3.el8.x86_64
	libc.so.6(GLIBC_2.3.4)(64bit) is needed by dos2unix-7.4.0-3.el8.x86_64
	libc.so.6(GLIBC_2.4)(64bit) is needed by dos2unix-7.4.0-3.el8.x86_64
	rtld(GNU_HASH) is needed by dos2unix-7.4.0-3.el8.x86_64
#不能查,也不能安装软件了

[root@rocky8 ~]# rpm -ivh /misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:tree-1.7.0-15.el8                ################################# [100%]
[root@rocky8 ~]# rpm -ivh /misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm --force
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:tree-1.7.0-15.el8                ################################# [100%]
#--force  强制安装

[root@rocky8 ~]#  rpm -q --scripts tree
[root@rocky8 ~]#  rpm -q --scripts bash
postinstall scriptlet (using <lua>):
nl        = '\n'
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.."\n")
  end
  f:close()
end
#rpm -q --scripts  查询包有没有脚本

[root@rocky8 ~]# rpm -qpl /misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm
/usr/bin/tree
/usr/lib/.build-id
/usr/lib/.build-id/47
/usr/lib/.build-id/47/6e749c56abc75d25e3bc5c29e83507a605fd53
/usr/share/doc/tree
/usr/share/doc/tree/LICENSE
/usr/share/doc/tree/README
/usr/share/man/man1/tree.1.gz
[root@rocky8 ~]# rpm -ql /misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm
/usr/bin/tree
/usr/lib/.build-id
/usr/lib/.build-id/47
/usr/lib/.build-id/47/6e749c56abc75d25e3bc5c29e83507a605fd53
/usr/share/doc/tree
/usr/share/doc/tree/LICENSE
/usr/share/doc/tree/README
/usr/share/man/man1/tree.1.gz
#centos 8 上直接 -ql 就可以查看,不用加p选项
#查询文件中包含的文件

[root@centos7 ~]# ls /misc/cd/Packages/tree-1.6.0-10.el7.x86_64.rpm
/misc/cd/Packages/tree-1.6.0-10.el7.x86_64.rpm
[root@centos7 ~]# rpm -ivh /misc/cd/Packages/tree-1.6.0-10.el7.x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:tree-1.6.0-10.el7                ################################# [100%]
[root@centos7 ~]# rpm -ql /misc/cd/Packages/tree-1.6.0-10.el7.x86_64.rpm
package /misc/cd/Packages/tree-1.6.0-10.el7.x86_64.rpm is not installed
[root@centos7 ~]# rpm -qpl /misc/cd/Packages/tree-1.6.0-10.el7.x86_64.rpm
/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
#centos 7上要加p选项才可以查,包文件包含的文件

3.4 包卸载

格式:

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

注意:当包卸载时,对应的配置文件不会删除, 以FILENAME.rpmsave形式保留

范例:强行删除rpm包,并恢复

[root@rocky8 ~]# rpm -e rpm --nodeps
[root@rocky8 ~]# rpm
-bash: /usr/bin/rpm: No such file or directory
[root@rocky8 ~]# dnf -y install rpm
error: Unable to open /usr/lib/rpm/rpmrc for reading: No such file or directory.
Traceback (most recent call last):
  File "/usr/bin/dnf", line 57, in <module>
    from dnf.cli import main
  File "/usr/lib/python3.6/site-packages/dnf/__init__.py", line 30, in <module>
    import dnf.base
  File "/usr/lib/python3.6/site-packages/dnf/base.py", line 32, in <module>
    from dnf.comps import CompsQuery
  File "/usr/lib/python3.6/site-packages/dnf/comps.py", line 27, in <module>
    from dnf.exceptions import CompsError
  File "/usr/lib/python3.6/site-packages/dnf/exceptions.py", line 22, in <module>
    import dnf.util
  File "/usr/lib/python3.6/site-packages/dnf/util.py", line 29, in <module>
    import dnf.callback
  File "/usr/lib/python3.6/site-packages/dnf/callback.py", line 22, in <module>
    import dnf.yum.rpmtrans
  File "/usr/lib/python3.6/site-packages/dnf/yum/rpmtrans.py", line 26, in <module>
    import rpm
  File "/usr/lib64/python3.6/site-packages/rpm/__init__.py", line 39, in <module>
    from rpm.transaction import *
  File "/usr/lib64/python3.6/site-packages/rpm/transaction.py", line 5, in <module>
    from rpm._rpm import ts as TransactionSetCore
ImportError: cannot import name 'ts'

#恢复过程
#重启进入rescue模式
#mkdir /mnt/cdrom
#mount /dev/sr0 /mnt/cdrom
#rpm -ivh /mnt/cdrom/BaseOS/Packages/rpm-4.14.3-13.el8.x86_64.rpm --root=/mnt/sysimage
#reboot

3.5 包校验

在安装包时,系统也会检查包的来源是否是合法的

检查包的完整性和签名

rpm -K|--checksig rpmfile

范例:

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
rpm -qa “gpg-pubkey*”

范例:CentOS 8

[root@rocky8 ~]# rpm -K /misc/cd/AppStream/Packages/httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64.rpm
/misc/cd/AppStream/Packages/httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64.rpm: digests SIGNATURES NOT OK
#提示没有导入key

[root@rocky8 ~]# cat /etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial 
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBGAofzYBEAC6yS1azw6f3wmaVd//3aSy6O2c9+jeetulRQvg2LvhRRS1eNqp
/x9tbBhfohu/tlDkGpYHV7diePgMml9SZDy1sKlI3tDhx6GZ3xwF0fd1vWBZpmNk
D9gRkUmYBeLotmcXQZ8ZpWLicosFtDpJEYpLUhuIgTKwt4gxJrHvkWsGQiBkJxKD
u3/RlL4IYA3Ot9iuCBflc91EyAw1Yj0gKcDzbOqjvlGtS3ASXgxPqSfU0uLC9USF
uKDnP2tcnlKKGfj0u6VkqISliSuRAzjlKho9Meond+mMIFOTT6qp4xyu+9Dj3IjZ
IC6rBXRU3xi8z0qYptoFZ6hx70NV5u+0XUzDMXdjQ5S859RYJKijiwmfMC7gZQAf
OkdOcicNzen/TwD/slhiCDssHBNEe86Wwu5kmDoCri7GJlYOlWU42Xi0o1JkVltN
D8ZId+EBDIms7ugSwGOVSxyZs43q2IAfFYCRtyKHFlgHBRe9/KTWPUrnsfKxGJgC
Do3Yb63/IYTvfTJptVfhQtL1AhEAeF1I+buVoJRmBEyYKD9BdU4xQN39VrZKziO3
hDIGng/eK6PaPhUdq6XqvmnsZ2h+KVbyoj4cTo2gKCB2XA7O2HLQsuGduHzYKNjf
QR9j0djjwTrsvGvzfEzchP19723vYf7GdcLvqtPqzpxSX2FNARpCGXBw9wARAQAB
tDNSZWxlYXNlIEVuZ2luZWVyaW5nIDxpbmZyYXN0cnVjdHVyZUByb2NreWxpbnV4
Lm9yZz6JAk4EEwEIADgWIQRwUcRwqSn0VM6+N7cVr12sbXRaYAUCYCh/NgIbDwUL
CQgHAgYVCgkICwIEFgIDAQIeAQIXgAAKCRAVr12sbXRaYLFmEACSMvoO1FDdyAbu
1m6xEzDhs7FgnZeQNzLZECv2j+ggFSJXezlNVOZ5I1I8umBan2ywfKQD8M+IjmrW
k9/7h9i54t8RS/RN7KNo7ECGnKXqXDPzBBTs1Gwo1WzltAoaDKUfXqQ4oJ4aCP/q
/XPVWEzgpJO1XEezvCq8VXisutyDiXEjjMIeBczxb1hbamQX+jLTIQ1MDJ4Zo1YP
zlUqrHW434XC2b1/WbSaylq8Wk9cksca5J+g3FqTlgiWozyy0uxygIRjb6iTzKXk
V7SYxeXp3hNTuoUgiFkjh5/0yKWCwx7aQqlHar9GjpxmBDAO0kzOlgtTw//EqTwR
KnYZLig9FW0PhwvZJUigr0cvs/XXTTb77z/i/dfHkrjVTTYenNyXogPtTtSyxqca
61fbPf0B/S3N43PW8URXBRS0sykpX4SxKu+PwKCqf+OJ7hMEVAapqzTt1q9T7zyB
QwvCVx8s7WWvXbs2d6ZUrArklgjHoHQcdxJKdhuRmD34AuXWCLW+gH8rJWZpuNl3
+WsPZX4PvjKDgMw6YMcV7zhWX6c0SevKtzt7WP3XoKDuPhK1PMGJQqQ7spegGB+5
DZvsJS48Ip0S45Qfmj82ibXaCBJHTNZE8Zs+rdTjQ9DS5qvzRA1sRA1dBb/7OLYE
JmeWf4VZyebm+gc50szsg6Ut2yT8hw==
=AiP8
-----END PGP PUBLIC KEY BLOCK-----

[root@rocky8 ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial #导入key
[root@rocky8 ~]# rpm -K /misc/cd/AppStream/Packages/httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64.rpm 
/misc/cd/AppStream/Packages/httpd-2.4.37-39.module+el8.4.0+571+fd70afb1.x86_64.rpm: digests signatures OK
#现在就能查到了

[root@rocky8 ~]# rpm -qa "gpg-pubkey*" #查看key是否导入
gpg-pubkey-6d745a60-60287f36

[root@rocky8 ~]# rpm -qi gpg-pubkey-6d745a60-60287f36 #查看key 信息
Name        : gpg-pubkey
Version     : 6d745a60
Release     : 60287f36
Architecture: (none)
Install Date: Wed 06 Oct 2021 07:22:44 PM CST
Group       : Public Keys
Size        : 0
License     : pubkey
Signature   : (none)
Source RPM  : (none)
Build Date  : Sun 14 Feb 2021 09:39:02 AM CST
Build Host  : localhost
Relocations : (not relocatable)
Packager    : Release Engineering <infrastructure@rockylinux.org>
Summary     : gpg(Release Engineering <infrastructure@rockylinux.org>)
Description :
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: rpm-4.14.3 (NSS-3)

mQINBGAofzYBEAC6yS1azw6f3wmaVd//3aSy6O2c9+jeetulRQvg2LvhRRS1eNqp
/x9tbBhfohu/tlDkGpYHV7diePgMml9SZDy1sKlI3tDhx6GZ3xwF0fd1vWBZpmNk
D9gRkUmYBeLotmcXQZ8ZpWLicosFtDpJEYpLUhuIgTKwt4gxJrHvkWsGQiBkJxKD
u3/RlL4IYA3Ot9iuCBflc91EyAw1Yj0gKcDzbOqjvlGtS3ASXgxPqSfU0uLC9USF
uKDnP2tcnlKKGfj0u6VkqISliSuRAzjlKho9Meond+mMIFOTT6qp4xyu+9Dj3IjZ
IC6rBXRU3xi8z0qYptoFZ6hx70NV5u+0XUzDMXdjQ5S859RYJKijiwmfMC7gZQAf
OkdOcicNzen/TwD/slhiCDssHBNEe86Wwu5kmDoCri7GJlYOlWU42Xi0o1JkVltN
D8ZId+EBDIms7ugSwGOVSxyZs43q2IAfFYCRtyKHFlgHBRe9/KTWPUrnsfKxGJgC
Do3Yb63/IYTvfTJptVfhQtL1AhEAeF1I+buVoJRmBEyYKD9BdU4xQN39VrZKziO3
hDIGng/eK6PaPhUdq6XqvmnsZ2h+KVbyoj4cTo2gKCB2XA7O2HLQsuGduHzYKNjf
QR9j0djjwTrsvGvzfEzchP19723vYf7GdcLvqtPqzpxSX2FNARpCGXBw9wARAQAB
tDNSZWxlYXNlIEVuZ2luZWVyaW5nIDxpbmZyYXN0cnVjdHVyZUByb2NreWxpbnV4
Lm9yZz6JAk4EEwEIADgWIQRwUcRwqSn0VM6+N7cVr12sbXRaYAUCYCh/NgIbDwUL
CQgHAgYVCgkICwIEFgIDAQIeAQIXgAAKCRAVr12sbXRaYLFmEACSMvoO1FDdyAbu
1m6xEzDhs7FgnZeQNzLZECv2j+ggFSJXezlNVOZ5I1I8umBan2ywfKQD8M+IjmrW
k9/7h9i54t8RS/RN7KNo7ECGnKXqXDPzBBTs1Gwo1WzltAoaDKUfXqQ4oJ4aCP/q
/XPVWEzgpJO1XEezvCq8VXisutyDiXEjjMIeBczxb1hbamQX+jLTIQ1MDJ4Zo1YP
zlUqrHW434XC2b1/WbSaylq8Wk9cksca5J+g3FqTlgiWozyy0uxygIRjb6iTzKXk
V7SYxeXp3hNTuoUgiFkjh5/0yKWCwx7aQqlHar9GjpxmBDAO0kzOlgtTw//EqTwR
KnYZLig9FW0PhwvZJUigr0cvs/XXTTb77z/i/dfHkrjVTTYenNyXogPtTtSyxqca
61fbPf0B/S3N43PW8URXBRS0sykpX4SxKu+PwKCqf+OJ7hMEVAapqzTt1q9T7zyB
QwvCVx8s7WWvXbs2d6ZUrArklgjHoHQcdxJKdhuRmD34AuXWCLW+gH8rJWZpuNl3
+WsPZX4PvjKDgMw6YMcV7zhWX6c0SevKtzt7WP3XoKDuPhK1PMGJQqQ7spegGB+5
DZvsJS48Ip0S45Qfmj82ibXaCBJHTNZE8Zs+rdTjQ9DS5qvzRA1sRA1dBb/7OLYE
JmeWf4VZyebm+gc50szsg6Ut2yT8hw==
=AiP8
-----END PGP PUBLIC KEY BLOCK-----

范例:校验包文件

[root@rocky8 ~]# cp /misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm /data
[root@rocky8 ~]# cd /data
[root@rocky8 data]# ll
total 60
-r--r--r-- 1 root root 59636 Oct 13 20:40 tree-1.7.0-15.el8.x86_64.rpm
[root@rocky8 data]# echo >>tree-1.7.0-15.el8.x86_64.rpm
[root@rocky8 data]# ll
total 60
-r--r--r-- 1 root root 59637 Oct 13 20:40 tree-1.7.0-15.el8.x86_64.rpm
[root@rocky8 data]# rpm -K /data/tree-1.7.0-15.el8.x86_64.rpm
/data/tree-1.7.0-15.el8.x86_64.rpm: DIGESTS SIGNATURES NOT OK
#更改了包,还是可以检查,它是检查签名,不校验包的完整性

软件在安装时,会将包里的每个文件的元数据,如:大小,权限,所有者,时间等记录至rpm相关的数据库中,可以用来检查包中的文件是否和当初安装时有所变化

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

#示例:
[root@centos8 ~]#rpm -V centos-release

S.5....T. c /etc/issue
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 mTime differs
P capabilities differ
. (点) 表示当前位置代表的字符含义一致

c 所在的位置表示文件类型

c 配置文件
d 文件数据文件
g 该文件不属于某个文件(少数情况)
l 许可证文件(license file)
r 自述文件(READ ME)

范例:

[root@rocky8 data]# rpm -ql tree
/usr/bin/tree
/usr/lib/.build-id
/usr/lib/.build-id/47
/usr/lib/.build-id/47/6e749c56abc75d25e3bc5c29e83507a605fd53
/usr/share/doc/tree
/usr/share/doc/tree/LICENSE
/usr/share/doc/tree/README
/usr/share/man/man1/tree.1.gz
[root@rocky8 data]# ll /usr/share/doc/tree/README
-rw-r--r-- 1 root root 5620 Apr 22  2014 /usr/share/doc/tree/README

[root@rocky8 data]# chown raymond /usr/share/doc/tree/README
[root@rocky8 data]# ll /usr/share/doc/tree/README
-rw-r--r-- 1 raymond root 5620 Apr 22  2014 /usr/share/doc/tree/README
[root@rocky8 data]# rpm -V tree
.....U...  d /usr/share/doc/tree/README
#U  表示文件的所有者被改
#-V  查看文件完整性

[root@rocky8 data]# chown root /usr/share/doc/tree/README
[root@rocky8 data]# rpm -V tree
#恢复之后就没有了

3.6 数据库

rpm包安装时生成的信息,都放在rpm数据库中

/var/lib/rpm

可以重建数据库

rpm {--initdb|--rebuilddb}
initdb: 初始化,如果事先不存在数据库,则新建之,否则,不执行任何操作
rebuilddb:重建已安装的包头的数据库索引目录
上一篇:Web安全形势严峻,防御DDoS势在必行,这些必要措施都有吗?


下一篇:深入Hotspot源码与X86操作系统分析Valitaile变量与Lock前缀