前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ubuntu 系统 dpkg 命令使用详解

Ubuntu 系统 dpkg 命令使用详解

作者头像
用户5807183
发布2019-08-12 14:59:36
37.8K1
发布2019-08-12 14:59:36
举报
文章被收录于专栏:Linux知识积累Linux知识积累

dpkg 即 package manager for Debian ,是 Debian 和基于 Debian 的系统中一个主要的包管理工具,可以用来安装、构建、卸载、管理 deb 格式的软件包。

安装软件

使用 dpkg 命令安装软件时,可以使用 -i 选项并指定 deb 安装包的路径。和 Ubuntu 下的另一个包管理工具 apt-get(Advanced Package Tool)有所不同。 apt-get 命令并不直接操作 deb 安装包文件,而是从 /etc/apt/sources.list 配置文件中定义的软件镜像源里下载软件包并安装,使用时也只需指定软件的名称(或者也可以附加上版本号)。

apt-get 命令安装软件: $ apt-get install

dpkg 命令安装软件: $ dpkg -i

因此,dpkg 主要是用来安装已经下载到本地的 deb 软件包,或者对已经安装好的软件进行管理。而 apt-get 可以直接从远程的软件仓库里下载安装软件。

$ sudo apt-get install emacs
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
将会同时安装下列软件:emacs-bin-common emacs-common emacs-el emacs-gtk libm17n-0
  libotf0 m17n-db
建议安装:mailutils emacs-common-non-dfsg m17n-docs gawk
下列【新】软件包将被安装:emacs emacs-bin-common emacs-common emacs-el emacs-gtk libm17n-0
  libotf0 m17n-db
升级了 0 个软件包,新安装了 8 个软件包,要卸载 0 个软件包,有 115 个软件包未被升级。需要下载 34.4 
$ 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) 的触发器 ...

列出已安装的软件

可以使用 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 命令筛选出名称中包含指定模式的软件包。

$ 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 选项只会移除指定的软件包而不对其配置文件产生影响,
其他包管理操作
查看软件包的内容
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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-08-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Linux知识积累 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装软件
  • 其他包管理操作
    • 查看软件包的内容
      • 查看软件包的安装位置
        • 筛选出包含指定文件(模式)的软件包
        相关产品与服务
        云数据库 MySQL
        腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档