Product SiteDocumentation Site

5.4. 以 dpkg 管理套件

dpkg is the base command for handling Debian packages on the system. If you have .deb packages, it is dpkg that allows installation or analysis of their contents. But this program only has a partial view of the Debian universe: it knows what is installed on the system, and whatever it is given on the command line, but knows nothing of the other available packages. As such, it will fail if a dependency is not met. Tools such as apt and aptitude, on the contrary, will create a list of dependencies to install everything as automatically as possible.

5.4.1. 安裝套件

首先,dpkg 是安裝 Debian 已經可用套件的工具 (因為不需下載任何東西)。我們使用它的 -i--install 選項。

範例 5.2. 以 dpkg 安裝套件

# dpkg -i man-db_2.9.4-2_amd64.deb
(Reading database ... 227466 files and directories currently installed.)
Preparing to unpack man-db_2.9.4-2_amd64.deb ...
Unpacking man-db (2.9.4-2) over (2.8.5-2) ...
Setting up man-db (2.9.4-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.69) ...
We can see the different steps performed by dpkg; we know, thus, at what point any error may have occurred. The installation can also be effected in two stages: first unpacking, then configuration. apt takes advantage of this, limiting the number of calls to dpkg (since each call is costly, due to loading of the database in memory, especially the list of already installed files).

範例 5.3. 分開解壓與組態

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

dpkg 安裝套件時偶而失敗並送回錯誤的訊息;若使用者要求忽略該等訊息,就能送出警示;這就是 --force-* 選項的用途。dpkg --force-help 命令,或此命令的文件,將提供完整的選項。最常發生的錯誤,遲早每個人都會碰到,就是檔案衝突。已被其他套件安裝使用的檔案,另個套件同時需要用到它時,dpkg 會於安裝時再次使手它。此時出現以下的訊息:
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. 移除套件

Invoking dpkg with the -r or --remove option, followed by the name of a package, removes that package. This removal is, however, not complete: all of the configuration files, maintainer scripts, log files (system logs) and other user data handled by the package remain. That way disabling the program is easily done by uninstalling it, and it is still possible to quickly reinstall it with the same configuration. To completely remove everything associated with a package, use the -P or --purge option, followed by the package name.

範例 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
$ dpkg -S /bin/date
coreutils: /bin/date
$ dpkg -s coreutils
Package: coreutils
Essential: yes
Status: install ok installed
Priority: required
Section: utils
Installed-Size: 17478
Maintainer: Michael Stone <>
Architecture: amd64
Multi-Arch: foreign
Source: coreutils (8.32-4)
Version: 8.32-4+b1
Pre-Depends: libacl1 (>= 2.2.23), libattr1 (>= 1:2.4.44), libc6 (>= 2.28), libgmp10, 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
$ dpkg -l 'b*'
| 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  backupninja                <none>         <none>       (no description available)
un  backuppc                   <none>         <none>       (no description available)
ii  baloo-kf5                  5.78.0-3       amd64        framework for searching and manag>
un  balsa                      <none>         <none>       (no description available)
ii  baobab                     3.38.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                 11.1           amd64        Debian base system miscellaneous >
ii  base-passwd                3.5.51         amd64        Debian base system master passwor>
ii  bash                       5.1-2+b1       amd64        GNU Bourne Again SHell
$ dpkg -c /var/cache/apt/archives/bash_5.1-3+b1_amd64.deb
drwxr-xr-x root/root         0 2021-07-25 20:43 ./
drwxr-xr-x root/root         0 2021-07-25 20:43 ./bin/
-rwxr-xr-x root/root   1234376 2021-07-25 20:43 ./bin/bash
drwxr-xr-x root/root         0 2021-07-25 20:43 ./etc/
-rw-r--r-- root/root      1994 2021-07-25 20:43 ./etc/bash.bashrc
drwxr-xr-x root/root         0 2021-07-25 20:43 ./etc/skel/
-rw-r--r-- root/root       220 2021-07-25 20:43 ./etc/skel/.bash_logout
-rw-r--r-- root/root      3526 2021-07-25 20:43 ./etc/skel/.bashrc
-rw-r--r-- root/root       807 2021-07-25 20:43 ./etc/skel/.profile
drwxr-xr-x root/root         0 2021-07-25 20:43 ./usr/
drwxr-xr-x root/root         0 2021-07-25 20:43 ./usr/bin/
-rwxr-xr-x root/root      6759 2021-07-25 20:43 ./usr/bin/bashbug
-rwxr-xr-x root/root     14648 2021-07-25 20:43 ./usr/bin/clear_console
drwxr-xr-x root/root         0 2021-07-25 20:43 ./usr/share/
drwxr-xr-x root/root         0 2021-07-25 20:43 ./usr/share/doc/
$ dpkg -I /var/cache/apt/archives/bash_5.1-3+b1_amd64.deb
 new Debian package, version 2.0.
 size 1416600 bytes: control archive=7256 bytes.
      77 bytes,     4 lines      conffiles
    1030 bytes,    27 lines      control
    4511 bytes,    64 lines      md5sums
     603 bytes,    31 lines   *  postinst             #!/bin/bash
     500 bytes,    25 lines   *  postrm               #!/bin/sh
   14536 bytes,    33 lines   *  preinst              
     289 bytes,    22 lines   *  prerm                #!/bin/bash
 Package: bash
 Source: bash (5.1-3)
 Version: 5.1-3+b1
 Architecture: amd64
 Essential: yes
 Maintainer: Matthias Klose <>
 Installed-Size: 6470
 Pre-Depends: libc6 (>= 2.25), libtinfo6 (>= 6)
 Depends: base-files (>= 2.1.12), debianutils (>= 2.15)
 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
 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 套件在其控制資訊內有個 Architecture 欄位。其內容為 “all” (表示該套件不受架構限制) 或指出其適用的架構 (如 “amd64”、“armhf”、…)。以後例而言,主機的架構符合後,dpkg 才開始安裝該套件,並送回 dpkg --print-architecture
此限制確保使用者不會用到不合其架構的執行檔。除了可以執行多個架構的電腦之外,不論在地的 ( “amd64“ 系統可執行 “i386” 執行檔) 或虛擬的電腦都可找到適用的套件。 啟用多架構

dpkg 的多架構支授允許使用者限制 “怪異的架構” 安裝在現行的系統上。以下例的 dpkg --add-architecture 就能做到。有個對應的 dpkg --remove-architecture 指令可以除去怪異的架構,不過祗發生於無套件可用時。
# dpkg --print-architecture
# dpkg --print-foreign-architectures
# dpkg -i gcc-9-base_9.3.0-22_armhf.deb
dpkg: error processing archive gcc-9-base_9.3.0-22_armhf.deb (--install):
 package architecture (armhf) does not match system (amd64)
Errors were encountered while processing:
# dpkg --add-architecture armhf
# dpkg --add-architecture armel
# dpkg --print-foreign-architectures
# dpkg -i gcc-9-base_9.3.0-22_armhf.deb
(Reading database ... 456367 files and directories currently installed.)
Preparing to unpack gcc-9-base_9.3.0-22_armhf.deb ...
Unpacking gcc-9-base:armhf (9.3.0-22) ...
Setting up gcc-9-base:armhf (9.3.0-22) ...
# dpkg --remove-architecture armhf
dpkg: error: cannot remove architecture 'armhf' currently in use by the database
# dpkg --remove-architecture armel
# dpkg --print-foreign-architectures
armhf 多架構相關的改變

To make multi-arch actually useful and usable, libraries had to be repackaged and moved to an architecture-specific directory so that multiple copies (targeting different architectures) can be installed alongside. Such updated packages contain the “Multi-Arch: same” header field to tell the packaging system that the various architectures of the package can be safely co-installed (and that those packages can only satisfy dependencies of packages of the same architecture). The most important libraries have been converted since the introduction of multi-arch in Debian 7 Wheezy, but there are many libraries that will likely never be converted unless someone specifically requests it (through a bug report for example).
$ dpkg -s gcc-9-base
dpkg-query: error: --status needs a valid package name but 'gcc-9-base' is not: ambiguous package name 'gcc-9-base' with more than one installed instance

Use --help for help about querying packages.
$ dpkg -s gcc-9-base:amd64 gcc-9-base:armhf | grep ^Multi
Multi-Arch: same
Multi-Arch: same
$ dpkg -L libgcc-s1:amd64 |grep .so
$ dpkg -S /usr/share/doc/gcc-9-base/copyright
gcc-9-base:amd64, gcc-9-base:armhf: /usr/share/doc/gcc-9-base/copyright
Multi-Arch: same 套件的名稱必須符合其架構才能被明確的指認出來。也可能以相同的套件在其他情況下共享檔案;共享時,dpkg 確認所有套件的每個位元都相同。最後但不是最不重要,套件的所有實例必須使用相同的版本。也必須一起更新。
多架構支援帶來處理相依性的有趣挑戰。滿足相依性的套件標示為 “Multi-Arch: foreign” 或宣告其匹配架構的相依性 (在這種相依性解決過程中,假設架構獨立的套件與主機不同)。相依性可能被弱化以允許納入其他架構,以 套件:any 語法,但怪異套件祗需滿足一個相依性,如果標示為 “Multi-Arch: allowed”。