Product SiteDocumentation Site

5.4. 通过dpkg来操作软件包

dpkg 是系统上处理 Debian 软件包的基础指令。如果有 .deb 软件包,那么可以用 dpkg 来安装或分析其内容。但此指令只能看到 Debian 世界的部分内容:它知道系统安装了那些软件包,以及命令行给出了什么,但不知道其它还有哪些可用的软件包。这样,如果没有相依性就会失败。如 aptaptitude 这样的工具,相反会可产生相依性清单,来尽量自动地安装软件包。

5.4.1. 安装软件包

首先,dpkg 是安装 Debian 已经可用软件包的工具 (因为不需下载任何东西)。我们使用它的 -i--install 选项。

例 5.2. 通过dpkg来安装一个软件包

# dpkg -i man-db_2.11.2-2_amd64.deb
(Reading database ... 166273 files and directories currently installed.)
Preparing to unpack man-db_2.11.2-2_amd64.deb ...
Unpacking man-db (2.11.2-2) over (2.11.2-2) ...
Setting up man-db (2.11.2-2) ...
Updating database of manual pages ...
man-db.service is a disabled or a static unit not running, not starting it.
Processing triggers for mailcap (3.70+nmu1) ...
可以看到由 dpkg 执行的不同步骤;这样,我们知道在哪一点会发生错误。安装也可以分为两个阶段来生效:首先拆包,然后配置。apt 会利用这个优点,来限制调用 dpkg 的次数(因为每次调用都有成本,要把数据加载到内存中,特别是已经安装的文件的列表)。

例 5.3. 分开解压和配置

# dpkg --unpack man-db_2.11.2-2_amd64.deb

(Reading database ... 166273 files and directories currently installed.)
Preparing to unpack man-db_2.11.2-2_amd64.deb ...
Unpacking man-db (2.11.2-2) over (2.11.2-2) ...
Processing triggers for mailcap (3.70+nmu1) ...
# dpkg --configure man-db
Setting up man-db (2.11.2-2) ...
Updating database of manual pages ...
man-db.service is a disabled or a static unit not running, not starting it.

Sometimes, dpkg will fail to install a package and return an error; if the user orders it to ignore this, it will only issue a warning; it is for this reason that we have the different --force-* options. The dpkg --force-help command, or documentation of this command, will give a complete list of these options. The most frequent error, which you are bound to encounter sooner or later, is a file collision. When a package contains a file that is already installed by another package, dpkg will refuse to install it. The following messages will then appear:
Unpacking libgdm (from .../libgdm_3.8.3-2_amd64.deb) ...
dpkg: 处理 /var/cache/apt/archives/libgdm_3.8.3-2_amd64.deb (--unpack) 时发生错误:
 试图覆写 '/usr/bin/gdmflexiserver',也同时在软件包 gdm3 3.4.1-9 内
在这种情况下,如果你认为文件替换该文件可能不会对系统的稳定性造成重大的风险(通常情况下),你可以使用--force-overwrite选项来告诉dpkg去忽略这个错误并且覆盖该文件。
While there are many available --force-* options, only --force-overwrite is likely to be used regularly. These options only exist for exceptional situations, rarely encountered in Debian Stable. It is better to leave them alone as much as possible in order to respect the rules imposed by the packaging mechanism. Do not forget, these rules ensure the consistency and stability of your system.

5.4.2. 软件包移除

使用 dpkg 加上 -r--remove 选项,再加上软件包名称,就可移除该软件包。然而,这种方式的移除并不完整:仍保留了所有的配置文件、维护脚本、日志文件(系统日志)及由软件包而处理的其他用户数据。停用程序的方式是卸载它,并且仍可以用相同的配置快速地重新安装回来。要完全移除与软件包相关的所有内容,应使用 -P--purge 选项,再加上软件包的名称。

例 5.4. 移除与清除 debian-cd 软件包

# dpkg -r debian-cd
(Reading database ... 228705 files and directories currently installed.)
Removing debian-cd (3.1.35) ...
# dpkg -P debian-cd
(Reading database ... 228049 files and directories currently installed.)
Purging configuration files for debian-cd (3.1.35) ...

5.4.3. 查询 dpkg 的数据库,并检查 .deb 文件

Before concluding this section, we will study dpkg options that query the internal database in order to obtain information. Giving first the long options and then corresponding short options (that will evidently take the same possible arguments) we cite
  • --listfiles package (or -L), which lists the files installed by this package;
  • --search file (or -S), which finds the package(s) containing the file;
  • --status package (or -s), which displays the headers of an installed package;
  • --list (or -l), which displays the list of packages known to the system and their installation status;
  • --contents file.deb (or -c), which lists the files in the Debian package specified;
  • --info file.deb (or -I), which displays the headers of this Debian package.

例 5.5. dpkg的各种查询

$ dpkg -L base-passwd
/.
/usr
/usr/sbin
/usr/sbin/update-passwd
/usr/share
/usr/share/base-passwd
/usr/share/base-passwd/group.master
/usr/share/base-passwd/passwd.master
/usr/share/doc
/usr/share/doc/base-passwd
/usr/share/doc/base-passwd/README
/usr/share/doc/base-passwd/changelog.gz
/usr/share/doc/base-passwd/copyright
/usr/share/doc/base-passwd/users-and-groups.html
/usr/share/doc/base-passwd/users-and-groups.txt.gz
/usr/share/doc-base
/usr/share/doc-base/base-passwd.users-and-groups
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/base-passwd
/usr/share/man
/usr/share/man/de
/usr/share/man/de/man8
/usr/share/man/de/man8/update-passwd.8.gz
/usr/share/man/es
/usr/share/man/es/man8
/usr/share/man/es/man8/update-passwd.8.gz
/usr/share/man/fr
/usr/share/man/fr/man8
/usr/share/man/fr/man8/update-passwd.8.gz
/usr/share/man/ja
/usr/share/man/ja/man8
/usr/share/man/ja/man8/update-passwd.8.gz
/usr/share/man/man8
/usr/share/man/man8/update-passwd.8.gz
/usr/share/man/pl
/usr/share/man/pl/man8
/usr/share/man/pl/man8/update-passwd.8.gz
/usr/share/man/ru
/usr/share/man/ru/man8
/usr/share/man/ru/man8/update-passwd.8.gz
$ dpkg -S /bin/date
coreutils: /bin/date
$ dpkg -s coreutils
Package: coreutils
Essential: yes
Status: install ok installed
Priority: required
Section: utils
Installed-Size: 18062
Maintainer: Michael Stone <mstone@debian.org>
Architecture: amd64
Multi-Arch: foreign
Version: 9.1-1
Pre-Depends: libacl1 (>= 2.2.23), libattr1 (>= 1:2.4.44), libc6 (>= 2.34), libgmp10 (>= 2:6.2.1+dfsg1), libselinux1 (>= 3.1~)
Description: GNU core utilities
 This package contains the basic file, shell and text manipulation
 utilities which are expected to exist on every operating system.
 .
 Specifically, this package includes:
 arch base64 basename cat chcon chgrp chmod chown chroot cksum comm cp
 csplit cut date dd df dir dircolors dirname du echo env expand expr
 factor false flock fmt fold groups head hostid id install join link ln
 logname ls md5sum mkdir mkfifo mknod mktemp mv nice nl nohup nproc numfmt
 od paste pathchk pinky pr printenv printf ptx pwd readlink realpath rm
 rmdir runcon sha*sum seq shred sleep sort split stat stty sum sync tac
 tail tee test timeout touch tr true truncate tsort tty uname unexpand
 uniq unlink users vdir wc who whoami yes
Homepage: http://gnu.org/software/coreutils
$ dpkg -l 'b*'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                       Version        Architecture Description
+++-==========================-==============-============-===================
un  backuppc                   <none>         <none>       (no description available)
un  balsa                      <none>         <none>       (no description available)
ii  baobab                     43.0-1         amd64        GNOME disk usage analyzer
un  base                       <none>         <none>       (no description available)
un  base-config                <none>         <none>       (no description available)
ii  base-files                 12.4+deb12u5   amd64        Debian base system miscellaneous files
ii  base-passwd                3.6.1          amd64        Debian base system master password and group files
ii  bash                       5.2.15-2+b2    amd64        GNU Bourne Again SHell
ii  bash-completion            1:2.11-6       all          programmable completion for the bash shell
[..]
$ dpkg -c /var/cache/apt/archives/bash_5.2.15-2+b2_amd64.deb
drwxr-xr-x root/root         0 2023-04-23 17:23 ./
drwxr-xr-x root/root         0 2023-04-23 17:23 ./bin/
-rwxr-xr-x root/root   1265648 2023-04-23 17:23 ./bin/bash
drwxr-xr-x root/root         0 2023-04-23 17:23 ./etc/
-rw-r--r-- root/root      1994 2023-04-23 17:23 ./etc/bash.bashrc
drwxr-xr-x root/root         0 2023-04-23 17:23 ./etc/skel/
-rw-r--r-- root/root       220 2023-04-23 17:23 ./etc/skel/.bash_logout
-rw-r--r-- root/root      3526 2023-04-23 17:23 ./etc/skel/.bashrc
-rw-r--r-- root/root       807 2023-04-23 17:23 ./etc/skel/.profile
drwxr-xr-x root/root         0 2023-04-23 17:23 ./usr/
drwxr-xr-x root/root         0 2023-04-23 17:23 ./usr/bin/
-rwxr-xr-x root/root      6865 2023-04-23 17:23 ./usr/bin/bashbug
-rwxr-xr-x root/root     14488 2023-04-23 17:23 ./usr/bin/clear_console
drwxr-xr-x root/root         0 2023-04-23 17:23 ./usr/share/
drwxr-xr-x root/root         0 2023-04-23 17:23 ./usr/share/debianutils/
drwxr-xr-x root/root         0 2023-04-23 17:23 ./usr/share/debianutils/shells.d/
-rw-r--r-- root/root        21 2023-04-23 17:23 ./usr/share/debianutils/shells.d/bash
drwxr-xr-x root/root         0 2023-04-23 17:23 ./usr/share/doc/
[..]
$ dpkg -I /var/cache/apt/archives/bash_5.2.15-2+b2_amd64.deb
 new Debian package, version 2.0.
 size 1490660 bytes: control archive=2972 bytes.
      77 bytes,     4 lines      conffiles
    1039 bytes,    27 lines      control
    4581 bytes,    65 lines      md5sums
     486 bytes,    24 lines   *  postinst             #!/bin/bash
     209 bytes,     5 lines   *  postrm               #!/bin/sh
     289 bytes,    22 lines   *  prerm                #!/bin/bash
 Package: bash
 Source: bash (5.2.15-2)
 Version: 5.2.15-2+b2
 Architecture: amd64
 Essential: yes
 Maintainer: Matthias Klose <doko@debian.org>
 Installed-Size: 7164
 Pre-Depends: libc6 (>= 2.36), libtinfo6 (>= 6)
 Depends: base-files (>= 2.1.12), debianutils (>= 5.6-0.1)
 Recommends: bash-completion (>= 20060301-0)
 Suggests: bash-doc
 Conflicts: bash-completion (<< 20060301-0)
 Replaces: bash-completion (<< 20060301-0), bash-doc (<= 2.05-1)
 Section: shells
 Priority: required
 Multi-Arch: foreign
 Homepage: http://tiswww.case.edu/php/chet/bash/bashtop.html
 Description: GNU Bourne Again SHell
  Bash is an sh-compatible command language interpreter that executes
  commands read from the standard input or from a file.  Bash also
  incorporates useful features from the Korn and C shells (ksh and csh).
  .
  Bash is ultimately intended to be a conformant implementation of the
  IEEE POSIX Shell and Tools specification (IEEE Working Group 1003.2).
  .
  The Programmable Completion Code, by Ian Macdonald, is now found in
  the bash-completion package.

5.4.4. dpkg的日志文件

dpkg 把所有的交易记录存在名为 /var/log/dpkg.log 的日志档。这个日志档的内容颇为详尽且啰唆,记录软件包被 dpkg 处理的每个进程。追踪 dpkg 的每个作为之外,日志档还保留系统发展的记录:可以覆查每个软件包的安装与移除步骤,对于了解最近的变动极有帮助。此外,还记录所有的版本,可以交互检查 changelog.Debian.gz 的内容,查看有问题的软件包或在线的错误报告。

5.4.5. 多架构支持

所有的 Debian 包在它们的管控信息中都有一个架构 字段。该字段可包含:“全部“(对与架构无关的包),或者它的目标架构名称(如“amd64”, “armhf”,…)。对于后者,默认情况下,dpkg将只能接受安装与主机架构匹配的软件包,主机架构可通过dpkg --print-architecture来获得。
该限制确保用户不会因为错误架构编译的二进制文件而挂掉。一切都很完美,除了(某些)计算机可以运行多架构的二进制文件,或者以本地方式(“amd64“系统可运行”i386“二进制文件)运行,或者以模拟器运行。

5.4.5.1. 启用多架构

dpkg的多架构支持允许用户定义可安装于当前系统的“异质架构” 。这可以方便的通过 dpkg --add-architecture完成,如下例。有一相应的 dpkg --remove-architecture 以放弃对异质架构的支持,但仅可用于未余留该架构软件包的情况。
# dpkg --print-architecture
amd64
# dpkg --print-foreign-architectures
# dpkg -i gcc-12-base_12.2.0-14_arm64.deb
dpkg: error: cannot access archive 'gcc-12-base_12.2.0-14_arm64.deb': No such file or directory
# dpkg --add-architecture arm64
# dpkg --add-architecture arm64
# dpkg --print-foreign-architectures
arm64
# dpkg -i gcc-12-base_12.2.0-14_arm64.deb
Selecting previously unselected package gcc-12-base:arm64.
(Reading database ... 166273 files and directories currently installed.)
Preparing to unpack gcc-12-base_12.2.0-14_arm64.deb ...
Unpacking gcc-12-base:arm64 (12.2.0-14) ...
Setting up gcc-12-base:arm64 (12.2.0-14) ...
# dpkg --remove-architecture arm64
dpkg: error: cannot remove architecture 'arm64' currently in use by the database
[..]
# dpkg --remove-architecture arm64
# dpkg --print-foreign-architectures
# 

5.4.5.2. 多架构相关的变更

要让多架构真正实用和可用,库文件必须要重新打包,并移动到于特定架构的目录中,以便可以并存安装多个副本(针对不同架构)。这样的更新软件包中包含“Multi-Arch: same”标头字段,以告诉打包系统此软件包的不同架构可以安全地并存安装(而且这些软件包仅能满足相同架构的软件包的依赖)。自从 Debian 7 Wheezy 引入多重架构后,最重要的库已经转换了。但也有很多库恐怕永远不会被转换,除非有人特别请求(例如通过缺陷报告)。
$ dpkg -s gcc-12-base
dpkg-query: error: --status needs a valid package name but 'gcc-12-base' is not: ambiguous package name 'gcc-12-base' with more than one installed instance

Use --help for help about querying packages.
$ dpkg -s gcc-12-base:arm64 | grep ^Multi
Multi-Arch: same
$ dpkg -L libgcc-s1:amd64 |grep .so
/lib/x86_64-linux-gnu/libgcc_s.so.1
$ dpkg -S /usr/share/doc/gcc-12-base/copyright
gcc-12-base:amd64, gcc-12-base:arm64: /usr/share/doc/gcc-12-base/copyright
值得注意的是,Multi-Arch: same软件包需具备能够明晰识别架构的名称。它们也能够与相同包的其他实例共享文件;dpkg确保所有的包在共享时具有逐位对应识别的文件。最后但并非最不重要的,一个包的所有实例必须为相同版本。它们必须同时升级。
多架构支持也带来了在依赖处理方式上的一些有趣的挑战。要满足依赖,需要:或者有Multi-Arch: foreign标识的软件包,或者架构上能匹配已声明依赖的软件包(在该依赖方案处理中,架构无关包假定为同样架构,而非主机架构)。通过 软件包 :any语法,依赖可以被弱化,以便允许任意架构能够满足依赖,但异质软件包仅能满足如下依赖:有“ Multi-Arch: allowed ”标识的依赖。