Product SiteDocumentation Site

6.2. aptitudeapt-getapt 命令

APT 是个原先有图形接口的大计划。以包括核心应用程序的程序库为基础,且包括第一个前端 — 命令行指令 — apt-getapt 是 APT 的第二个命令行指令包括若干处理错误的 apt-get
Both tools are built on top of the same library and are thus very close but the default behaviour of apt has been improved for interactive use and to actually do what most users expect. APT's developers reserve the right to change the public interface of this tool to further improve it. On the opposite, 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的命令行语法也仍与之非常类似。在aptapt-getaptitude存在较大差异时,将会详述差异。

6.2.1. 开端

对于 APT 的任何操作,需要更新可用包的列表;这可以简单地通过apt update来完成。取决于您连接的速度,该操作需要花费一定时间,因为这包括下载一定数量的软件包/源代码/翻译-语言代码文件,这些文件随着 Debian 的开发而逐渐变大(对于main部分,至少有 10MB 数据)。当然,从光盘中安装无需任何下载 — 此情况下,操作是十分迅速的。

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 归档可以补救(参阅侧边栏r 进阶旧包版本: snapshot.debian.org)。

例 6.3. 安装 spamassassin(垃圾邮件过滤)unstable 版本

#  apt install spamassassin/unstable
If the package to install has been made available to you under the form of a simple .deb file without any associated package repository, it is still possible to use APT to install it together with its dependencies (provided that the dependencies are available in the configured repositories) with a simple command: apt install ./path-to-the-package.deb. The leading ./ is important to make it clear that we are referring to a filename and not to the name of a package available in one of the repositories.

6.2.3. 系统升级

常规升级是推荐的,因为它们包含最新的安全更新。要升级,可使用apt upgradeapt-get upgradeaptitude safe-upgrade(当然需在apt update之后)。该命令会查找要升级的已安装包,同时不移除任何包。也就是说,目标是确保可能的侵入式升级最小化。apt-get命令略苛刻于 aptitudeapt命令,因为它会拒绝安装未预先安装的软件包。
apt 会选择最新的版本号 (除了来自 实验版稳定反向植入版,其缺省忽略版本号)。若指定 测试版不稳定版sources.list 内,则 apt upgrade 会把 稳定版 系统切换至 测试版不稳定版,这些都不是您要的。
搜索升级的软件包时需把特定的发行版告知 apt,采用 -t--target-release 选项,然后是发行版的名称 (如:apt -t stable upgrade)。为了避免每次使用 apt 都需指定版本,可以把, you can add 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 接受给读者相当的便利。

6.2.4. 配置选项

除了已提及的配置细目,还可在 /etc/apt/apt.conf.d/ 文件夹内增加 APT 的其他配置。让 APT 告知 dpkg 忽略由 DPkg::options { "--force-overwrite"; } 引发的错误。
若只能经由代理服务器近用网页,添加像这样的一列 Acquire::http::proxy "http://yourproxy:3128"。对 FTP 代理服务器,用 Acquire::ftp::proxy "ftp://yourproxy"。更多的配置选项,参考 apt.conf(5) 手册页以 man apt.conf 命令 (手删的详情,见 第 7.1.1 节 “手册页面”) 查看。

6.2.5. 包的优先级管理

配置 APT 最重要的层面之一是管理每个软件包来源的优先性。例如,从 测试版不稳定实验版 中选取一个或多个软件包。可以指令每个软件包的优先性 (视其版本或发行版,同个软件包可以有多个优先性)。这些优先性将影响 APT 的行为:每个软件包总是选择最优先的版本 (除非旧于已安装的版本或优先性小于 1000)。
APT 设置若干缺省的优先性。已安装软件包版本的优先性是 100。未安装版本缺省优先性为 500,若是另个发布的目标则可跳至 990 (以 -t 命令行选项或 APT::Default-Release 配置指令调整)。
/etc/apt/preferences 文件内添加条目的方式,指名受影响软件包名称、版本、出处及其新的优先性。
APT 永远不会安装旧版的软件包 (就是软件包的版本编号小于已安装的软件包),除非其优先性高于 1000。APT 总是安装优先性最高的软件包。若两个软件包的优先性相同,APT 安装最新的 (其版本编号较高)。若同版本的两个软件包优先性相同但内容不同,APT 安装还没有安装的版本 (包括没有递增修订编号的软件包,通常是需要的)。
更具体地说,永远不会安装优先性小于 0 的软件包。没有安装其他版本时,将安装优先性在 0 与 100 之间的软件包。其他发行版没有更新版本或可用的版本时,安装优先性在 100 与 500 之间的软件包。在目标发行版内没有更新的版本时,安装优先性在 501 与 990 之间的软件包。已安装的版本不是更新时,安装优先性在 990 与 1000 之间的软件包。即使使强迫 APT 降级也要安装优先性高于greater than 1000 的软件包。
When APT checks /etc/apt/preferences, 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 9 for Debian Stretch). 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.24 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.24*
Pin-Priority: 1001
此配置档的参考文档在手册的 apt_preferences(5),以 man apt_preferences 命令就可看到。

6.2.6. 在多个发行版工作

apt 是很奇妙的工具,可从其他发行版提取软件包。例如,安装 稳定版 系统之后,或许想要在不偏离系统原来状态下,试试 测试版不稳定版 的软件包。
混用不同版本软件包出问题时,Even if you will occasionally encounter problems while mixing packages from different distributions, apt 可以处理得极为恰当把风险降到最低。最好的方法是把所有的发行版置于 /etc/apt/sources.list 文件内 (某些人总是置入三个发行版,不过还是要记得 不稳定版 是有经验用户的专属。) 且在 APT::Default-Release 参数中设置偏好的发行版 (见 第 6.2.3 节 “系统升级”) 。
假设 稳定版 是您的参考版本,但是 测试版不稳定版 也列在 sources.list 文件内。在这个情况下,您可使用 apt install 软件包名称/testing 安装来自 测试版 的软件包。若因相依性未满足而安装失败,可以在 测试版 内加入 -t testing 参数。同样的方式也适用于 不稳定版
在此情况下,除了已经被其他发行版升级之外,升级 (upgradefull-upgrade) 只在 稳定版 内完成:其他的升级在各自的发行版内处理。稍后以 APT 缺省的优先性说明此行为。请使用 apt-cache policy (见专栏 秘诀 apt-cache policy) 查看指定的优先性。
每件事都围绕在 APT 只处理高于或等于已安装版本号软件包的前提下 (假设 /etc/apt/preferences 还未强迫优先性高于 1000 的某些软件包)。
假设从Let's assume that you have installed version 1 of a first package from 稳定版 安装第一个软件包的第一版且从and that version 2 and 3 are available respectively in 测试版不稳定版 安装同软件包的第二版与第三版水。已安装的版本优先性为 100 但在but the version available in 稳定版 (相同的) 优先性为 990 (因为它是目标发布版的一部分)。在 测试版不稳定版 的软件包其优先性为 500 (缺省不安装版本的优先性)。获选者为优先性 990 的版本1。软件包 “留在 稳定版 内”。
另个软件包的例子,已从 测试版 安装版本 2。版本 1 在 稳定版 且版本 3 在 不稳定版。因为优先性小于已安装的版本,所以版本 1 (优先性为 990 — 小于 1000) 已作废。只剩下优先性为 500 的版本 2 与版本 3。在此情况下,APT 选择较新的版本,即使用 不稳定版。若不要已安装自 测试版 的软件包,可以迁移至 不稳定版,必须指定来自于 不稳定版 优先性小于 500 (例如 490) 的软件包。修改 /etc/apt/preferences 文件即可:
Package: *
Pin: release a=unstable
Pin-Priority: 490

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

apt 的必要功能之一是经由相依性追踪已安装的软件包。这些软件包称为 “自动”,且通常包括其程序库。
With this information, when packages are removed, the package managers can compute a list of automatic packages that are no longer needed (because there is no “manually installed” packages depending on them). apt-get autoremove or apt autoremove will get rid of those packages. aptitude does not have this command because it removes them automatically as soon as they are identified. In all cases, the tools display a clear message listing the affected packages.
把软件包标记为自动是个好习惯,不需要时就被自动移除。apt-mark auto 软件包名称 会标记该软件包为自动而 apt-mark manual 软件包名称 则不标记。aptitude markautoaptitude unmarkauto 作用相同只是功能较多 (见 第 6.4.1 节 “aptitude)。命令交互接口的 aptitude 也可查看多个软件包的 “自动旗标”。
自动安装软件包出现在系统内。系由于从命令行取得该等信息,可使用 aptitude why 软件包名称 (aptapt-get 没有类似的功能):
$ aptitude why python-debian
i   aptitude         Recommends apt-xapian-index         
i A apt-xapian-index Depends    python-debian (>= 0.1.15)