Product SiteDocumentation Site

6.3. The apt-cache 命令

apt-cache 命令可显示 APT 内部数据库里的多种信息。这些信息是从 sources.list 文件内聚集不同来源的缓存。于运行 apt update 运作时产生的。
apt-cache 命令可以通过 apt-cache search keyword做基于关键词的软件包搜索。也能使用 apt-cache show 软件包名称 显示可用版本软件包的标头。这个命令提供软件包说明、依赖性、维护者名称等。请注意,apt searchapt showaptitude searchaptitude show 都以同样方式运作。
Some features are more rarely used. For instance, apt-cache dumpavail displays the headers of all available versions of all packages. apt-cache pkgnames displays the list of all the packages which appear at least once in the cache.
One of the features which might come in handy more often is apt-cache policy, described in the following section.

6.3.1. The apt-cache policy Command

The apt-cache policy command displays the pinning priorities and distribution properties of each package source as explained in 第 6.2.5 节 “软件包优先级管理”. It can also show the pinning priorities for all available versions and sources of a package. For the sources.list example used in 例 6.2 “给 Debian 稳定版用户的 /etc/apt/sources.list 文件” and APT::Default-Release set to "bullseye", the output will look like this:
$ apt-cache policy
Package files:
 100 /var/lib/dpkg/status
     release a=now
 100 https://deb.debian.org/debian bullseye-backports/main amd64 Packages
     release o=Debian Backports,a=bullseye-backports,n=bullseye-backports,l=Debian Backports,c=main,b=amd64
     origin deb.debian.org
 990 https://deb.debian.org/debian bullseye/non-free amd64 Packages
     release v=11.0,o=Debian,a=stable,n=bullseye,l=Debian,c=non-free,b=amd64
     origin deb.debian.org
 990 https://deb.debian.org/debian bullseye/contrib amd64 Packages
     release v=11.0,o=Debian,a=stable,n=bullseye,l=Debian,c=contrib,b=amd64
     origin deb.debian.org
 990 https://deb.debian.org/debian bullseye/main amd64 Packages
     release v=11.0,o=Debian,a=stable,n=bullseye,l=Debian,c=main,b=amd64
     origin deb.debian.org
 500 http://security.debian.org bullseye-security/main amd64 Packages
     release v=11,o=Debian,a=stable-security,n=bullseye-security,l=Debian-Security,c=main,b=amd64
     origin security.debian.org
Pinned packages:
apt-cache policy 可以显示指定软件包所有可用版本的固定优先级和源。
$ apt-cache policy limnoria
limnoria:
  Installed: 2021.06.15-1
  Candidate: 2021.06.15-1
  Version table:
     2021.07.21-1~bpo11+1 100
        100 https://deb.debian.org/debian bullseye-backports/main amd64 Packages
 *** 2021.06.15-1 990
        990 https://deb.debian.org/debian bullseye/main amd64 Packages
        100 /var/lib/dpkg/status
Although there is a newer version of limnoria in the bullseye-backports repository, APT will not install it automatically based on the priority. One would have to use apt install limnoria/bullseye-backports or add a higher pinning priority to /etc/apt/preferences.d/limnoria.pref:
Package: limnoria
Pin: release o=Debian Backports, a=bullseye-backports
Pin-Priority: 1001
$ apt-cache policy limnoria
limnoria:
  Installed: 2021.06.15-1
  Candidate: 2021.07.21-1~bpo11+1
  Version table:
     2021.07.21-1~bpo11+1 1001
        100 https://deb.debian.org/debian bullseye-backports/main amd64 Packages
 *** 2021.06.15-1 990
        990 https://deb.debian.org/debian bullseye/main amd64 Packages
        100 /var/lib/dpkg/status