Debian 13 apt 包管理工具命令详解

详解 Debian 13 的 apt/apt-get/dpkg 包管理工具,覆盖软件源配置、search、update、install、remove、purge、autoremove、list、show、clean、history、upgrade、dist-upgrade 及离线 deb 安装,所有回显均为 Debian 13 实测结果

debianDebian 13Linux系统2026/10/07

Debian 13 apt 包管理工具命令详解

目标:详解 Debian 13(trixie)的 apt / apt-get / dpkg 包管理工具,覆盖软件源配置、搜索、安装、卸载、查询、缓存、历史与升级全套命令,并演示离线 deb 安装。所有命令与回显均为 Debian 13 实测结果。

一、环境信息

  • 操作系统:Debian GNU/Linux 13(VERSION_CODENAME 代号 trixie;stable 套件即指向 trixie)
  • apt:apt 3.0.3 (amd64)(apt 与 apt-get 同版本)
  • dpkg:Debian 'dpkg' package management program version 1.22.22 (amd64)

二、apt 包管理器概览

Debian 的包管理基于 dpkg,apt 在其上提供仓库解析、依赖自动处理等能力:

命令定位
dpkg底层包管理,直接操作 .deb,不做依赖解析
apt-get经典前端,脚本友好,输出稳定
apt面向交互的新前端,带进度条与彩色输出

仓库位置与配置文件

Debian 13 使用 deb822 格式源文件(Debian 从 12 起默认采用):

ls /etc/apt/sources.list.d/
grep -E "^(Types|URIs|Suites|Components|Signed-By)" /etc/apt/sources.list.d/debian.sources
debian.sources
docker.list
Types: deb
URIs: http://deb.debian.org/debian
Suites: stable stable-updates
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.pgp
Types: deb
URIs: http://deb.debian.org/debian-security
Suites: stable-security
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.pgp

💡 Debian 13 的 stable 即 trixie(发布代号)。GPG 密钥除系统自带的 /usr/share/keyrings/ 外,第三方仓库按惯例放 /etc/apt/keyrings/。

三、软件源配置(官方源/国内镜像源)

1. 切换为阿里云 Debian 镜像源

备份后把官方源替换为阿里云:

cp /etc/apt/sources.list.d/debian.sources /etc/apt/sources.list.d/debian.sources.bak
sed -i -e "s|http://deb.debian.org/debian|https://mirrors.aliyun.com/debian|" \
       -e "s|http://security.debian.org/debian-security|https://mirrors.aliyun.com/debian-security|" \
       /etc/apt/sources.list.d/debian.sources
grep "^URIs" /etc/apt/sources.list.d/debian.sources
URIs: https://mirrors.aliyun.com/debian
URIs: https://mirrors.aliyun.com/debian-security

2. 验证镜像源生效

apt-get update
Get:5 https://mirrors.aliyun.com/debian stable/main amd64 Packages [9678 kB]
Get:6 https://mirrors.aliyun.com/debian stable-updates/main amd64 Packages [4412 B]
Get:7 https://mirrors.aliyun.com/debian-security stable-security/main amd64 Packages [264 kB]
Fetched 10.2 MB in 19s (524 kB/s)
Reading package lists...

3. 第三方仓库示例:阿里云 docker-ce 源

Debian 13 用 trixie 通道,GPG 密钥用 gpg --dearmor 转成 keyring 文件:

install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/debian trixie stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
cat /etc/apt/sources.list.d/docker.list
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/debian trixie stable

四、search 搜索

apt search 是全文搜索,按包名与描述匹配:

apt search sl
apt-cache search sl
2048 - Slide and add puzzle game for text mode
abcm2ps - Translates ABC music description files to PostScript
libabsl-dev - extensions to the C++ standard library (development files)
libabsl20240722 - extensions to the C++ standard library

💡 想精确定位可用 apt list -a <包名>。

五、update 刷新仓库

apt-get update
apt update    # 两种写法等价
Reading package lists...
Building dependency tree...
Reading state information...
1 package can be upgraded. Run 'apt list --upgradable' to see them.

六、install 安装

1. 多包安装

apt-get install -y htop tree
Unpacking tree (2.2.1-1) ...
Setting up htop (3.4.1-5) ...
Setting up tree (2.2.1-1) ...

💡 Debian 13 仓库中的 htop 为 3.4.1-5、tree 为 2.2.1-1(与 Ubuntu 24.04 的版本不同,随各自仓库而定)。

2. 指定版本安装

apt list -a htop
apt-get install -y htop=3.4.1-5
Listing...
htop/stable,now 3.4.1-5 amd64 [installed]

💡 按 包名=版本 指定版本;与已装版本一致时提示 htop is already the newest version。

3. --no-install-recommends 精简安装

apt-get install -y --no-install-recommends <包名>

💡 不装 Recommended 推荐依赖,适合最小化环境。

七、remove / purge / autoremove 卸载

1. remove 卸载(保留配置文件)

apt remove -y tree
(Reading database ... 13544 files and directories currently installed.)
Removing tree (2.2.1-1) ...

2. purge 彻底清除(含配置文件)

apt-get purge -y htop
(Reading database ... 13536 files and directories currently installed.)
Removing htop (3.4.1-5) ...

3. autoremove 清理孤儿依赖

apt autoremove -y
Summary:
  Upgrading: 0, Installing: 0, Removing: 0, Not Upgrading: 1

💡 apt 3.0 的 autoremove 输出为 Summary 汇总格式;无孤儿依赖时 Removing 为 0。

八、list / info / show 查询

1. dpkg -l 列出已装包

dpkg -l | grep -E "docker-ce|containerd"
ii  containerd.io                  2.3.3-1~debian.13~trixie          amd64        ...
ii  docker-ce                      5:29.7.2-1~debian.13~trixie       amd64        ...
ii  docker-ce-cli                  5:29.7.2-1~debian.13~trixie       amd64        ...
ii  docker-ce-rootless-extras      5:29.7.2-1~debian.13~trixie       amd64        ...

💡 第一列 ii 表示已安装;rc 表示已卸载但保留配置。

2. apt show 查看仓库信息

apt show htop
Package: htop
Version: 3.4.1-5
Priority: optional
Section: utils
Maintainer: Daniel Lange <DLange@debian.org>
Installed-Size: 434 kB
Depends: libc6 (>= 2.38), libncursesw6 (>= 6), libtinfo6 (>= 6)
Suggests: lm-sensors, lsof, strace

3. dpkg -s / -L

dpkg -s htop
dpkg -L htop
Package: htop
Status: install ok installed
Priority: optional
Section: utils
Installed-Size: 424
...
/.
/usr
/usr/bin
/usr/bin/htop

💡 dpkg -s 看已装包状态,dpkg -L 列出包安装的文件清单。

九、clean / 缓存管理

du -sh /var/cache/apt/archives
apt clean
apt autoclean
12K     /var/cache/apt/archives
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

💡 apt clean 清空全部缓存无输出;apt autoclean 只清理不再需要的旧版本 .deb。

十、历史记录与升级(history / upgrade / dist-upgrade)

1. 操作历史

tail -20 /var/log/apt/history.log
Start-Date: 2026-10-07  06:39:08
Commandline: apt-get upgrade -y
Upgrade: libpcre2-8-0:amd64 (10.46-1~deb13u2, 10.46-1~deb13u3)
End-Date: 2026-10-07  06:39:09

2. 查看可升级包

apt list --upgradable
Listing...
libpcre2-8-0/stable-security 10.46-1~deb13u3 amd64 [upgradable from: 10.46-1~deb13u2]

3. upgrade 普通升级

apt-get upgrade -y
Unpacking libpcre2-8-0:amd64 (10.46-1~deb13u3) over (10.46-1~deb13u2) ...
Setting up libpcre2-8-0:amd64 (10.46-1~deb13u3) ...
Processing triggers for libc-bin (2.41-12+deb13u4) ...

💡 upgrade 只升级已有包,不安装新依赖、不卸载冲突包。

4. dist-upgrade 智能升级

apt-get dist-upgrade -y
Calculating upgrade...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

💡 dist-upgrade 升级时处理依赖变化(安装/删除)。apt 对应的交互命令为 apt upgrade(Debian 12+ 默认启用 dist-upgrade 语义)。

十一、离线 deb 安装实测(dpkg -i 与依赖修复)

离线机把 .deb 拷入后执行 dpkg -i。实测用 apt-get download sl 下载后直接安装,出现依赖缺失:

cd /var/cache/docker-debs
apt-get download sl
dpkg -i sl*.deb
W: Download is performed unsandboxed as root as file '/var/cache/docker-debs/sl_5.02-1+b1_amd64.deb' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)
Selecting previously unselected package sl.
(Reading database ... 13520 files and directories currently installed.)
Preparing to unpack sl_5.02-1+b1_amd64.deb ...
Unpacking sl (5.02-1+b1) ...
dpkg: dependency problems prevent configuration of sl:
 sl depends on libncurses6 (>= 6); however:
  Package libncurses6 is not installed.

dpkg: error processing package sl (--install):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 sl

⚠️ 实测:dpkg -i 不解析依赖,报 sl depends on libncurses6 属正常现象。补齐依赖后修复:

apt-get install -f
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

💡 离线环境需提前用 apt-get download 把依赖闭包一并下载携带(如 docker-ce 离线包场景),联网环境直接 apt-get install -f 自动补装。

十二、常见问题与真实行为

  1. apt search 结果太多:全文搜索按描述匹配,想按包名精确搜用 apt-cache search 或 apt list -a。

  2. remove 后包仍占用空间:remove 保留配置文件,需 purge 彻底清除;孤儿依赖需 autoremove 清理。

  3. dpkg -i 报依赖缺失:dpkg 不做依赖解析,执行 apt-get install -f 修复,或提前把依赖 deb 一起 dpkg -i。

  4. apt 与 apt-get 命令对应:apt 为交互优化版(update/install/remove/purge/autoremove/list/show/search/upgrade);apt-get 更稳定适合脚本(update/install/remove/purge/autoremove/upgrade/dist-upgrade/download)。两者读取同一套源配置,无功能差异。

十三、命令与参数速查表

命令作用常用参数
apt-get update刷新软件包索引-
apt-get install安装包-y 免确认;包名=版本 指定版本;--no-install-recommends 精简安装
apt-get remove卸载(保留配置)-y
apt-get purge卸载并清除配置-y
apt autoremove清理孤儿依赖-y
apt-get upgrade升级已有包-y
apt-get dist-upgrade智能升级(处理依赖变化)-y
apt-get install -f修复损坏依赖-
apt search / apt-cache search搜索包-
apt list列出包--installed / -a 所有版本 / --upgradable
apt show / apt-cache show查看包详情-
apt clean / autoclean清理缓存-
dpkg -i / -r / --purge底层安装/卸载/清除-
dpkg -s / -l / -L查状态/列表/文件清单-
apt-get download仅下载 deb 不安装(离线用)-