两木三草 發表於 2021-11-10 19:31:00

Linux Ubuntu dpkg 命令详解

<h1 id="ubuntu-系统-dpkg-命令使用详解">Ubuntu 系统 dpkg <strong>命令使用详解</strong></h1>
<p>  <code>dpkg</code> 即 package manager for Debian ,是 Debian 和基于 Debian 的系统中一个主要的<strong>包管理工具</strong>,可以用来安装、构建、卸载、管理 <code>deb</code> 格式的软件包。dpkg的主要和更友好的前端是aptitude。DPKG本身完全由命令行参数控制,命令行参数只包含一个动作和零个或多个选项。动作参数告诉dpkg要做什么,选项以某种方式控制动作的行为。<br>
  <br>
  `dpkg` 主要是用来安装已经下载到本地的 `deb` 软件包,或者对已经安装好的软件进行管理。</p>
<p>  <code>apt</code>和`apt-get`是类似红帽系的<code>yum</code>工具,能够自行解决<code>deb</code>包的依赖关系。直接从远程的软件仓库下载安装软件。apt命令可可参考Linux apt 命令</p>
<hr>
<h2 id="dpkg-命令命令详解">dpkg 命令命令详解</h2>
<p>   <code>dpkg -i </code>手动安装软件包(这个命令并不能解决软件包之前的依赖性问题),如果在安装某一个软件包的时候遇到了软件依赖的问题,可以用apt-get -f install在解决信赖性这个问题.<br>
   <br>
   <code>dpkg --info"软件包名"</code> --列出软件包解包后的包名称.</p>
<p>   <code>dpkg -l </code>--列出当前系统中所有的包.可以和参数less一起使用在分屏查看. (类似于rpm -qa)</p>
<p>   <code>dpkg -l &lt;package_name&gt;</code> 查询软件包的信息<br>
   <br>
   <code>dpkg -l |grep -i "软件包名"</code> --查看系统中与"软件包名"相关联的包.</p>
<p>   <code>dpkg -s </code>查询已安装的包的详细信息.</p>
<p>   <code>dpkg -L </code>查询系统中已安装的软件包所安装的位置. (类似于rpm -ql)</p>
<p>   <code>dpkg -S </code>查询系统中某个文件属于哪个软件包. (类似于rpm -qf)</p>
<p>   <code>dpkg -I </code>查询deb包的详细信息,在一个软件包下载到本地之后看看用不用安装(看一下呗).</p>
<p>   <code>dpkg -r </code>卸载软件包.不是完全的卸载,它的配置文件还存在.</p>
<p>   <code>dpkg -P </code>全部卸载(但是还是不能解决软件包的依赖性的问题)</p>
<p>   <code>dpkg -reconfigure </code>重新配置</p>
<hr>
<h2 id="示例">示例</h2>
<h4 id="软件安装">软件安装</h4>
<pre><code class="language-ruby">$ 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) 的触发器 ...
</code></pre>
<h4 id="查询vim包信息">查询vim包信息</h4>
<pre><code class="language-csharp">$ 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
+++-==============-===================-============-=================================
iivim            2:8.1.2269-1ubuntu5 amd64      Vi IMproved - enhanced vi editor
</code></pre>
<h4 id="列出已安装的软件">列出已安装的软件</h4>
<p>可以使用 <code>dpkg -l</code> 命令列出当前系统中已经安装的软件以及软件包的<strong>状态</strong>。如:</p>
<pre><code class="language-csharp">$ dpkg -l
期望状态=未知(u)/安装(i)/删除(r)/清除(p)/保持(h)
| 状态=未安装(n)/已安装(i)/仅存配置(c)/仅解压缩(U)/配置失败(F)/不完全安装(H)/触发器等待(W)/触发器未决(T)
|/ 错误?=(无)/须重装(R) (状态,错误:大写=故障)
||/ 名称                                          版本                              体系结构   描述
+++-=============================================-===================================-============-===============================================================================
ii2048-qt                                       0.1.6-1build1                     amd64      mathematics based puzzle game
iiaccountsservice                               0.6.50-0ubuntu1                     amd64      query and manipulate user account information
iiacl                                           2.2.53-4                            amd64      access control list - utilities
iiacpi-support                                  0.143                               amd64      scripts for handling many ACPI events
iiacpid                                       1:2.0.31-1ubuntu2                   amd64      Advanced Configuration and Power Interface event daemon
iiadduser                                       3.118ubuntu1                        all          add and remove users and groups
iiadwaita-icon-theme                            3.32.0-1ubuntu1                     all          default icon theme of GNOME (small subset)
iialsa-base                                     1.0.25+dfsg-0ubuntu5                all          ALSA driver configuration files
iialsa-utils                                    1.1.8-1ubuntu1                      amd64      Utilities for configuring and using ALSA
...
</code></pre>
<p>该命令每行输出中的第一列 <code>ii</code> 表示软件包的安装和配置状态,其格式如下:<br>
<code>期望状态|当前状态|错误</code><br>
其中<strong>期望状态</strong>有以下几种</p>
<ul>
<li>u:即 unknown,软件包未安装且用户未请求安装</li>
<li>i:即 install,用户请求安装该软件包</li>
<li>r:即 remove,用户请求卸载该软件包</li>
<li>p:即 purge,用户请求卸载该软件包并清理配置文件</li>
<li>h:即 hold,用户请求保持续当前软件包版本</li>
</ul>
<p><strong>当前状态</strong> 有以下几种:</p>
<ul>
<li>n:即 not-installed,软件包未安装</li>
<li>i:即 installed,软件包已安装并完成配置</li>
<li>c:即 config-files,软件包已经被卸载,但是其配置文件未清理</li>
<li>u:即 unpacked,软件包已经被解压缩,但还未配置</li>
<li>f:即 half-configured,配置软件包时出现错误</li>
<li>w:即 triggers-awaited,触发器等待</li>
<li>t:即 triggers-pending,触发器未决</li>
</ul>
<p><strong>错误状态</strong> 有以下几种:</p>
<ul>
<li>h:软件包被强制保持</li>
<li>r:即 reinstall-required,需要卸载并重新安装</li>
<li>x:软件包被破坏</li>
</ul>
<p>因此 <code>ii</code> 表示该软件需要安装且已经安装,没有出现错误;<br>
<code>iu</code> 表示已经安装该软件,但未正确配置;<br>
<code>rc</code> 表示该软件已经被删除,但配置文件未清理。</p>
<p>查看处于 <code>rc</code> 状态的软件包:</p>
<pre><code class="language-csharp">$ dpkg -l | grep ^rc
rclibmhash2:amd64                               0.9.9.9-7                           amd64      Library for cryptographic hashing and message authentication
rclinux-image-5.0.0-13-generic                  5.0.0-13.14                         amd64      Signed kernel image generic
rclinux-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
rclinux-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
rczabbix-proxy-mysql                            1:4.0.4+dfsg-1                      amd64      network monitoring solution - proxy (using MySQL)
</code></pre>
<p>此外,还可以使用 <code>dpkg -l &lt;package_name_pattern&gt;</code> 命令筛选出名称中包含指定模式的软件包。</p>
<pre><code class="language-xml">$ dpkg -l "nginx*"
期望状态=未知(u)/安装(i)/删除(r)/清除(p)/保持(h)
| 状态=未安装(n)/已安装(i)/仅存配置(c)/仅解压缩(U)/配置失败(F)/不完全安装(H)/触发器等待(W)/触发器未决(T)
|/ 错误?=(无)/须重装(R) (状态,错误:大写=故障)
||/ 名称         版本            体系结构   描述
+++-==============-===============-============-=========================================================
iinginx          1.15.9-0ubuntu1 all          small, powerful, scalable web/proxy server
iinginx-common   1.15.9-0ubuntu1 all          small, powerful, scalable web/proxy server - common files
iinginx-core   1.15.9-0ubuntu1 amd64      nginx web/proxy server (standard version)
unnginx-doc      &lt;无&gt;            &lt;无&gt;         (无描述)
unnginx-extras   &lt;无&gt;            &lt;无&gt;         (无描述)
unnginx-full   &lt;无&gt;            &lt;无&gt;         (无描述)
unnginx-light    &lt;无&gt;            &lt;无&gt;         (无描述)
</code></pre>
<h2 id="卸载软件">卸载软件</h2>
<p><code>dpkg</code> 命令的 <code>-r</code> 选项可以用来卸载已安装的软件包,此时只需要指定软件的名称即可。</p>
<pre><code class="language-ruby">$ 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)
</code></pre>
<p>需要注意的是,<code>-r</code> 选项只会移除指定的软件包而不对其配置文件产生影响,可以使用 <code>-P</code> 选项在删除软件包的同时清理配置文件。<br>
<code>sudo dpkg -P &lt;package&gt;</code></p>
<h2 id="其他包管理操作">其他包管理操作</h2>
<h5 id="查看软件包的内容">查看软件包的内容</h5>
<pre><code>dpkg -c &lt;package_file_path&gt;
</code></pre>
<pre><code class="language-ruby">$ 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 -&gt; fping
</code></pre>
<h5 id="查看软件包已安装的详细信息">查看软件包(已安装)的详细信息</h5>
<pre><code>dpkg -s &lt;package&gt;` 或 `dpkg --status &lt;package&gt;
</code></pre>
<pre><code class="language-csharp">$ dpkg -s fping
Package: fping
Status: deinstall ok installed
Priority: optional
Section: net
Installed-Size: 87
Maintainer: Ubuntu Developers &lt;ubuntu-devel-discuss@lists.ubuntu.com&gt;
Architecture: amd64
Version: 4.2-1
Depends: libcap2-bin, netbase, libc6 (&gt;= 2.15)
Enhances: netdata (&gt;= 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 &lt;abe@debian.org&gt;
Homepage: https://www.fping.org/
</code></pre>
<h5 id="查看软件包的安装位置">查看软件包的安装位置</h5>
<pre><code>dpkg -L &lt;package&gt;` 或 `dpkg --list-files &lt;package&gt;
</code></pre>
<pre><code class="language-ruby">$ 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
</code></pre>
<h5 id="筛选出包含指定文件模式的软件包">筛选出包含指定文件(模式)的软件包</h5>
<pre><code>dpkg -S &lt;filename_pattern&gt;` 或 `dpkg --search &lt;filename_pattern&gt;
</code></pre>
<pre><code class="language-jsx">$ 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
</code></pre>
<h2 id="dpkg-参数">dpkg 参数</h2>
<pre><code class="language-csharp">root@Ubuntu20.04:~# dpkg --help
Usage: dpkg [&lt;option&gt; ...] &lt;command&gt;

Commands:
-i|--install       &lt;.deb file name&gt;... | -R|--recursive &lt;directory&gt;...
--unpack         &lt;.deb file name&gt;... | -R|--recursive &lt;directory&gt;...
-A|--record-avail&lt;.deb file name&gt;... | -R|--recursive &lt;directory&gt;...
--configure      &lt;package&gt;... | -a|--pending
--triggers-only    &lt;package&gt;... | -a|--pending
-r|--remove      &lt;package&gt;... | -a|--pending
-P|--purge         &lt;package&gt;... | -a|--pending
-V|--verify [&lt;package&gt;...]       Verify the integrity of package(s).
--get-selections [&lt;pattern&gt;...]Get list of selections to stdout.
--set-selections               Set package selections from stdin.
--clear-selections               Deselect every non-essential package.
--update-avail [&lt;Packages-file&gt;] Replace available packages info.
--merge-avail [&lt;Packages-file&gt;]Merge with info from file.
--clear-avail                  Erase existing available info.
--forget-old-unavail             Forget uninstalled unavailable pkgs.
-s|--status [&lt;package&gt;...]       Display package status details.
-p|--print-avail [&lt;package&gt;...]Display available version details.
-L|--listfiles &lt;package&gt;...      List files 'owned' by package(s).
-l|--list [&lt;pattern&gt;...]         List packages concisely.
-S|--search &lt;pattern&gt;...         Find package(s) owning file(s).
-C|--audit [&lt;package&gt;...]      Check for broken package(s).
--yet-to-unpack                  Print packages selected for installation.
--predep-package               Print pre-dependencies to unpack.
--add-architecture &lt;arch&gt;      Add &lt;arch&gt; to the list of architectures.
--remove-architecture &lt;arch&gt;   Remove &lt;arch&gt; from the list of architectures.
--print-architecture             Print dpkg architecture.
--print-foreign-architectures    Print allowed foreign architectures.
--assert-&lt;feature&gt;               Assert support for the specified feature.
--validate-&lt;thing&gt; &lt;string&gt;      Validate a &lt;thing&gt;'s &lt;string&gt;.
--compare-versions &lt;a&gt; &lt;op&gt; &lt;b&gt;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=&lt;directory&gt;   Use &lt;directory&gt; instead of /var/lib/dpkg.
--root=&lt;directory&gt;         Install on a different root directory.
--instdir=&lt;directory&gt;      Change installation dir without changing admin dir.
--path-exclude=&lt;pattern&gt;   Do not install paths which match a shell pattern.
--path-include=&lt;pattern&gt;   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.
--triggers            Skip or force consequential trigger processing.
--verify-format=&lt;format&gt;   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=&lt;octal&gt;         Enable debugging (see -Dhelp or --debug=help).
--status-fd &lt;n&gt;            Send status change updates to file descriptor &lt;n&gt;.
--status-logger=&lt;command&gt;Send status change updates to &lt;command&gt;'s stdin.
--log=&lt;filename&gt;         Log status changes and actions to &lt;filename&gt;.
--ignore-depends=&lt;package&gt;,...
                           Ignore dependencies involving &lt;package&gt;.
--force-...                Override problems (see --force-help).
--no-force-...|--refuse-...
                           Stop when problems encountered.
--abort-after &lt;n&gt;          Abort after encountering &lt;n&gt; 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);
&lt; &lt;&lt; &lt;= = &gt;= &gt;&gt; &gt;       (only for compatibility with control file syntax).

Use 'apt' or 'aptitude' for user-friendly package management.
</code></pre>
<h2 id="dpkg-query">dpkg-query</h2>
<p>在软件包查询方面还有一个软件<code>dpkg-query</code>和<code>dpkg</code>有相同的功能,具体可参考:dpkg-query 命令</p>
<h2 id="参考资料">参考资料</h2>
<p>15 Practical Examples of “dpkg commands” for Debian Based Distros<br>
Linux软件安装管理之——dpkg与apt-*详解<br>
原文:https://www.jianshu.com/p/2ec0f4b945a2</p>


</div>
<div id="MySignature" role="contentinfo">
    永远年轻,永远热泪盈眶<br><br>
来源:https://www.cnblogs.com/xzongblogs/p/15535087.html
頁: [1]
查看完整版本: Linux Ubuntu dpkg 命令详解