- CompTIA Linux+ Certification Guide
- Philip Inshanally
- 1380字
- 2021-08-13 15:49:54
The dpkg command
Firstly, the dpkg utility is a low-level system tool to extract, analyze, unpack, install, and remove packages with a .deb extension. The scripts read by dpkg inside each .deb file are important as they give information to the program regarding the packages' installation, removal, and configuration. The dpkg utility resides at the base of the package management system in Debian-based distributions. The Debian package, dpkg, provides the dpkg utility, as well as several other programs necessary for runtime functioning of the packaging system; namely: dpkg-deb, dpkg-split, dpkg-query, dpkg-statoverride, dpkg-divert, and dpkg-trigger. We can take a glance at the /var/log/dpkg.log file. There are a lot of verbose details about triggers and packages going through varying stages of unpacking and configuration.
Let's take a look at /var/log/dpkg.log:
philip@ubuntu:~$ cat /var/log/dpkg.log
2018-07-02 06:43:57 startup archives unpack
2018-07-02 06:44:01 install linux-image-4.4.0-130-generic:amd64 <none> 4.4.0-130.156
2018-07-02 06:44:01 status half-installed linux-image-4.4.0-130-generic:amd64 4.4.0-130.156
2018-07-02 06:44:09 status unpacked linux-image-4.4.0-130-generic:amd64 4.4.0-130.156
2018-07-02 06:44:09 status unpacked linux-image-4.4.0-130-generic:amd64 4.4.0-130.156
2018-07-02 06:44:09 install linux-image-extra-4.4.0-130-generic:amd64 <none> 4.4.0-130.156
2018-07-02 06:44:09 status half-installed linux-image-extra-4.4.0-130-generic:amd64 4.4.0-130.156
2018-07-02 06:44:20 status unpacked linux-image-extra-4.4.0-130-generic:amd64 4.4.0-130.156
2018-07-02 06:44:20 status unpacked linux-image-extra-4.4.0-130-generic:amd64 4.4.0-130.156
2018-07-02 06:44:21 upgrade linux-generic:amd64 4.4.0.128.134 4.4.0.130.136
2018-07-02 06:44:21 status half-configured linux-generic:amd64 4.4.0.128.134
2018-07-02 06:44:21 status unpacked linux-generic:amd64 4.4.0.128.134
2018-07-02 06:44:21 status half-installed linux-generic:amd64 4.4.0.128.134
2018-07-02 06:44:21 status half-installed linux-generic:amd64 4.4.0.128.134
2018-07-02 06:44:21 status unpacked linux-generic:amd64 4.4.0.130.136
2018-07-02 06:44:21 status unpacked linux-generic:amd64 4.4.0.130.136
2018-07-02 06:44:21 upgrade linux-image-generic:amd64 4.4.0.128.134 4.4.0.130.136
2018-07-02 06:44:21 status half-configured linux-image-generic:amd64 4.4.0.128.134
2018-07-02 06:44:21 status unpacked linux-image-generic:amd64 4.4.0.128.134
2018-07-02 06:44:21 status half-installed linux-image-generic:amd64 4.4.0.128.134
From the preceding output, we have learned about the various packages that the dpkg utility is managing. If we want to see a list of packages on this system, we can use the l option:
philip@ubuntu:~$ dpkg -l
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 a11y-profile-manager-indicato 0.1.10-0ubuntu3 amd64 Accessibility Profile Manager - Unity desktop indicator
ii account-plugin-facebook 0.12+16.04.20160126 all GNOME Control Center account plugin for single signon - facebook
ii account-plugin-flickr 0.12+16.04.20160126 all GNOME Control Center account plugin for single signon - flickr
ii account-plugin-google 0.12+16.04.20160126 all GNOME Control Center account plugin for single signon
ii accountsservice 0.6.40-2ubuntu11.3 amd64 query and manipulate user account information
ii activity-log-manager 0.9.7-0ubuntu23.16. amd64 blacklist configuration user interface for Zeitgeist
ii adduser 3.113+nmu3ubuntu4 all add and remove users and groups
ii adium-theme-ubuntu 0.3.4-0ubuntu1.1 all Adium message style for Ubuntu
ii app-install-data 15.10 all Ubuntu applications (data files)
ii app-install-data-partner 16.04 all Application Installer (data files for partner applications/repos
ii apparmor 2.10.95-0ubuntu2.9 amd64 user-space parser utility for AppArmor
ii appmenu-qt:amd64 0.2.7+14.04.2014030 amd64 application menu for Qt
ii appmenu-qt5 0.3.0+16.04.2017021 amd64 application menu for Qt5
ii apport 2.20.1-0ubuntu2.18 all automatically generate crash reports for debugging
ii apport-gtk 2.20.1-0ubuntu2.18 all GTK+ frontend for the apport crash report system
In the preceding output, we read this from left to right. We should now focus our attention on the far right of the output. This is the description section; the packages are presented in a human-readable summary for each package that is currently installed onto the system.
We can also narrow down our output by filtering the dpkg command; let's look for the xterm program:
philip@ubuntu:~$ dpkg -l xterm
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 xterm 322-1ubuntu1 amd64 X terminal emulator
philip@ubuntu:~$
We can verify whether or not a package is installed with --get-selections:
philip@ubuntu:~$ dpkg --get-selections
a11y-profile-manager-indicator install
account-plugin-facebook install
account-plugin-flickr install
account-plugin-google install
accountsservice install
acl install
acpi-support install
acpid install
activity-log-manager install
adduser install
adium-theme-ubuntu install
adwaita-icon-theme install
aisleriot install
alsa-base install
alsa-utils install
amd64-microcode install
anacron install
apg install
app-install-data install
app-install-data-partner install
apparmor install
appmenu-qt:amd64 install
appmenu-qt5 install
apport install
We can view the locations in which a package is installed by using the L option. Let's continue with our example:
philip@ubuntu:~$ dpkg -L xterm
/.
/etc
/etc/X11
/etc/X11/app-defaults
/etc/X11/app-defaults/UXTerm-color
/etc/X11/app-defaults/UXTerm
/etc/X11/app-defaults/KOI8RXTerm-color
/etc/X11/app-defaults/KOI8RXTerm
/etc/X11/app-defaults/XTerm-color
/usr/share/man/man1/koi8rxterm.1.gz
/usr/share/man/man1/resize.1.gz
/usr/share/man/man1/xterm.1.gz
/usr/share/man/man1/lxterm.1.gz
philip@ubuntu:~$
We can search for a particular package in this system by using the s option:
philip@ubuntu:~$ dpkg -s apache
dpkg-query: package 'apache' is not installed and no information is available
Use dpkg --info (= dpkg-deb --info) to examine archive files,
and dpkg --contents (= dpkg-deb --contents) to list their contents.
philip@ubuntu:~$ dpkg --info apache
dpkg-deb: error: failed to read archive 'apache': No such file or directory
philip@ubuntu:~$
In this case, Apache is not installed by default on this system.
I've downloaded a tftp client for this demonstration. Let's verify that the tftp client isn't installed on this system:
philip@ubuntu:~/Downloads$ dpkg -l tftp
dpkg-query: no packages found matching tftp
philip@ubuntu:~/Downloads$
Now we'll install a package using the dpkg command. Let's try to install the tftp client package using the i option:
philip@ubuntu:~/Downloads$ dpkg -i tftp_0.17-18_i386.deb
dpkg: error: requested operation requires superuser privilege
philip@ubuntu:~/Downloads$
From the preceding output, you can see that we would need root privileges to install or remove a package. Let's retry as root:
root@ubuntu:/home/philip/Downloads# ls -l | grep tftp
-rw-rw-r-- 1 philip philip 17208 Jul 18 08:15 tftp_0.17-18_i386.deb
root@ubuntu:/home/philip/Downloads#
root@ubuntu:/home/philip/Downloads# dpkg -i tftp_0.17-18_i386.deb
Selecting previously unselected package tftp:i386.
(Reading database ... 241431 files and directories currently installed.)
Preparing to unpack tftp_0.17-18_i386.deb ...
Unpacking tftp:i386 (0.17-18) ...
Setting up tftp:i386 (0.17-18) ...
Processing triggers for man-db (2.7.5-1) ...
root@ubuntu:/home/philip/Downloads#
Great! Now, let's retry the dpkg command with the l option:
root@ubuntu:/home/philip/Downloads# dpkg -l tftp
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 tftp:i386 0.17-18 i386 Trivial file transfer protocol client
root@ubuntu:/home/philip/Downloads#
Awesome! We can now see that our tftp client is listed. We can also run the dpkg with the --get-selections to verify the following:
root@ubuntu:/home/philip/Downloads# dpkg --get-selections | grep tftp
tftp:i386 install
root@ubuntu:/home/philip/Downloads#
We can also remove a package using the dpkg command. Let's remove the tftp package which we installed in the previous example. We will use the -r option for this:
root@ubuntu:/home/philip/Downloads# dpkg -r tftp
(Reading database ... 241438 files and directories currently installed.)
Removing tftp:i386 (0.17-18) ...
Processing triggers for man-db (2.7.5-1) ...
root@ubuntu:/home/philip/Downloads#
Now, let's verify that the tftp package has indeed been uninstalled:
root@ubuntu:/home/philip/Downloads# dpkg -l tftp
dpkg-query: no packages found matching tftp
root@ubuntu:/home/philip/Downloads#
Great! However, when we use the -r option, it does not remove the configuration file(s). In order to remove the package, along with the configuration files, we should use the -P (purge) option. Here is how that works:
root@ubuntu:/home/philip/Downloads# dpkg -P tftp
(Reading database ... 241438 files and directories currently installed.)
Removing tftp:i386 (0.17-18) ...
Processing triggers for man-db (2.7.5-1) ...
root@ubuntu:/home/philip/Downloads#
We can also extract the content of a package without installing it. We should use the -x option for this:
root@ubuntu:/home/philip/Downloads# dpkg -x tftp_0.17-18_i386.deb ./tftp_0.17-18_i386
root@ubuntu:/home/philip/Downloads# ls
root@ubuntu:/home/philip/Downloads# ls tftp_0.17-18_i386
usr
root@ubuntu:/home/philip/Downloads# ls tftp_0.17-18_i386/usr/
bin share
root@ubuntu:/home/philip/Downloads#
root@ubuntu:/home/philip/Downloads# ls tftp_0.17-18_i386/usr/bin/
tftp
root@ubuntu:/home/philip/Downloads# ls tftp_0.17-18_i386/usr/share/
doc/ man/
root@ubuntu:/home/philip/Downloads# ls tftp_0.17-18_i386/usr/share/
doc man
root@ubuntu:/home/philip/Downloads#
Before we can download any packages and install them using the dpkg utility, we need to know the correct hardware architecture of the system. Fortunately, we can use the dpkg-architecture command:
root@ubuntu:/home/philip/Downloads# dpkg-architecture
DEB_BUILD_ARCH=amd64
DEB_BUILD_ARCH_BITS=64
DEB_BUILD_ARCH_CPU=amd64
DEB_BUILD_ARCH_ENDIAN=little
DEB_BUILD_ARCH_OS=linux
DEB_BUILD_GNU_CPU=x86_64
DEB_BUILD_GNU_SYSTEM=linux-gnu
DEB_BUILD_GNU_TYPE=x86_64-linux-gnu
DEB_TARGET_ARCH_CPU=amd64
DEB_TARGET_ARCH_ENDIAN=little
DEB_TARGET_ARCH_OS=linux
DEB_TARGET_GNU_CPU=x86_64
DEB_TARGET_GNU_SYSTEM=linux-gnu
DEB_TARGET_GNU_TYPE=x86_64-linux-gnu
DEB_TARGET_MULTIARCH=x86_64-linux-gnu
root@ubuntu:/home/philip/Downloads#
Based on the preceding output, we can see that this system supports either 32-bit or 64-bit packages. We can also garner useful information regarding the purpose of a package. We need to use the dpkg-query command with the -s option here:
root@ubuntu:/home/philip/Downloads# dpkg-query -s tftp
Package: tftp
Status: install ok unpacked
Priority: optional
Section: net
Installed-Size: 80
Maintainer: Alberto Gonzalez Iniesta <agi@inittab.org>
Architecture: i386
Source: netkit-tftp
Version: 0.17-18
Config-Version: 0.17-18
Replaces: netstd
Depends: netbase, libc6 (>= 2.3)
Description: Trivial file transfer protocol client
Tftp is the user interface to the Internet TFTP (Trivial File Transfer
Protocol), which allows users to transfer files to and from a remote machine.
The remote host may be specified on the command line, in which case tftp uses
host as the default host for future transfers.
root@ubuntu:/home/philip/Downloads#
From the preceding output, we are given a description at the bottom regarding the use of the tftp package.