Product SiteDocumentation Site

6.2. aptitudeapt-getapt 命令

APT 是个原先有图形接口的大计划。以包括核心应用程序的程序库为基础,且第一个前端 apt-get — 基于命令行 — 也包括在项目开发中。apt 是 APT 的第二个基于命令行的前端,包括若干 apt-get 错误的处理。
Both tools are built on top of the same library and are thus very close, but the default behavior of apt has been improved for interactive use and to actually do what most users expect. The APT developers reserve the right to change the public interface of this tool to further improve it. Conversely, the public interface of apt-get is well defined and will not change in any backwards incompatible way. It is thus the tool that you want to use when you need to script package installation requests.
其他众多的图形界面以外部项目形式出现:synapticaptitude(它同时包含文本模式界面和图形界面 — 即使尚未完成)、wajig等等。最为推荐的界面,apt,会在本节示例中使用。注意,尽管 apt-getaptitude 的命令行语法也非常类似。在三个命令存在较大差异时,将会详述差异。

6.2.1. 初始化

For any work with APT, the list of available packages needs to be updated; this can be done simply through apt update. Depending on the speed of your connection and configuration, the operation can take a while, since it involves downloading a certain number of (usually compressed) files (Packages, Sources, Translation-language-code), which have gradually become bigger and bigger as Debian has developed (at least 10-16 MB of data for the main section). Of course, installing from a CD-ROM/DVD set does not require any downloading — in this case, the operation is very fast.

6.2.2. 安装和卸载

通过APT,软件包可以从系统中添加或移除,命令分别为apt install软件包apt remove软件包。在以上两种情况中,APT 将会自动安装必要的依赖包或者删除依赖于删除包的软件包。apt purge软件包命令包含一个彻底删除 — 相关的配置文件也会被删除。
如果文件 sources.list 包含了数个发布版本,可以指定要安装软件包的版本。可以通过 apt install package=version命令要求特定版本号,但以标明其初始发行版本 (Stable, Testing 或者 Unstable)的方式— 通过 apt install package/distribution — 更为推荐。倘若 sources.list文件所述软件源之一仍可用的情况下,通过此命令可以回溯一个软件包的旧版本 (比如您知晓其情况良好的情况)。否则,snapshot.debian.org 归档可以补救(参阅侧边栏 GOING FURTHER Old package versions: snapshot.debian.org and archive.debian.org)。

例 6.4. 安装 spamassassinUnstable 版本

#  apt install spamassassin/unstable
如果要安装没有关联存储库的简单 .deb 文件形式的软件包,仍然可以使用APT将其依赖项一起安装(前提是依赖项在配置的存储库中可用),使用简单命令: apt install ./path-to-the-package.deb。前缀 ./ 必须明确,表示要安装的是一个文件名,而不是其中一个存储库中可用的软件包名称。

6.2.3. 系统升级

常规升级是推荐的,因为它们包含最新的安全更新。要升级,可使用apt upgradeapt-get upgradeaptitude safe-upgrade(当然需在apt update之后)。该命令会查找要升级的已安装包,同时不移除任何包。也就是说,目标是确保可能的侵入式升级最小化。apt-get命令略苛刻于 aptitudeapt命令,因为它会拒绝安装未预先安装的软件包。
apt 会选择最新的版本号 (除了来自 Experimentalstable-backports,其缺省忽略版本号)。若在 sources.list 内指定 测试版不稳定版 ,则 apt upgrade 会把 稳定版 系统切换至 测试版不稳定版,这些可能不是您要的。
搜索升级的软件包时需把特定的发行版告知 apt,采用 -t--target-release 选项,然后是发行版的名称 (如:apt -t stable upgrade)。为了避免每次使用 apt 都需指定版本,可以把 APT::Default-Release "stable"; 加在 /etc/apt/apt.conf.d/local 内。
对于非常重要的升级,如从一个 Debian 主版本变更为下一版本,你需要使用apt full-upgrade。使用此命令,apt将会完全升级,即使必须移除一些废弃包或者安装新的依赖包。这也适用于日常使用 Debian Unstable并每天跟随其演化的用户。它是如此简便以至于几乎无需解释:APT的声誉基于此项强大的功能。
不同于 aptaptitudeapt-get 不能用 full-upgrade 命令。反而,您应使用 apt-get dist-upgrade (“升级发行版”),此古老且知名的命令也被 aptaptitude 接受,相当的便利。
这些操作的结果记录到 /var/log/apt/history.log/var/log/apt/term.log,而 dpkg 的日志记录到文件 /var/log/dpkg.log

6.2.4. 配置选项

除了已提及的配置项目,还可在 /etc/apt/apt.conf.d/ 文件夹内增加 APT 的其他配置文件或编辑 /etc/apt/apt.conf 本身。让 APT 告知 dpkg 忽略由 DPkg::options { "--force-overwrite"; } 引发的错误。
If the Web can only be accessed through a proxy, add a line like Acquire::http::proxy "http://yourproxy:3128". For an FTP proxy, write Acquire::ftp::proxy "ftp://yourproxy". To discover more configuration options, read the apt.conf(5) manual page (for details on manual pages, see 第 7.1.1 节 “手册页面”).

6.2.5. 软件包优先级管理

配置 APT 最重要的层面之一是管理每个软件包来源的优先性。例如,从 测试版不稳定实验版 中选取一个或多个软件包。可以指定每个软件包的优先性 (视其版本或发行版,同个软件包可以有多个优先性)。这些优先性将影响 APT 的行为:每个软件包总是选择最优先的版本 (除非旧于已安装的版本或优先性小于 1000)。
APT 设置若干缺省的优先性。已安装软件包版本的优先性是 100。未安装版本缺省优先性为 500,若是另个发布的目标则可跳至 990 (以 -t 命令行选项或 APT::Default-Release 配置指令调整)。
可以在 /etc/apt/preferences.d/ 内添加包含条目的文件或向 /etc/apt/preferences 文件添加条目,指名受影响软件包名称、版本、出处及其新的优先性。
APT will never install an older version of a package (that is, a package whose version number is lower than the one of the currently installed package) except if its priority is higher than 1000 (or it is explicitly requested by the user, see 第 6.2.2 节 “安装和卸载”). APT will always install the highest priority package which follows this constraint. If two packages have the same priority, APT installs the newest one (whose version number is the highest). If two packages of same version have the same priority but differ in their content, APT installs the version that is not installed (this rule has been created to cover the case of a package update without the increment of the revision number, which is usually required).
更具体地说,一个软件包的优先性是
< 0
将永远不会被安装,
1..99
则只会在没有安装其他版本时才安装,
100..499
只有在没有安装其他较新版本或在其他发行版中可用的版本时才会安装,
500....989
只有在目标分发中没有较新或可用版本时才安装,
990..1000
除非已安装较新版本,否则都会安装,
> 1000
强制安装,即使APT会降级到较旧的版本。
When APT checks /etc/apt/preferences and /etc/apt/preferences.d/, it first takes into account the most specific entries (often those specifying the concerned package), then the more generic ones (including, for example, all the packages of a distribution). If several generic entries exist, the first match is used. The available selection criteria include the package's name and the source providing it. Every package source is identified by the information contained in a Release file that APT downloads together with the Packages files. It specifies the origin (usually “Debian” for the packages of official mirrors, but it can also be a person's or an organization's name for third-party repositories). It also gives the name of the distribution (usually Stable, Testing, Unstable or Experimental for the standard distributions provided by Debian) together with its version (for example, 11 for Debian Bullseye). Let's have a look at its syntax through some realistic case studies of this mechanism.
暂时假设您只需用到 Debian 稳定版的软件包。除非特别指明,不会安装其他版本的软件包。可以在 /etc/apt/preferences 文件内写入以下条目:
Package: *
Pin: release a=stable
Pin-Priority: 900

Package: *
Pin: release o=Debian
Pin-Priority: -10
a=stable 设置发行版的名称。o=Debian 限制来自 “Debian” 的软件包。
Let's now assume that you have a server with several local programs depending on the version 5.28 of Perl and that you want to ensure that upgrades will not install another version of it. You could use this entry:
Package: perl
Pin: version 5.28*
Pin-Priority: 1001
To gain a better understanding of the mechanisms of priority and distribution or repository properties to pin do not hesitate to execute apt-cache policy to display the default priority associated with each package source, or apt-cache policy package to display the default priority for each available version and source of a package as explained in 第 6.3.1 节 “The apt-cache policy Command”.
文件 /etc/apt/preferences/etc/apt/preferences.d/ 的参考文档在手册的 apt_preferences(5),使用 man apt_preferences 命令就可看到。

6.2.6. 在多个发行版中工作

apt 是很奇妙的工具,可从其他发行版提取软件包。例如,安装 稳定版 系统之后,或许想要在不偏离系统原来状态下,试试 测试版不稳定版 的软件包。
混用不同版本软件包出问题时,即使您偶尔会在混用不同发行版的包时遇到问题,apt 也可以处理得极为恰当把风险降到最低。最好的方法是把所有的发行版置于 /etc/apt/sources.list 文件内 (某些人总是置入三个发行版,不过还是要记得 不稳定版 是有经验用户的专属) 且在 APT::Default-Release 参数中设置偏好的发行版 (见 第 6.2.3 节 “系统升级”) 。
假设 稳定版 是您的参考版本,但是 测试版不稳定版 也列在 sources.list 文件内。在这个情况下,您可使用 apt install 软件包名称/testing 安装来自 测试版 的软件包。若因相依性未满足而安装失败,可以在 测试版 内加入 -t testing 参数。同样的方式也适用于 不稳定版
In this situation, upgrades (upgrade and full-upgrade) are done within Stable except for packages already upgraded to another distribution: those will follow updates available in the other distributions. We will explain this behavior with the help of the default priorities set by APT below. Do not hesitate to use apt-cache policy (第 6.3.1 节 “The apt-cache policy Command”) to verify the given priorities.
每件事都围绕在 APT 只处理高于或等于已安装版本号软件包的前提下 (假设 /etc/apt/preferences 还未强迫优先性高于 1000 的某些软件包)。
假设从L 稳定版 安装第一个软件包的第一版且从 测试版不稳定版 安装同软件包的第二版与第三版。已安装的版本优先性为 100 但在 稳定版 (相同的) 优先性为 990 (因为它是目标发布版的一部分)。在 测试版不稳定版 的软件包其优先性为 500 (缺省不安装版本的优先性)。获选者为优先性 990 的版本1。软件包 “留在 稳定版 内”。
Let's take the example of another package whose version 2 has been installed from Testing. Version 1 is available in Stable and version 3 in Unstable. Version 1 (of priority 990 — thus lower than 1000) is discarded because it is lower than the installed version. This only leaves version 2 and 3, both of priority 500. Faced with this alternative, APT selects the newest version, the one from Unstable. If you don't want a package installed from Testing to migrate to the version in Unstable, you have to assign a priority lower than 500 (490 for example) to packages coming from Unstable. You can modify /etc/apt/preferences to this effect:
Package: *
Pin: release a=unstable
Pin-Priority: 490

6.2.7. 自动追踪已安装的软件包

apt 的必要功能之一是经由相依性追踪已安装的软件包。这些软件包称为 “自动”,且通常包括程序库。
有了这些信息后,移除软件包时,软件包管理器可以计算不再需要的自动软件包 (因为没有 “手动安装” 软件包相依于他)。apt-get autoremoveapt autoremove 会清除该软件包。aptitude 没有这个命令,因为在标识之后会马上自动移除。在所有的情况下,清除消息中列出所有受影响的软件包。
把软件包标记为自动是个好习惯,不需要时就被自动移除。apt-mark auto 软件包名称 会标记该软件包为自动而 apt-mark manual 软件包名称 则不标记。aptitude markautoaptitude unmarkauto 作用相同只是功能较多 (见 第 6.5.1 节 “aptitude)。aptitude 的命令交互接口也可查看多个软件包的 “自动标识”。
人们可能希望知道为什么自动安装软件包出现在系统内。由于从命令行取得该等信息,可使用 aptitude why 软件包名称 (aptapt-get 没有类似的功能):
$ aptitude why python3-debian
i   aptitude         Suggests apt-xapian-index
p   apt-xapian-index Depends  python3-debian (>= 0.1.14)

6.2.8. APT Patterns

Patterns allow you to specify complex search queries to select the packages you want to install or show. They were first implemented for aptitude (see 第 6.5 节 “前端:aptitudesynaptic), but now APT can also make use of them.
For example, we can use apt list ?automatic to list all the packages automatically installed. To find packages automatically installed no longer depended upon by manually installed packages the pattern ?garbage can be used.
Logic patterns can be combined with other packages to form more complex expressions. For instance, we could use a pattern like ?and(PATTERN, PATTERN). See apt-patterns(7) and glob(7) for all the patterns you can use and the complex expressions you can create with them.