Linux dpkg 命令初学者教程(8 个示例)
在此页
- Linux dpkg 命令
- Q1。如何使用 dpkg 安装软件包?
- Q2。如何使用 dpkg 删除已安装的包?
- Q3。如何列出系统中所有已安装的软件包?
- Q4。如何使 dpkg 列出包的内容?
- Q5。如何使用 dpkg 解压一个包?
- Q6。如何检查软件包是否安装?
- Q7。如何打印 dpkg 安装包的体系结构?
- Q8。如何使用 dpkg 清除包?
- 结论
如果您使用的是 Debian 或基于 Debian 的系统(如 Ubuntu),您很可能遇到过 .deb 包。这些是 Debian 软件包,Linux 命令行提供了内置命令/工具来处理此类软件包。一个这样的工具是 dpkg,我们将在本教程中讨论它。
但在此之前,值得一提的是,本教程中的所有示例都已在 Ubuntu 16.04LTS 机器上进行了测试。
Linux dpkg 命令
dpkg 工具基本上是 Debian/基于 Debian 的系统的包管理器。以下是它的语法:
dpkg ACTIONS
或者
dpkg [options] filename
手册页是这样解释的:
dpkg is a tool to install, build, remove and manage Debian packages.
The primary and more user-friendly front-end for dpkg is aptitude(1).
dpkg itself is controlled entirely via command line parameters, which
consist of exactly one action and zero or more options. The action-
parameter tells dpkg what to do and options control the behavior of the
action in some way.
dpkg can also be used as a front-end to dpkg-deb(1) and dpkg-query(1).
The list of supported actions can be found later on in the ACTIONS sec?
tion. If any such action is encountered dpkg just runs dpkg-deb or
dpkg-query with the parameters given to it, but no specific options are
currently passed to them, to use any such option the back-ends need to
be called directly.
以下是一些 Q&A 风格的示例,应该可以让您对 dpkg 的工作原理有一个很好的基本了解。
Q1。如何使用 dpkg 安装软件包?
您可以使用 -i 命令行选项执行此操作。
dpkg -i [package-name]
例如:
dpkg -i google-chrome-stable_current_amd64.deb
以下是安装过程中涉及的所有步骤:
1. Extract the control files of the new package.
2. If another version of the same package was installed before
the new installation, execute prerm script of the old package.
3. Run preinst script, if provided by the package.
4. Unpack the new files, and at the same time back up the old
files, so that if something goes wrong, they can be restored.
5. If another version of the same package was installed before
the new installation, execute the postrm script of the old pack?
age. Note that this script is executed after the preinst script
of the new package, because new files are written at the same
time old files are removed.
6. Configure the package. See --configure for detailed informa?
tion about how this is done.
Q2。如何使用 dpkg 删除已安装的包?
这可以使用 -r 命令行选项来完成。
dpkg -r [package-name]
例如:
dpkg -r googler_3.3.0-1_all.deb
以下是手册页关于此选项的内容:
Removing of a package consists of the following steps:
1. Run prerm script
2. Remove the installed files
3. Run postrm script
Q3.如何列出系统中所有已安装的软件包?
为此,您可以使用 -l 命令行选项。
dpkg -l
例如,这是在我的系统上生成的此命令行选项的输出:

Q4.如何使 dpkg 列出包的内容?
这可以使用 --contents 标志来完成。
dpkg --contents [package name]
例如:

Q5.如何使用 dpkg 解压一个包?
有时您可能只想解压包,而不是配置它。那么,dpkg 也为此提供了一个选项:--unpack。
dpkg --unpack [package-name]
如果稍后你想配置一个已经解压的包,你可以使用 --configure 命令行选项来完成。
dpkg --configure [package-name]
以下是手册页关于此选项的内容:
Configuring consists of the following steps:
1. Unpack the conffiles, and at the same time back up the old
conffiles, so that they can be restored if something goes wrong.
2. Run postinst script, if provided by the package.
Q6.如何检查软件包是否安装?
为此使用 -s 命令行选项。
dpkg -s [package-name]
例如:

Q7.如何打印 dpkg 安装包的体系结构?
可以使用 --print-architecture 命令行选项访问此信息。
dpkg --print-architecture
例如,上述命令在我的系统上产生的输出是:
amd64
Q8.如何使用 dpkg 清除包?
我们已经讨论了如何使用 dpkg 命令删除包。您还可以清除一个包,一个删除所有内容的过程,包括配置文件。这可以使用 -P 命令行选项来完成。
dpkg -P [package-name]
以下是手册页中关于此选项的说明:
Some configuration files might be unknown to dpkg because
they are created and handled separately through the configura?
tion scripts. In that case, dpkg won't remove them by itself,
but the package's postrm script (which is called by dpkg), has
to take care of their removal during purge. Of course, this only
applies to files in system directories, not configuration files
written to individual users' home directories.
Purging of a package consists of the following steps:
1. Remove the package, if not already removed. See --remove for
detailed information about how this is done.
2. Run postrm script.
结论
dpkg 命令提供了大量的选项。我们在这里讨论的是那些将帮助您开始使用该工具的选项。完成这些练习后,请前往命令手册页了解更多信息。