Ubuntu apt 命令介绍

发布时间: 更新时间: 总字数:4505 阅读时间:9m 作者: IP上海 分享 网址

Ubuntu/Debian apt 提供了丰富的命令,完成包的管理任务

介绍

  • APT(Advanced Package Tool)
  • apt-get 命令本身并不具有管理软件包功能,只是提供了一个软件包管理的命令行平台。
  • apt 命令不适合在脚本中运行,它会有颜色的显示、进度条显示等一些友好的交互界面,在脚本中不稳定,可能报错:WARNING: apt does not have a stable CLI interface. Use with caution in scripts.,此时,使用 apt-get 替代

使用

apt-get 命令的一般语法格式为:

apt-get  subcommands  [ -d | -f | -m | -q | --purge | --reinstall | - b | - s | - y | - u | - h | -v ]  [pkg]

更新或升级

apt-get update          # 更新源
apt-get upgrade         # 更新所有已安装的包
apt-get dist-upgrade    # 发行版升级(如,从10.10到11.04)

安装或重装类

apt-get install pkg                # 安装pkg软件包,多个软件包用空格隔开
apt-get install --reinstall pkg    # 重新安装软件包pkg
apt-get install -f pkg             # 修复安装(破损的依赖关系)软件包pkg

卸载类

apt-get check                  # 检查是否有损坏的依赖
apt-get remove package – - purge
apt-get remove pkg             # 删除软件包pkg(不包括配置文件)
apt-get purge pkg              # 删除软件包pkg(包括配置文件)

下载清除类

apt-get clean                  # 清除缓存(/var/cache/apt/archives/{,partial}下)中所有已下载的包
apt-get autoclean              # 类似于clean,但清除的是缓存中过期的包(即已不能下载或者是无用的包)
apt-get autoremove             # 删除因安装软件自动安装的依赖,而现在不需要的依赖包

源码编译类

apt-get source pkg             # 下载pkg包的源代码到当前目录
apt-get download pkg           # 下载pkg包的二进制包到当前目录
apt-get source -d pkg          # 下载完源码包后,编译
apt-get build-dep  pkg         # 构建pkg源码包的编译环境

查询类

apt-cache 提供了搜索功能

apt-cache stats                 # 显示系统软件包的统计信息
apt-cache search pkg            # 使用关键字pkg搜索软件包
apt-cache show pkg_name         # 显示软件包pkg_name的详细信息,如说明、大小、版本等
apt-cache depends pkg           # 查看pkg所依赖的软件包
apt-cache rdepends pkg          # 查看pkg被那些软件包所依赖

其中:

  1. 普通用户需要可以添加 sudo 申请管理权限
  2. 在 MAN 命令中需要退出命令帮助请按 q

相关目录

  • 默认的缓存目录是
    • /var/cache/apt/archives/
    • /var/lib/apt/lists
  • 一般的 deb 包都在 /usr/share。自己下载的压缩包或者编译的包,一般放在/usr/local//opt 目录下

/etc/apt/ 目录文件及文件夹

/etc/apt/ 目录是 Advanced Package Tool (APT) 的核心配置文件目录,APT 是 Ubuntu 和其他 Debian 系 Linux 发行版中用于管理软件包(安装、升级、移除等)的工具。

  • 主要文件夹
文件夹名称 作用及描述
sources.list.d/ 存放额外的软件包源列表文件,通常以 .list 结尾。这是推荐的添加新软件源的方式,因为它将自定义源与主源文件分开,便于管理和维护。
preferences.d/ 存放用于配置软件包优先级(Pinning)的文件。这些文件允许你指定从特定源安装或禁止安装某些软件包,或指定软件包的特定版本。
apt.conf.d/ 存放 APT 配置片段的文件,通常以数字开头(如 01autoremove)。这些文件用于覆盖或添加默认的 APT 配置选项,例如设置默认的自动移除行为或配置代理。
auth.conf.d/ 存放用于认证私人软件包仓库的凭证文件。
trusted.gpg.d/ 存放信任的 GPG 公钥文件,用于验证软件包仓库的数字签名。这是验证软件源真实性和完整性的关键。
  • 主要文件
文件名称 作用及描述
sources.list 主软件包源列表文件。它定义了 APT 从哪里获取软件包(通常是官方的 Ubuntu 仓库)。每一行指定了一个软件源的类型、URI、发行版代号和组件(如 main, universe)。
apt.conf 主 APT 配置文件。它通常是空的或不存在。如果存在,它会包含全局的 APT 配置设置,但更推荐使用 apt.conf.d/ 目录下的配置片段。
preferences 主软件包优先级配置文件。它通常是空的或不存在。如果存在,它定义了软件包的全局优先级和 pinning 规则,但更推荐使用 preferences.d/ 目录下的文件。
trustdb.gpg 包含 APT 信任的 GPG 密钥的数据库,用于验证软件包的签名。

aptitude

解决安装包冲突问题

aptitudeaptapt-get 功能一样,都是用来为 Ubuntu 安装软件包,它可以 自动解决安装时出现的各种依赖问题,安装命令如下:

apt install aptitude

缓存包

sudo aptitude --download-only install <package-name>
# 或
apt-get -d install <package-name>
或
apt --download-only <pkg> -o Dir::Cache::archives=/var/cache/apt/archives/

缓存包的位置:/var/cache/apt/archives,离线安装命令:dpkg -i *.deb

缓存 nginx 包示例:

# 1. 创建一个自定义缓存目录
mkdir ~/nginx_download

# 2. 运行命令,指定下载目录
sudo apt --download-only nginx -o Dir::Cache::archives=~/nginx_download/

# 3. 查看下载结果
ls -l ~/nginx_download

apt-key

apt-key 用来管理 Ubuntu/Debian Linux 系统中的软件包密钥,保证 deb 的可信性。

$ apt-key
Usage: apt-key [--keyring file] [command] [arguments]

Manage apt's list of trusted keys

  apt-key add <file>          - add the key contained in <file> ('-' for stdin)
  apt-key del <keyid>         - remove the key <keyid>
  apt-key export <keyid>      - output the key <keyid>
  apt-key exportall           - output all trusted keys
  apt-key update              - update keys using the keyring package
  apt-key net-update          - update keys using the network
  apt-key list                - list keys
  apt-key finger              - list fingerprints
  apt-key adv                 - pass advanced options to gpg (download key)

If no specific keyring file is given the command applies to all keyring files.

说明:

  • apt-key list 列出已保存的 key

  • apt-key add <keyid> 把 key 添加到本地信任列表

    • 对于更现代的 Debian/Ubuntu 版本,推荐将公钥放置到 /usr/share/keyrings/ 目录下,并在 APT 源列表中使用 signed-by 选项:
    # 下载公钥并dearmor
    curl -fsSL https://example.com/my-public-key.asc | sudo gpg --dearmor -o /usr/share/keyrings/my-public-key.gpg
    
    # 然后在你的 APT 源文件 (例如 /etc/apt/sources.list.d/myrepo.list) 中添加或修改类似以下行:
    deb [signed-by=/usr/share/keyrings/my-public-key.gpg] https://example.com/myrepo stable main
    
  • apt-key export <keyid> - 通过 keyid 导出 key

  • apt-key del <keyid> 从本地删除 key

  • apt-key net-update 通过网络更新 key

  • apt-key update 删除本地过期的 key

  • apt-key finger显示被信任密钥的列表

apt-mark

  • apt-mark 对软件包进行设置(手动/自动 )安装标记
    • auto 标记指定软件包为自动安装
    • manual 标记指定软件包为手动安装
    • minimize-manual Mark all dependencies of meta packages as automatically installed
    • hold 标记指定软件包为保留(held back),阻止软件自动更新
    • unhold 取消指定软件包的保留(held back)标记,解除阻止自动更新
    • showauto 列出所有自动安装的软件包
    • showmanual 列出所有手动安装的软件包
    • showhold 列出设为保留的软件包
# 禁止自动更新
$ apt-mark hold nano
nano set on hold.

# 查看禁止的软件
$ apt-mark showhold
hold

# 取消禁止自动更新
$ apt-mark unhold nano
Canceled hold on nano.

若更新 hold 的软件,会提示 xxx was already set on hold

APT 源配置格式

从传统的 单行格式 (.list) 过渡到新的 DEB822 格式 (.sources),主要是为了提高可读性、易于程序解析以及更好地管理 GPG 密钥。

旧格式 (One-Line Style)

  • 文件位置: /etc/apt/sources.list/etc/apt/sources.list.d/example.list
  • 特点: 所有信息挤在一行,选项写在方括号 [] 里。
deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu jammy stable
  • /etc/apt/sources.list 示例
# ubuntu:22.04
deb http://archive.ubuntu.com/ubuntu/ jammy main restricted

# 格式
# 二进制包
deb <uri> <distribution> [component1] [component2] […]
# 源码包
deb-src <uri> <distribution> [component1] [component2] […]
  • 以发行版 componentjammy 版本为例,官方仓库地址
    • 包含如下目录
      • main/ Canonical 支持的自由开源软件
      • multiverse/ 受版权或法律问题限制的软件
      • restricted/ 设备的专有驱动程序
      • universe/ 社区维护的自由开源软件
    • 包含如下文件
      • Contents-amd64.gz
      • Contents-i386.gz
      • InRelease
      • Release 索引文件
      • Release.gpg
    • 标准的 debian 组件包括:maincontribnon-freenon-us
  • apt 流程,从 /ubuntu/dists/jammy/Release 文件获取 Contents-amd64.gz,然后获取需要包的地址,下载,安装

新格式 (DEB822 Style)

  • 文件位置: /etc/apt/sources.list.d/example.sources
  • 特点: 类似 YAML 或 Email 头部的键值对结构,清晰分行。
Types: deb
URIs: http://archive.ubuntu.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Types: deb
URIs: http://security.ubuntu.com/ubuntu/
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

核心区别详解

特性 旧格式 (.list) 新格式 (.sources)
文件后缀 必须以 .list 结尾 必须以 .sources 结尾
结构 单行空格分隔 多行键值对 (Stanza 格式)
可读性 简单源很简洁,但复杂源(带选项)极难阅读 始终清晰,字段一目了然
注释 行内注释容易破坏解析 支持标准的 # 注释,且不影响结构
选项位置 塞在 deb 和 URL 之间的 [] 拥有独立的字段(如 Architectures
GPG 密钥 通常放在 trusted.gpg.d (已弃用) 或 signed-by 选项中 Signed-By 字段,甚至支持直接嵌入 ASCII 密钥块
程序解析 需复杂的正则匹配 标准化的 DEB822 解析库,极易自动化修改

字段对应关系表

如果你需要手动转换,请参考以下对应关系:

  • deb / deb-src $\rightarrow$ Types: deb (可以同时写 deb deb-src)
  • URL (例如 http://cn.archive.ubuntu.com/ubuntu/) $\rightarrow$ URIs:
  • 发行版代号 (例如 noble, jammy) $\rightarrow$ Suites:
  • 软件分类 (例如 main, restricted) $\rightarrow$ Components:
  • [arch=amd64] $\rightarrow$ Architectures: amd64
  • [signed-by=...] $\rightarrow$ Signed-By: ...

现状与兼容性

  • Ubuntu 24.04 LTS (Noble Numbat): 已全面默认采用新格式 (ubuntu.sources)。
  • Ubuntu 22.04 / Debian 11+: 支持新格式,但默认安装可能仍保留旧格式。
  • 兼容性: APT 同时支持这两种格式。你可以混合使用(例如系统自带源用 .sources,你自己加的第三方源用 .list),但这不推荐,最好统一管理。

apt 软件仓库

APT 参数

APT 配置遵循一种层次结构,例如 Acquire::http::Dl-Limit。您可以在 -o 后面指定几乎任何合法的配置项。

更多详细参数参考代码:https://github.com/Debian/apt/blob/3.1.9/apt-pkg/init.cc#L129

下载和网络 (Acquire)

这类选项控制 APT 如何获取软件包(下载速度、代理、超时等)。

配置项 作用 示例
Acquire::http::Dl-Limit 限制 HTTP 下载速度 (KB/s)。 -o Acquire::http::Dl-Limit=100
Acquire::https::Dl-Limit 限制 HTTPS 下载速度 (KB/s)。 -o Acquire::https::Dl-Limit=50
Acquire::http::Proxy 设置 HTTP 代理。 -o Acquire::http::Proxy="http://user:pass@host:port"
Acquire::Timeout 设置下载超时时间(秒)。 -o Acquire::Timeout=30
Acquire::Queue-Mode 控制并发下载模式(hostaccess)。 -o Acquire::Queue-Mode=host
  • /etc/apt/apt.conf.d/99timeouts 参考
Acquire::ftp::Timeout "240";
Acquire::http::timeout "240";
Acquire::https::timeout "240";
  • 限速
# 限制 apt update 的速度到 50 KB/s
sudo apt -o Acquire::http::Dl-Limit=50 update

# 限制安装软件包(例如 vim)的速度到 50 KB/s
# 注意:你需要将 'vim' 替换为你实际想要安装的包
sudo apt -o Acquire::http::Dl-Limit=50 install vim

或修改配置文件 /etc/apt/apt.conf.d/99limit-speed

// 永久限制 HTTP 和 HTTPS 的下载速度为 100 KB/s
Acquire::http::Dl-Limit "100";
Acquire::https::Dl-Limit "100";

软件包操作 (APT::Get)

这类选项控制 installupgrade 等操作的行为。

配置项 作用 示例
APT::Get::Download-Only 仅下载,不安装(与 --download-only 等价)。 -o APT::Get::Download-Only=true
APT::Get::Assume-Yes 自动回答(与 -y 等价)。 -o APT::Get::Assume-Yes=true
APT::Get::Show-Upgraded 在升级时是否显示已升级的软件包。 -o APT::Get::Show-Upgraded=true
APT::Get::Allow-Unauthenticated 允许安装未经验证的软件包。 -o APT::Get::Allow-Unauthenticated=true

目录和路径 (Dir)

这类选项用于临时更改 APT 使用的目录,例如缓存路径。

配置项 作用 示例
Dir::Cache::archives 设置 .deb 文件的缓存目录。 -o Dir::Cache::archives=/tmp/my_cache
Dir::State::lists 设置软件包列表(Packages 文件)的存储目录。 -o Dir::State::lists=/tmp/my_lists

依赖解析 (APT::Install)

这类选项影响软件包依赖关系的解决方式。

配置项 作用 示例
APT::Install-Recommends 是否将推荐软件包视为依赖项(默认是 true)。 -o APT::Install-Recommends=false
APT::Install-Suggests 是否将建议软件包视为依赖项(默认是 false)。 -o APT::Install-Suggests=true

发布和版本 (APT)

这类选项用于指定从哪个发行版获取软件包。

配置项 作用 示例
APT::Default-Release 指定默认的目标发布版本。 -o APT::Default-Release="stable"

私有源认证

  • /etc/apt/auth.conf.d/private-repo.conf
# 机器(machine)匹配 sources.list 中 URI 的主机名(hostname)部分
# 可以选择性地包含协议(protocol://)、端口(:port)和路径(/path)
machine example.com
login myuser
password mypassword

代理认证

  • /etc/apt/apt.conf.d/70proxy
Acquire::http::Proxy "http://proxy.internal:8080";
# 注意:使用 auth.conf.d 方式,Acquire::http::Proxy 就不再需要包含用户名和密码了。
  • /etc/apt/auth.conf.d/proxy.conf
# 机器(machine)匹配你在 apt.conf.d 中配置的代理服务器的主机名
# 假设你的代理配置是 Acquire::http::Proxy "http://proxy.internal:8080";
machine proxy.internal:8080
login proxyuser
password proxypass

deb 包 gpg 签名示例

也可以使用 debsign 签名,但通常签名 .deb 包通常不是直接签名 .deb 文件本身,而是通过创建 Release 文件和 Packages 文件的 GPG 签名来实现的,示例如下:

gpg --full-generate-key
  • 创建一个简单的 .deb 软件包
# 创建软件包目录结构
mkdir -p my-example-app/DEBIAN
mkdir -p my-example-app/usr/local/bin

# 创建一个简单的可执行脚本
echo '#!/bin/bash' > my-example-app/usr/local/bin/my-example-script
echo 'echo "Hello from my example app!"' >> my-example-app/usr/local/bin/my-example-script
chmod +x my-example-app/usr/local/bin/my-example-script

# 创建 control 文件
cat <<EOF > my-example-app/DEBIAN/control
Package: my-example-app
Version: 1.0.0
Section: custom
Priority: optional
Architecture: amd64
Maintainer: Your Name <your.email@example.com>
Description: A simple example application.
EOF

# 构建 .deb 包
dpkg-deb --build my-example-app my-example-app_1.0.0_amd64.deb
  • 为 APT 仓库准备和签名 Release 文件
  • 创建一个简单的本地 APT 仓库
mkdir -p ~/my-apt-repo/dists/stable/main/binary-amd64
mkdir -p ~/my-apt-repo/pool/main

# 将你的 .deb 包移动到仓库的 pool 目录
mv my-example-app_1.0.0_amd64.deb ~/my-apt-repo/pool/main/

# 生成 Packages 文件
cd ~/my-apt-repo
dpkg-scanpackages pool/main /dev/null | gzip -9c > dists/stable/main/binary-amd64/Packages.gz
dpkg-scanpackages pool/main /dev/null > dists/stable/main/binary-amd64/Packages
  • 生成 Release 文件,Release 文件包含仓库中所有软件包的哈希值和大小信息
# 注意:路径需要正确指向你的 Packages 和 Packages.gz 文件
apt install apt-utils
cd dists/stable && apt-ftparchive release . > Release
  • 签名 Release 文件,生成 Release.gpg 和 InRelease 文件,参数说明:
    • --armor 选项表示使用 ASCII Armored 格式输出(文本格式)
    • --detach-sign 表示生成一个单独的签名文件(.gpg 后缀)
    • --clearsign 则将签名嵌入到原始文件内部(生成 InRelease)
# 签名 Release 文件,生成 Release.gpg
gpg --yes --output dists/stable/Release.gpg --armor --detach-sign dists/stable/Release

# 生成 InRelease 文件(推荐,因为它包含了签名和 Release 数据在一个文件中)
gpg --yes --output dists/stable/InRelease --armor --clearsign dists/stable/Release
  • 发布公钥
gpg --armor --export "Your Name <your.email@example.com>" > my-repo-key.asc
  • 使用时,添加公钥
sudo apt-key add my-repo-key.asc
# 或者,对于更现代的 Debian/Ubuntu 版本,更推荐的方式:
# curl -fsSL https://your-repo.com/my-repo-key.asc | sudo gpg --dearmor -o /usr/share/keyrings/my-repo-keyring.gpg

# 或
mv xxx.gpg /etc/apt/trusted.gpg.d/
  • /etc/apt/sources.list.d/my-repo.list
# 如果你使用了 InRelease 或 Release.gpg
deb [arch=amd64] file:///home/user/my-apt-repo stable main

# 如果你使用了 signed-by 选项(推荐)
# deb [arch=amd64 signed-by=/usr/share/keyrings/my-repo-keyring.gpg] file:///home/user/my-apt-repo stable main

FaQ

Updates for this repository will not be applied.

apt update 时,报如上错误,该问题一般由于系统时间不同步导致,使用如下命令同步:

hwclock --hctosys

apt skip ssl cert verify

cat << EOF > /etc/apt/apt.conf.d/80ssl-exceptions
// Do not verify peer certificate
Acquire::https::Verify-Peer "false";
// Do not verify that certificate name matches server name
Acquire::https::Verify-Host "false";
EOF

apt -o "Acquire::https::Verify-Peer=false" update

echo 'Acquire::https::Verify-Peer "false";' >> /etc/apt/apt.conf.d/99disable-signature-verification
echo 'Acquire::https::Verify-Host "false";' >> /etc/apt/apt.conf.d/99disable-signature-verification

proxy 配置

  • 方式一:修改 /etc/apt/apt.conf 配置
# Acquire::http::Proxy "http://[username:password]@proxy-host:proxy-port/";
Acquire::http::Proxy "http://192.168.0.10:1080/";

或执行命令时临时指定 proxy

# apt install xxx -o -o Acquire::http::proxy="http://[username:password]@proxy-host:proxy-port/"
sudo apt install vim -o Acquire::http::proxy="http://192.168.0.10:1080/"
  • 方式二:修改 /etc/apt/apt.conf.d/proxy.conf
Acquire {
  HTTP::proxy "http://127.0.0.1:8889";
  HTTPS::proxy "http://127.0.0.1:8889";
}
  • 方式三:/etc/apt/auth.conf 参考
machine example.org
login apt
password debian

或 sources.list 中调整为

deb http://apt:debian@example.org/debian buster main

disable signed

apt-get update --allow-unauthenticated
sudo apt-get install <package-name> --allow-unauthenticated
  • 或,/etc/apt/apt.conf.d/99insecure
APT::Get::AllowUnauthenticated "true";
Acquire::AllowInsecureRepositories "true";
Acquire::AllowDowngradeToInsecureRepositories "true";
本文总阅读量 次 本站总访问量 次 本站总访客数
Home Archives Categories Tags Statistics