dpkg命令是基于Debian系PMS工具的核心。包含在这个PMS中的其他工具有:

  •  apt-get
  •  apt-cache
  •  aptitude

到目前为止,最常用的命令行工具是aptitude,这是有原因的。aptitude工具本质上是apt工具
和dpkg的前端。dpkg是软件包管理系统工具,而aptitude则是完整的软件包管理系统。

命令行下使用aptitude命令有助于避免常见的软件安装问题,如软件依赖关系缺失、系统
环境不稳定及其他一些不必要的麻烦。本节将会介绍如何在命令行下使用aptitude命令工具。

9.2.1 用 aptitude 管理软件包

Linux系统管理员面对的一个常见任务是确定系统上已经安装了什么软件包。好在aptitude有
个很方便的交互式界面可以轻松完成这项任务。

如果使用的Linux发行版中已经安装了aptitude,只需要在shell提示符键入aptitude并按下回
车键就行了。紧接着就会进入aptitude的全屏模式,如图9-1所示。
image.png

可以用方向键在菜单上移动。选择菜单选项Installed Packages来查看已安装了什么软件包。
你可以看到几组软件包,比如编辑器等。每组后面的括号里都有个数字,表示这个组包含多少个
软件包。

使用方向键高亮显示一个组,按回车键来查看每个软件包分组。你会看到每个单独的软件包
名称以及它们的版本号。在软件包上按回车键可以获得更详细的信息,比如软件包的描述、主页、
大小和维护人员等。

看完了已安装软件包后,按q键来退出显示。你可以继续用方向键和回车键打开或关闭软件
包和它们所在的分组。如果想退出,多按几次q键,直到看到弹出的屏幕提示“Really quit
Aptitude?”。

如果你已经知道了系统上的那些软件包,只想快速显示某个特定包的详细信息,就没必要到
aptitude的交互式界面。可以在命令行下以单个命令的方式使用aptitude。

aptitude show package_name

下面的例子显示了包mysql-client的详情。

$ aptitude show mysql-client
Package: mysql-client
State: not installed
Version: 5.5.38-0ubuntu0.14.04.1
Priority: optional
Section: database
Maintainer: Ubuntu Developers ubuntu-devel-discuss@lists.ubuntu.com
Architecture: all
Uncompressed Size: 129 k
Depends: mysql-client-5.5
Provided by: mysql-client-5.5
Description: MySQL database client (metapackage depending on the latest version)
This is an empty package that depends on the current “best” version of
mysql-client (currently mysql-client-5.5), as determined by the MySQL
maintainers. Install this package if in doubt about which MySQL version you
want, as this is the one considered to be in the best shape by the Maintainers.
Homepage: http://dev.mysql.com/
$


说明 aptitude show命令显示上面例子中的软件包还没有安装到系统上。它输出的软件包相关的详细信息来自于软件仓库。


无法通过aptitude看到的一个细节是所有跟某个特定软件包相关的所有文件的列表。要得
到这个列表,就必须用dpkg命令。

dpkg -L package_name

下面这个例子是用dpkg列出vim-common软件包所安装的全部文件。

$
$ dpkg -L vim-common /.
/usr
/usr/bin
/usr/bin/xxd
/usr/bin/helpztags
/usr/lib
/usr/lib/mime
/usr/lib/mime/packages
/usr/lib/mime/packages/vim-common
/usr/share
/usr/share/man
/usr/share/man/ru
/usr/share/man/ru/man1
/usr/share/man/ru/man1/vim.1.gz
/usr/share/man/ru/man1/vimdiff.1.gz
/usr/share/man/ru/man1/xxd.1.gz
/usr/share/man/it
/usr/share/man/it/man1
[…]
$

同样可以进行反向操作,查找某个特定文件属于哪个软件包。

dpkg —search absolute_file_name

注意,在使用的时候必须用绝对文件路径。

$
$ dpkg —search /usr/bin/xxd
vim-common: /usr/bin/xxd
$

从输出中可以看出/usr/bin/xxd文件是作为vim-common包的一部分被安装的。

9.2.2 用 aptitude 安装软件包

了解了怎样在系统中列出软件包信息之后,本节将带你逐步学习怎样安装软件包。首先,要
确定准备安装的软件包名称。怎么才能找到特定的软件包呢?用aptitude命令加search选项。

aptitude search package_name

search选项的妙处在于你无需在package_name周围加通配符。通配符会隐式添加。下面是
用aptitude来查找wine软件包的例子。

$
$ aptitude search wine
p gnome-wine-icon-theme - red variation of the GNOME- …
v libkwineffects1-api -
p libkwineffects1a - library used by effects…
p q4wine - Qt4 GUI for wine (W.I.N.E)
p shiki-wine-theme - red variation of the Shiki- …
p wine - Microsoft Windows Compatibility …
p wine-dev - Microsoft Windows Compatibility …
p wine-gecko - Microsoft Windows Compatibility …
p wine1.0 - Microsoft Windows Compatibility … p wine1.0-dev - Microsoft Windows Compatibility …
p wine1.0-gecko - Microsoft Windows Compatibility …
p wine1.2 - Microsoft Windows Compatibility …
p wine1.2-dbg - Microsoft Windows Compatibility …
p wine1.2-dev - Microsoft Windows Compatibility …
p wine1.2-gecko - Microsoft Windows Compatibility …
p winefish - LaTeX Editor based on Bluefish
$

注意,在每个包名字之前都有一个p或i。如果看到一个i,说明这个包现在已经安装到了你
的系统上了。如果看到一个p或v,说明这个包可用,但还没安装。我们在上面的列表中可以看到
系统中尚未安装wine,但是在软件仓库中可以找到这个包。

在系统上用aptitude从软件仓库中安装软件包非常简单。

aptitude install package_name

一旦通过search选项找到了软件包名称,只要将它通过install选项插入aptitude命令。

$
$ sudo aptitude install wine
The following NEW packages will be installed:
cabextract{a} esound-clients{a} esound-common{a} gnome-exe-thumbnailer
{a}
icoutils{a} imagemagick{a} libaudio2{a} libaudiofile0{a} libcdt4{a}
libesd0{a} libgraph4{a} libgvc5{a} libilmbase6{a} libmagickcore3-extra
{a}
libmpg123-0{a} libnetpbm10{a} libopenal1{a} libopenexr6{a}
libpathplan4{a} libxdot4{a} netpbm{a} ttf-mscorefonts-installer{a}
ttf-symbol-replacement{a} winbind{a} wine wine1.2{a} wine1.2-gecko{a}
0 packages upgraded, 27 newly installed, 0 to remove and 0 not upgraded.
Need to get 0B/27.6MB of archives. After unpacking 121MB will be used.
Do you want to continue? [Y/n/?] Y
Preconfiguring packages …
[…]
All done, no errors.
All fonts downloaded and installed.
Updating fontconfig cache for /usr/share/fonts/truetype/msttcorefonts
Setting up winbind (2:3.5.4~dfsg-1ubuntu7) …

  • Starting the Winbind daemon winbind
    [ OK ]
    Setting up wine (1.2-0ubuntu5) …
    Setting up gnome-exe-thumbnailer (0.6-0ubuntu1) …
    Processing triggers for libc-bin …
    ldconfig deferred processing now taking place

$


说明 在上面的例子中,在aptitude命令之前出现了sudo命令。sudo命令允许你以root用户身
份运行一个命令。可以用sudo命令进行管理任务,比如安装软件。


要检查安装过程是否正常,只要再次使用search选项就可以了。这次你应该可以看到在wine
软件包出现了i u ,这说明它已经安装好了。

你可能还会注意到这里的另外一些包前面也有i u 。这是因为aptitude自动解析了必要的
包依赖关系,并安装了需要的额外的库和软件包。这是许多包管理系统都有的非常好的功能。

9.2.3 用 aptitude 更新软件

尽管aptitude可以帮忙解决安装软件时遇到的问题,但解决有依赖关系的多个包的更新会
比较烦琐。要用软件仓库中的新版本妥善地更新系统上所有的软件包,可用safe-upgrade选项。

aptitude safe-upgrade

注意,这个命令不需要使用软件包名称作为参数。因为safe-upgrade选项会将所有已安装
的包更新到软件仓库中的最新版本,更有利于系统稳定。
这里是aptitude safe-upgrade命令的输出示例。
$

$ sudo aptitude safe-upgrade
The following packages will be upgraded:
evolution evolution-common evolution-plugins gsfonts libevolution
xserver-xorg-video-geode
6 packages upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 9,312kB of archives. After unpacking 0B will be used.
Do you want to continue? [Y/n/?] Y
Get:1 http://us.archive.ubuntu.com/ubuntu/ maverick/main
libevolution i386 2.30.3-1ubuntu4 [2,096kB]
[…]
Preparing to replace xserver-xorg-video-geode 2.11.9-2
(using …/xserver-xorg-video-geode_2.11.9-3_i386.deb) …
Unpacking replacement xserver-xorg-video-geode …
Processing triggers for man-db …
Processing triggers for desktop-file-utils …
Processing triggers for python-gmenu …
[…]
Current status: 0 updates [-6].
$

还有一些不那么保守的软件升级选项:

  •  aptitude full-upgrade
  •  aptitude dist-upgrade

这些选项执行相同的任务,将所有软件包升级到最新版本。它们同safe-upgrade的区别在
于,它们不会检查包与包之间的依赖关系。整个包依赖关系问题非常麻烦。如果不是很确定各种
包的依赖关系,那还是坚持用safe-upgrade选项吧。


说明 显然,应该定期运行aptitude的safe-upgrade选项来保持系统处于最新状态。这点在
安装了一个全新的发行版之后尤其重要。通常在发行版推出最新的完整发布之后,就会
跟着出现很多新的安全补丁和更新。


9.2.4 用 aptitude 卸载软件

用aptitude卸载软件包与安装及更新它们一样容易。你要作出的唯一选择就是要不要保留
软件数据和配置文件。

要想只删除软件包而不删除数据和配置文件,可以使用aptitude的remove选项。要删除软
件包和相关的数据和配置文件,可用purge选项。

$ sudo aptitude purge wine
[sudo] password for user:
The following packages will be REMOVED:
cabextract{u} esound-clients{u} esound-common{u} gnome-exe-thumbnailer
{u}
icoutils{u} imagemagick{u} libaudio2{u} libaudiofile0{u} libcdt4{u}
libesd0{u} libgraph4{u} libgvc5{u} libilmbase6{u} libmagickcore3-extra
{u}
libmpg123-0{u} libnetpbm10{u} libopenal1{u} libopenexr6{u}
libpathplan4{u} libxdot4{u} netpbm{u} ttf-mscorefonts-installer{u}
ttf-symbol-replacement{u} winbind{u} wine{p} wine1.2{u} wine1.2-gecko
{u}
0 packages upgraded, 0 newly installed, 27 to remove and 6 not upgraded.
Need to get 0B of archives. After unpacking 121MB will be freed.
Do you want to continue? [Y/n/?] Y
(Reading database … 120968 files and directories currently installed.)
Removing ttf-mscorefonts-installer …
[…]
Processing triggers for fontconfig …
Processing triggers for ureadahead …
Processing triggers for python-support …
$

要看软件包是否已删除,可以再用aptitude的search选项。如果在软件包名称的前面看到
一个c,意味着软件已删除,但配置文件尚未从系统中清除;如果前面是个p的话,说明配置文件
也已删除。

9.2.5 aptitude 仓库

aptitude默认的软件仓库位置是在安装Linux发行版时设置的。具体位置存储在文件
/etc/apt/sources. list中。

很多情况下,根本不需要添加或删除软件仓库,所以也没必要接触这个文件。但aptitude
只会从这些仓库中下载文件。另外,在搜索软件进行安装或更新时,aptitude同样只会检查这
些库。如果需要为你的PMS添加一些额外的软件仓库,就在这个文件中设置吧。


窍门 Linux发行版的开发人员下了大工夫,以保证添加到软件仓库的包版本不会互相冲突。通
常通过库来升级或安装软件包是最安全的。即使在其他地方有更新的版本,也应该等到
该版本出现在你的Linux发行版仓库中的时候再安装。


下面是Ubuntu系统中sources.list文件的例子。

$ cat /etc/apt/sources.list

deb cdrom:[Ubuntu 14.04 LTS Trusty Tahr - Release i386 (20140417)]/

trusty main restricted

See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to

newer versions of the distribution.

deb http://us.archive.ubuntu.com/ubuntu/ trusty main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty main restricted

Major bug fix updates produced after the final release of the

distribution.

deb http://us.archive.ubuntu.com/ubuntu/ trusty-updates main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty-updates main restricted

N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu

team. Also, please note that software in universe WILL NOT receive any

review or updates from the Ubuntu security team.

deb http://us.archive.ubuntu.com/ubuntu/ trusty universe
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty universe
deb http://us.archive.ubuntu.com/ubuntu/ trusty-updates universe
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty-updates universe
[…]

Uncomment the following two lines to add software from Canonical’s

‘partner’ repository.

This software is not part of Ubuntu, but is offered by Canonical and the

respective vendors as a service to Ubuntu users.

deb http://archive.canonical.com/ubuntu trusty partner

deb-src http://archive.canonical.com/ubuntu trusty partner

This software is not part of Ubuntu, but is offered by third-party

developers who want to ship their latest software.

deb http://extras.ubuntu.com/ubuntu trusty main
deb-src http://extras.ubuntu.com/ubuntu trusty main
$

首先,我们注意到文件里满是帮助性的注释和警告。使用下面的结构来指定仓库源。

deb (or deb-src) address distribution_name package_type_list

deb或deb-src的值表明了软件包的类型。deb值说明这是一个已编译程序源,而deb-src
值则说明这是一个源代码的源。

address条目是软件仓库的Web地址。distribution_name条目是这个特定软件仓库的发
行版版本的名称。在这个例子中,发行版名称是trusty。这未必就是说你使用的发行版就是Ubuntu
Trusty Tahr,它只是说明这个Linux发行版正在用Ubuntu Trusty Tahr软件仓库!举个例子,在Linux
Mint的sources.list文件中,你能看到混用了Linux Mint和Ubuntu的软件仓库。

最后,package_type_list条目可能并不止一个词,它还表明仓库里面有什么类型的包。
你可以看到诸如main、restricted、universe和partner这样的值。

当需要给你的source_list文件添加软件仓库时,你可以自己发挥,但一般会带来问题。通常
软件仓库网站或各种包开发人员网站上都会有一行文本,你可以直接复制,然后粘贴到sources.list
文件中。最好选择较安全的途径并且只复制/粘贴。

aptitude前端界面提供了智能命令行选项来配合基于Debian的dpkg工具。现在是时候了解
基于Red Hat的发行版的rpm工具和它的各种前端界面了。