Linux Ubuntu dpkg 命令詳解


Ubuntu 系統 dpkg 命令使用詳解

  dpkg 即 package manager for Debian ,是 Debian 和基於 Debian 的系統中一個主要的包管理工具,可以用來安裝、構建、卸載、管理 deb 格式的軟件包。dpkg的主要和更友好的前端是aptitude。DPKG本身完全由命令行參數控制,命令行參數只包含一個動作和零個或多個選項。動作參數告訴dpkg要做什么,選項以某種方式控制動作的行為。
  
  `dpkg` 主要是用來安裝已經下載到本地的 `deb` 軟件包,或者對已經安裝好的軟件進行管理。

  apt和`apt-get`是類似紅帽系的yum工具,能夠自行解決deb包的依賴關系。直接從遠程的軟件倉庫下載安裝軟件。apt命令可可參考Linux apt 命令


dpkg 命令命令詳解

   dpkg -i 手動安裝軟件包(這個命令並不能解決軟件包之前的依賴性問題),如果在安裝某一個軟件包的時候遇到了軟件依賴的問題,可以用apt-get -f install在解決信賴性這個問題.
   
   dpkg --info "軟件包名" --列出軟件包解包后的包名稱.

   dpkg -l --列出當前系統中所有的包.可以和參數less一起使用在分屏查看. (類似於rpm -qa)

   dpkg -l <package_name> 查詢軟件包的信息
   
   dpkg -l |grep -i "軟件包名" --查看系統中與"軟件包名"相關聯的包.

   dpkg -s 查詢已安裝的包的詳細信息.

   dpkg -L 查詢系統中已安裝的軟件包所安裝的位置. (類似於rpm -ql)

   dpkg -S 查詢系統中某個文件屬於哪個軟件包. (類似於rpm -qf)

   dpkg -I 查詢deb包的詳細信息,在一個軟件包下載到本地之后看看用不用安裝(看一下唄).

   dpkg -r 卸載軟件包.不是完全的卸載,它的配置文件還存在.

   dpkg -P 全部卸載(但是還是不能解決軟件包的依賴性的問題)

   dpkg -reconfigure 重新配置


示例

軟件安裝

$ sudo dpkg -i fping_4.2-1_amd64.deb
(正在讀取數據庫 ... 系統當前共安裝有 252654 個文件和目錄。)
准備解壓 fping_4.2-1_amd64.deb  ...
正在解壓 fping (4.2-1) 並覆蓋 (4.2-1) ...
正在設置 fping (4.2-1) ...
正在處理用於 man-db (2.8.5-2) 的觸發器 ...

查詢vim包信息

$ dpkg -l vim
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
+++-==============-===================-============-=================================
ii  vim            2:8.1.2269-1ubuntu5 amd64        Vi IMproved - enhanced vi editor

列出已安裝的軟件

可以使用 dpkg -l 命令列出當前系統中已經安裝的軟件以及軟件包的狀態。如:

$ dpkg -l
期望狀態=未知(u)/安裝(i)/刪除(r)/清除(p)/保持(h)
| 狀態=未安裝(n)/已安裝(i)/僅存配置(c)/僅解壓縮(U)/配置失敗(F)/不完全安裝(H)/觸發器等待(W)/觸發器未決(T)
|/ 錯誤?=(無)/須重裝(R) (狀態,錯誤:大寫=故障)
||/ 名稱                                          版本                                體系結構     描述
+++-=============================================-===================================-============-===============================================================================
ii  2048-qt                                       0.1.6-1build1                       amd64        mathematics based puzzle game
ii  accountsservice                               0.6.50-0ubuntu1                     amd64        query and manipulate user account information
ii  acl                                           2.2.53-4                            amd64        access control list - utilities
ii  acpi-support                                  0.143                               amd64        scripts for handling many ACPI events
ii  acpid                                         1:2.0.31-1ubuntu2                   amd64        Advanced Configuration and Power Interface event daemon
ii  adduser                                       3.118ubuntu1                        all          add and remove users and groups
ii  adwaita-icon-theme                            3.32.0-1ubuntu1                     all          default icon theme of GNOME (small subset)
ii  alsa-base                                     1.0.25+dfsg-0ubuntu5                all          ALSA driver configuration files
ii  alsa-utils                                    1.1.8-1ubuntu1                      amd64        Utilities for configuring and using ALSA
...

該命令每行輸出中的第一列 ii 表示軟件包的安裝和配置狀態,其格式如下:
期望狀態|當前狀態|錯誤
其中期望狀態有以下幾種

  • u:即 unknown,軟件包未安裝且用戶未請求安裝
  • i:即 install,用戶請求安裝該軟件包
  • r:即 remove,用戶請求卸載該軟件包
  • p:即 purge,用戶請求卸載該軟件包並清理配置文件
  • h:即 hold,用戶請求保持續當前軟件包版本

當前狀態 有以下幾種:

  • n:即 not-installed,軟件包未安裝
  • i:即 installed,軟件包已安裝並完成配置
  • c:即 config-files,軟件包已經被卸載,但是其配置文件未清理
  • u:即 unpacked,軟件包已經被解壓縮,但還未配置
  • f:即 half-configured,配置軟件包時出現錯誤
  • w:即 triggers-awaited,觸發器等待
  • t:即 triggers-pending,觸發器未決

錯誤狀態 有以下幾種:

  • h:軟件包被強制保持
  • r:即 reinstall-required,需要卸載並重新安裝
  • x:軟件包被破壞

因此 ii 表示該軟件需要安裝且已經安裝,沒有出現錯誤;
iu 表示已經安裝該軟件,但未正確配置;
rc 表示該軟件已經被刪除,但配置文件未清理。

查看處於 rc 狀態的軟件包:

$ dpkg -l | grep ^rc
rc  libmhash2:amd64                               0.9.9.9-7                           amd64        Library for cryptographic hashing and message authentication
rc  linux-image-5.0.0-13-generic                  5.0.0-13.14                         amd64        Signed kernel image generic
rc  linux-modules-5.0.0-13-generic                5.0.0-13.14                         amd64        Linux kernel extra modules for version 5.0.0 on 64 bit x86 SMP
rc  linux-modules-extra-5.0.0-13-generic          5.0.0-13.14                         amd64        Linux kernel extra modules for version 5.0.0 on 64 bit x86 SMP
rc  zabbix-proxy-mysql                            1:4.0.4+dfsg-1                      amd64        network monitoring solution - proxy (using MySQL)

此外,還可以使用 dpkg -l <package_name_pattern> 命令篩選出名稱中包含指定模式的軟件包。

$ dpkg -l "nginx*"
期望狀態=未知(u)/安裝(i)/刪除(r)/清除(p)/保持(h)
| 狀態=未安裝(n)/已安裝(i)/僅存配置(c)/僅解壓縮(U)/配置失敗(F)/不完全安裝(H)/觸發器等待(W)/觸發器未決(T)
|/ 錯誤?=(無)/須重裝(R) (狀態,錯誤:大寫=故障)
||/ 名稱           版本            體系結構     描述
+++-==============-===============-============-=========================================================
ii  nginx          1.15.9-0ubuntu1 all          small, powerful, scalable web/proxy server
ii  nginx-common   1.15.9-0ubuntu1 all          small, powerful, scalable web/proxy server - common files
ii  nginx-core     1.15.9-0ubuntu1 amd64        nginx web/proxy server (standard version)
un  nginx-doc      <無>            <無>         (無描述)
un  nginx-extras   <無>            <無>         (無描述)
un  nginx-full     <無>            <無>         (無描述)
un  nginx-light    <無>            <無>         (無描述)

卸載軟件

dpkg 命令的 -r 選項可以用來卸載已安裝的軟件包,此時只需要指定軟件的名稱即可。

$ sudo dpkg -r vim
(正在讀取數據庫 ... 系統當前共安裝有 252653 個文件和目錄。)
正在卸載 vim (2:8.1.0320-1ubuntu3.1) ...
update-alternatives: 使用 /usr/bin/vim.tiny 來在自動模式中提供 /usr/bin/vi (vi)
update-alternatives: 使用 /usr/bin/vim.tiny 來在自動模式中提供 /usr/bin/view (view)
update-alternatives: 使用 /usr/bin/vim.tiny 來在自動模式中提供 /usr/bin/ex (ex)
update-alternatives: 使用 /usr/bin/vim.tiny 來在自動模式中提供 /usr/bin/rview (rview)

需要注意的是,-r 選項只會移除指定的軟件包而不對其配置文件產生影響,可以使用 -P 選項在刪除軟件包的同時清理配置文件。
sudo dpkg -P <package>

其他包管理操作

查看軟件包的內容
dpkg -c <package_file_path>
$ dpkg -c fping_4.2-1_amd64.deb
drwxr-xr-x root/root         0 2019-02-20 06:27 ./
drwxr-xr-x root/root         0 2019-02-20 06:27 ./usr/
drwxr-xr-x root/root         0 2019-02-20 06:27 ./usr/bin/
-rwxr-xr-x root/root     52128 2019-02-20 06:27 ./usr/bin/fping
drwxr-xr-x root/root         0 2019-02-20 06:27 ./usr/share/
drwxr-xr-x root/root         0 2019-02-20 06:27 ./usr/share/bug/
-rwxr-xr-x root/root       118 2017-06-19 05:19 ./usr/share/bug/fping
drwxr-xr-x root/root         0 2019-02-20 06:27 ./usr/share/doc/
drwxr-xr-x root/root         0 2019-02-20 06:27 ./usr/share/doc/fping/
-rw-r--r-- root/root       495 2017-09-06 08:00 ./usr/share/doc/fping/NEWS.Debian.gz
-rw-r--r-- root/root      1615 2019-02-20 06:27 ./usr/share/doc/fping/changelog.Debian.gz
-rw-r--r-- root/root      3445 2017-12-07 04:09 ./usr/share/doc/fping/copyright
drwxr-xr-x root/root         0 2019-02-20 06:27 ./usr/share/lintian/
drwxr-xr-x root/root         0 2019-02-20 06:27 ./usr/share/lintian/overrides/
-rw-r--r-- root/root        41 2017-06-19 05:19 ./usr/share/lintian/overrides/fping
drwxr-xr-x root/root         0 2019-02-20 06:27 ./usr/share/man/
drwxr-xr-x root/root         0 2019-02-20 06:27 ./usr/share/man/man8/
-rw-r--r-- root/root      5733 2019-02-20 06:27 ./usr/share/man/man8/fping.8.gz
-rw-r--r-- root/root      1512 2019-02-20 06:27 ./usr/share/man/man8/fping6.8.gz
lrwxrwxrwx root/root         0 2019-02-20 06:27 ./usr/bin/fping6 -> fping
查看軟件包(已安裝)的詳細信息
dpkg -s <package>` 或 `dpkg --status <package>
$ dpkg -s fping
Package: fping
Status: deinstall ok installed
Priority: optional
Section: net
Installed-Size: 87
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Version: 4.2-1
Depends: libcap2-bin, netbase, libc6 (>= 2.15)
Enhances: netdata (>= 1.5)
Description: sends ICMP ECHO_REQUEST packets to network hosts
 fping is a ping like program which uses the Internet Control Message Protocol
 (ICMP) echo request to determine if a target host is responding.  fping
 differs from ping in that you can specify any number of targets on the command
 line, or specify a file containing the lists of targets to ping.  Instead of
 sending to one target until it times out or replies, fping will send out a
 ping packet and move on to the next target in a round-robin fashion.
Original-Maintainer: Axel Beckert <abe@debian.org>
Homepage: https://www.fping.org/
查看軟件包的安裝位置
dpkg -L <package>` 或 `dpkg --list-files <package>
$ dpkg -L fping
/.
/usr
/usr/bin
/usr/bin/fping
/usr/share
/usr/share/bug
/usr/share/bug/fping
/usr/share/doc
/usr/share/doc/fping
/usr/share/doc/fping/NEWS.Debian.gz
/usr/share/doc/fping/changelog.Debian.gz
/usr/share/doc/fping/copyright
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/fping
/usr/share/man
/usr/share/man/man8
/usr/share/man/man8/fping.8.gz
/usr/share/man/man8/fping6.8.gz
/usr/bin/fping6
篩選出包含指定文件(模式)的軟件包
dpkg -S <filename_pattern>` 或 `dpkg --search <filename_pattern>
$ dpkg -S sites-available
apache2: /etc/apache2/sites-available/default-ssl.conf
apache2: /etc/apache2/sites-available/000-default.conf
nginx-common: /etc/nginx/sites-available
nginx-common: /etc/nginx/sites-available/default
apache2: /etc/apache2/sites-available

dpkg 參數

root@Ubuntu20.04:~# dpkg --help
Usage: dpkg [<option> ...] <command>

Commands:
  -i|--install       <.deb file name>... | -R|--recursive <directory>...
  --unpack           <.deb file name>... | -R|--recursive <directory>...
  -A|--record-avail  <.deb file name>... | -R|--recursive <directory>...
  --configure        <package>... | -a|--pending
  --triggers-only    <package>... | -a|--pending
  -r|--remove        <package>... | -a|--pending
  -P|--purge         <package>... | -a|--pending
  -V|--verify [<package>...]       Verify the integrity of package(s).
  --get-selections [<pattern>...]  Get list of selections to stdout.
  --set-selections                 Set package selections from stdin.
  --clear-selections               Deselect every non-essential package.
  --update-avail [<Packages-file>] Replace available packages info.
  --merge-avail [<Packages-file>]  Merge with info from file.
  --clear-avail                    Erase existing available info.
  --forget-old-unavail             Forget uninstalled unavailable pkgs.
  -s|--status [<package>...]       Display package status details.
  -p|--print-avail [<package>...]  Display available version details.
  -L|--listfiles <package>...      List files 'owned' by package(s).
  -l|--list [<pattern>...]         List packages concisely.
  -S|--search <pattern>...         Find package(s) owning file(s).
  -C|--audit [<package>...]        Check for broken package(s).
  --yet-to-unpack                  Print packages selected for installation.
  --predep-package                 Print pre-dependencies to unpack.
  --add-architecture <arch>        Add <arch> to the list of architectures.
  --remove-architecture <arch>     Remove <arch> from the list of architectures.
  --print-architecture             Print dpkg architecture.
  --print-foreign-architectures    Print allowed foreign architectures.
  --assert-<feature>               Assert support for the specified feature.
  --validate-<thing> <string>      Validate a <thing>'s <string>.
  --compare-versions <a> <op> <b>  Compare version numbers - see below.
  --force-help                     Show help on forcing.
  -Dh|--debug=help                 Show help on debugging.

  -?, --help                       Show this help message.
      --version                    Show the version.

Assertable features: support-predepends, working-epoch, long-filenames,
  multi-conrep, multi-arch, versioned-provides.

Validatable things: pkgname, archname, trigname, version.

Use dpkg with -b, --build, -c, --contents, -e, --control, -I, --info,
  -f, --field, -x, --extract, -X, --vextract, --ctrl-tarfile, --fsys-tarfile
on archives (type dpkg-deb --help).

Options:
  --admindir=<directory>     Use <directory> instead of /var/lib/dpkg.
  --root=<directory>         Install on a different root directory.
  --instdir=<directory>      Change installation dir without changing admin dir.
  --path-exclude=<pattern>   Do not install paths which match a shell pattern.
  --path-include=<pattern>   Re-include a pattern after a previous exclusion.
  -O|--selected-only         Skip packages not selected for install/upgrade.
  -E|--skip-same-version     Skip packages whose same version is installed.
  -G|--refuse-downgrade      Skip packages with earlier version than installed.
  -B|--auto-deconfigure      Install even if it would break some other package.
  --[no-]triggers            Skip or force consequential trigger processing.
  --verify-format=<format>   Verify output format (supported: 'rpm').
  --no-debsig                Do not try to verify package signatures.
  --no-act|--dry-run|--simulate
                             Just say what we would do - don't do it.
  -D|--debug=<octal>         Enable debugging (see -Dhelp or --debug=help).
  --status-fd <n>            Send status change updates to file descriptor <n>.
  --status-logger=<command>  Send status change updates to <command>'s stdin.
  --log=<filename>           Log status changes and actions to <filename>.
  --ignore-depends=<package>,...
                             Ignore dependencies involving <package>.
  --force-...                Override problems (see --force-help).
  --no-force-...|--refuse-...
                             Stop when problems encountered.
  --abort-after <n>          Abort after encountering <n> errors.

Comparison operators for --compare-versions are:
  lt le eq ne ge gt       (treat empty version as earlier than any version);
  lt-nl le-nl ge-nl gt-nl (treat empty version as later than any version);
  < << <= = >= >> >       (only for compatibility with control file syntax).

Use 'apt' or 'aptitude' for user-friendly package management.

dpkg-query

在軟件包查詢方面還有一個軟件dpkg-querydpkg有相同的功能,具體可參考:dpkg-query 命令

參考資料

15 Practical Examples of “dpkg commands” for Debian Based Distros
Linux軟件安裝管理之——dpkg與apt-*詳解
原文:https://www.jianshu.com/p/2ec0f4b945a2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM