Ubuntu包管理

dpkg

dpkg是Debian的一个底层包管理工具,主要用于对已下载到本地和已安装的软件包进行管理。
dpkg即package manager for Debian ,是Debian和基于Debian的系统中一个主要的包管理工具,可以用来安装、构建、卸载、管理deb格式的软件包。

相关文件

  • /etc/dpkg/dpkg.cfg

    1
    2
    # dpkg包管理软件的配置文件
    /etc/dpkg/dpkg.cfg
  • /var/lib/dpkg/available

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    # 记录系统中所有可用软件包的信息
    # 不区分软件包的安装状态,只关注软件包的可用性。即使软件包未安装,只要它在软件源中可用,就会被记录在 available 文件中。
    /var/lib/dpkg/available
    # 部分内容
    Package: apt-utils
    Architecture: amd64
    Version: 2.7.14build2
    Priority: important
    Section: admin
    Source: apt
    Origin: Ubuntu
    Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
    Original-Maintainer: APT Development Team <deity@lists.debian.org>
    Bugs: <url id="ctv3sgkr4djh7of56nh0" type="url" status="parsed" title="One account for everything on Ubuntu" wc="702">https://bugs.launchpad.net/ubuntu/+filebug</url>
    Installed-Size: 660
    Depends: apt (= 2.7.14build2), libapt-pkg6.0t64 (&gt;= 2.7.14build2), libc6 (&gt;= 2.34), libdb5.3t64, libgcc-s1 (&gt;= 3.3.1), libstdc++6 (&gt;= 13.1)
    Filename: pool/main/a/apt/apt-utils_2.7.14build2_amd64.deb
    Size: 217106
    MD5sum: 276cc77854ef70115f98b576faafede6
    SHA1: 9f1c3f32d3cde66b6b8eec54e6218ed23f46db92
    SHA256: 67270c9a71dd209d7a3ba3e9474933e98a2c8e3b0390b12f2aa64cc586a00f9a
    SHA512: 42220bcd5bfcb43ac0fbc9b7b72f7c0d982d4ca38bb93ddcc102e96137ca713fab490d1e0956cc63cea5245cf1f32497c1412814d23f24271ac537668ea96972
    Description: package management related utility programs
    Task: minimal
    Description-md5: fa0295dc4e40dbbea6c84f614c570636

    Package:...
    ...
  • /var/lib/dpkg/status

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    # 记录系统中所有软件包的安装状态和配置信息。
    # 关注软件包的安装状态和配置情况。例如,可以查看软件包是否已安装、是否配置完成、是否存在错误等。
    /var/lib/dpkg/status
    # 部分内容
    Package: apt-utils
    Status: install ok installed
    Priority: important
    Section: admin
    Installed-Size: 660
    Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
    Architecture: amd64
    Source: apt
    Version: 2.7.14build2
    Depends: apt (= 2.7.14build2), libapt-pkg6.0t64 (>= 2.7.14build2), libc6 (>= 2.34), libdb5.3t64, libgcc-s1 (>= 3.3.1), libstdc++6 (>= 13.1)
    Description: package management related utility programs
    This package contains some less used commandline utilities related
    to package management with APT.
    .
    * apt-extracttemplates is used by debconf to prompt for configuration
    questions before installation.
    * apt-ftparchive is used to create Packages and other index files
    needed to publish an archive of Debian packages
    * apt-sortpkgs is a Packages/Sources file normalizer.
    Original-Maintainer: APT Development Team <deity@lists.debian.org>

    Package:...
    ...
  • /var/lib/dpkg/info

    1
    2
    3
    4
    5
    6
    # 与已安装软件包相关的配置和信息文件
    # 包括配置脚本(preinst、postinst、prerm、postrm)、配置文件列表(conffiles)、文件列表(list)、触发器文件(triggers)、共享库信息文件(shlibs)、MD5 校验和文件(md5sums)等。
    /var/lib/dpkg/info
    # 部分文件
    apt-utils.list # apt-utils包安装的所有文件的路径
    apt-utils.md5sums # MD5 校验和文件
  • /var/log/dpkg.log

    1
    2
    # dpkg包管理软件的日志文件
    /var/log/dpkg.log

    dpkg使用文本文件作为数据库来维护系统中软件,包括文件清单、依赖关系、软件状态等等,通常在/var/lib/dpkg目录下。

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 安装`.deb`包
dpkg -i myapp_1.0_all.deb # --install
# 安装一个目录下所有的`.deb`包
dpkg -R /xxx
# 将`.deb`包的内容解压到系统的相应目录中,但不进行配置
dpkg --unpack myapp_1.0_all.deb
# 配置软件包
dpkg --configure myapp_1.0_all.deb
# 显示.deb`包内文件列表
dpkg -c myapp_1.0_all.deb
# 从指定目录构建`.deb`包
dpkg -b /xxx/myapp myapp_1.0_all.deb

# 显示所有已安装包,指定具体包则只显示该包的信息
dpkg -l [myapp] # --list
# 显示某个包安装到系统里面的文件目录信息
dpkg -L myapp # --listfiles
# 报告指定包的状态信息(`/var/lib/dpkg/status`)
dpkg -s myapp # --status
# 搜索某文件来自哪个包,模糊搜索
dpkg -S filename # --search
# 删除软件包,但保留配置文件
dpkg -r myapp #--remove
# 显示包的具体信息(`/var/lib/dpkg/available`)
dpkg -p myapp
# 彻底清除软件包,包括配置文件和所有相关数据
dpkg -P myapp # --purge

APT

APT(Advanced Packaging Tool)是建立在dpkg之上更高级别的包管理工具,会使用dpkg来执行实际的软件包安装、配置和卸载操作。

起初APT的命令都被分散在了apt-getapt-cacheapt-config这三条命令当中,apt命令的引入就是为了解决命令过于分散的问题,它包括了apt-getapt-cacheapt-config中最常用命令选项的集合。

尽管dpkg在软件安装过程中解决了许多问题,但它在处理依赖关系方面存在局限性,需要用户手动解决依赖问题。
apt(Advanced Packaging Tool)则克服了这一缺陷。Linux发行版会将软件预先放置在服务器上,并分析软件的依赖关系,将这些信息记录下来。当用户需要安装软件时,apt会通过软件清单列表与本地已安装的软件数据进行比较,从而从网络服务器获取所有具有依赖关系的必要软件包。

相关文件

  • /etc/apt/apt.conf.d/

    1
    2
    3
    4
    5
    6
    7
    # 存放 APT 的配置文件,比如配置自动移除不再需要的软件包的行为。
    /etc/apt/apt.conf.d/
    # 文件如下
    $ ls apt.conf.d/
    01autoremove 15update-stamp 20auto-upgrades 50appstream 50unattended-upgrades 70debconf
    01-vendor-ubuntu 20apt-esm-hook.conf 20dbus 50apt-file.conf 60icons 99update-notifier
    10periodic 20archive 20packagekit 50command-not-found 60icons-hidpi
  • /etc/apt/sources.list.d/

    1
    2
    3
    4
    5
    6
    7
    8
    # 存放软件源配置文件
    /etc/apt/sources.list.d/
    # 文件如下
    $ ls /etc/apt/sources.list.d/
    google-chrome.list vscode.list
    ubuntu-esm-apps.sources wezterm.list
    ubuntu-esm-infra.sources xmake-io-ubuntu-xmake-noble.sources
    ubuntu.sources zerotier.list

    软件源配置文件通常以.list.sources为扩展名。
    .list文件主要用于定义传统的APT软件源。它们通常包含软件源的 URI、suite、component等信息。

    1
    2
    # 格式简单,单行即可配置;每行写一个suite,每个suite可以有多个component
    deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main

    .sources文件是较新的软件源配置格式,用于更灵活地定义软件源,支持更复杂的配置选项,如签名验证、软件源的启用状态等。

    1
    2
    3
    4
    5
    6
    # 将每个信息都写成单独一行的键值对
    Types: deb
    URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
    Suites: noble noble-updates noble-backports
    Components: main restricted universe multiverse
    Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
  • /var/cache/apt/archives/

    1
    2
    # 存储从软件源下载的`.deb`包(安装后可能会被自动删除)
    /var/cache/apt/archives/
  • /var/lib/apt/lists/

    1
    2
    # 保存从软件源更新得到的软件列表信息
    /var/lib/apt/lists/

    在该文件夹中保存有软件列表的许多信息,以从官方源(http://archive.ubuntu.com/ubuntu/)拉取到的信息为例。
    每个suite级别有*InRelease文件、*Contents-*.lz4文件;每个suite下的每个component*Packages文件、*cnf_Commands-*文件、*dep11_Components-*.yml.gz文件、*dep11_icons-*.tar.gz文件、*i18n_Translation-*等。

  • *InRelease:包含了软件源的发布信息,如版本号、签名等,用于验证软件源的完整性和一致性。

  • *Contents-*.lz4:包含了软件包内容索引,使用 lz4 压缩格式,提供了软件包文件的详细列表和位置信息。

  • *Packages:包含了软件包的详细信息,如包名、版本、依赖关系、描述等,是 APT 获取软件包信息的主要来源。

  • *cnf_Commands-*:给出了软件包提供的多个命令行操作

  • *dep11_Components-*.yml.gz:包含了软件包的依赖关系信息,使用 gzip 压缩格式,帮助 APT 解析和满足软件包的依赖需求。

  • *dep11_icons-*.tar.gz:包含了软件包的图标文件,使用 tar 和 gzip 压缩格式。

  • *i18n_Translation-*:提供了软件包的本地化描述。

软件源

对官方软件源进行详细介绍,以Ubuntu22.04LTS为例。

/etc/apt/sources.list 文件如下(目前在用的Ubuntu24.04LTS已经将所有源信息的文件放入 /etc/apt/sources.list.d/ 文件夹中):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://archive.ubuntu.com/ubuntu/ jammy main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ jammy main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ jammy universe
# deb-src http://archive.ubuntu.com/ubuntu/ jammy universe
deb http://archive.ubuntu.com/ubuntu/ jammy-updates universe
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://archive.ubuntu.com/ubuntu/ jammy multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ jammy multiverse
deb http://archive.ubuntu.com/ubuntu/ jammy-updates multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted
deb http://security.ubuntu.com/ubuntu/ jammy-security universe
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security universe
deb http://security.ubuntu.com/ubuntu/ jammy-security multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security multiverse

deb-src deb:

  • deb-src: 包含软件源代码、版本修改说明、构建指令以及编译工具等。允许用户下载软件包的源代码,从而可以自己编译软件。这通常用于开发者或那些需要定制软件包或需要最新开发版本的用户。
  • deb: 包含可执行文件、库文件、配置文件、man/info页面、版权声明和其它文档。提供了预先编译好的软件包,用户可以直接安装而无需自己编译。

jammy: Ubuntu22.04 代号

  • Ubuntu 18.04 LTS bionic
  • Ubuntu 20.04 LTS focal
  • Ubuntu 22.04 LTS jammy
  • Ubuntu 24.04 LTS noble

Suites: jammy jammy-security jammy-backports jammy-updates jammy-proposed

  • 基础:由于ubuntu是每6个月发行一个新版,当发行后,所有软件包的版本在这六个月内将保持不变,即使是有新版都不更新。除开重要的安全补丁外,所有新功能和非安全性补丁将不会提供给用户更新。  
  • security:仅修复漏洞,并且尽可能少的改变软件包的行为,低风险。
  • backports:backports 的团队则认为最好的更新策略是 security 策略加上新版本的软件(包括候选版本的),但不会由Ubuntu security team审查和更新。
  • updates:包含了针对特定版本的更新,这些更新是在原始版本发布之后提供的,可能包括安全修复、bug修复和一些较小的功能改进。  
  • proposed:updates类的测试部分,仅建议提供测试和反馈的人进行安装。

Components: main restricted universe multiverse

  • main: 保留了Ubuntu支持的自由和开源软件。
  • restricted: 保留专有驱动程序(即 NVIDIA)。
  • universe: 保留了社区维护的免费开源软件。
  • multiverse: 保留有版权限制或法律问题的软件。

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
apt [options] [command] [package ...]

# 常见的options
-h # 显示帮助
-y # 直接对后续询问回答“是”
-f # 尝试修正系统依赖损坏处 sudo apt install -f

# 从配置的源下载包信息,更新本地包索引数据库
apt update
# 从配置的源安装当前系统中的所有包的可用升级
apt upgrade
# 相比`apt upgrade`更激进,如果有依赖问题,需要安装或移除新的包,就会试着去安装或移除它(更有风险)
apt dist-upgrade

# 安装一个或多个指定的包
apt install vim
# 用来删除包,但是会保留包的配置文件
apt remove vim
# 用来删除包,在删除包的同时删除其配置文件
apt purge vim
# 用于删除自动安装的包(满足其他包的依赖关系),由于依赖关系的更改或需要它们的包已被删除
apt autoremove

# 用于在可用包列表中搜索给定的项并显示匹配到的内容
apt search vim
# 显示关于给定包的信息,包括它的依赖关系、安装和下载大小、包的来源、包内容的描述等等
apt show vim
# 显示满足特定条件的包列表,默认列出所有的包
apt list --upgradeable # 列出可更新的软件包及版本信息
apt list --installed # 列出已安装的软件包及版本信息

参考:

  1. 源中的 backports proposed security updates 的意思(已解决) - Ubuntu中文论坛

  2. Linux apt 命令 | 菜鸟教程 (runoob.com)

  3. Ubuntu 软件包管理工具 —— dkpg、apt(dpkg常用指令、apt 软件源的配置)-CSDN博客

  4. Ubuntu操作-05 Dpkg与Apt


Ubuntu包管理
https://kaysonyu.github.io/2025/01/Ubuntu-package-management/
Author
Yu Kang
Posted on
January 1, 2025
Licensed under