ILD

debian学习笔记
作者:Yuan Jianpeng 邮箱:yuanjp89@163.com
发布时间:2026-5-11 站点:Inside Linux Development

Debian是一个主流的Linux发行版。从Debian衍生出了很多其它的发行版,比如著名的Ubuntu。下面这个链接可以查看完整的Debian家族。

https://upload.wikimedia.org/wikipedia/commons/6/69/DebianFamilyTree1210.svg

  1. 关于Debian

从版本管理、体系结构等了解Debian系统。

  1. Release

Debian有3个版本,stable、testing、unstable。

stable是稳定版本,testing是测试版本,unstable是开发版本。testing版本是下一个要发布的stable版本。unstable是开发者正在开发的版本,可能有很多bug,但是它的版本是最新的。

Debian release的生命周期是5年,前3年全力支持,后2年long term support。

当前的stable release是Debian 12,代号bookworm,2023年6月首次发布12.0版本。当前testing版本,代号trixie。unstable版本,代号总是叫做sid。

Debian稳定版本的软件相对比较老旧,比如最新的stable版本Debian 12.6的内核版本是6.1,6.1的内核相对来说比较老了。这可能是Debian的一个显著特点。而Ubuntu的版本似乎激进很多。Debian faq对这个策略有专门讲解。Debian的stable版本一旦发布,就不更新软件版本,而只更新安全修复。

  1. Compatibility

Debian有所有package的源码,因此只要设备上能跑起来Linux内核,就可以编译出可运行的debian系统。因此兼容性是极好的。

目前的Debian release,下列架构包含了完整的二进制发布。

amd64/arm64/armel/armhf/i386/ia64/mips/powerpc/ppc64el/s390x

Debian遵循Linux Filesystem Hierarchy标准。

/usr/local目录不在Debian包管理系统目录中,因此第三方的和用户自定义的程序可以安装到此目录。

  1. Package

debian的所有东西都是包。

Everything in Debian – every application, every component – everything – is built into a package.

分为binary package和source package.

conffile是配置文件,位于/etc目录下,在包升级的时候,不会覆盖conffile。

优先级:required,important,standard,optional,extra

Virtual package

  1. Build package

debian的package编译很标准,可以使用debian的标准命令从repository下载源码,然后使用标准命令编译。

  1. 更改sources.list,加上deb-src

$ sudo vim /etc/apt/sources.list

yuanjp@debian:~/hello$ cat /etc/apt/sources.list
deb http://mirrors.ustc.edu.cn/debian/ stable main
deb-src http://mirrors.ustc.edu.cn/debian/ stable main

加上之后,执行update

$ sudo apt update

  1. 安装工具和依赖包

安装dpkg-dev

$ sudo apt-get install dpkg-dev

安装包编译工具

$ sudo apt install devscripts

安装包的编译依赖,我们要编译hello包

$ sudo apt build-dep hello

  1. 下载源码

$ mkdir hello

$ cd hello

$ apt-get source hello

yuanjp@debian:~/hello$ apt source hello
Reading package lists... Done
NOTICE: 'hello' packaging is maintained in the 'Git' version control system at:
https://salsa.debian.org/sanvila/hello.git
Please use:
git clone https://salsa.debian.org/sanvila/hello.git
to retrieve the latest (possibly unreleased) updates to the package.
Need to get 741 kB of source archives.
Get:1 http://mirrors.ustc.edu.cn/debian stable/main hello 2.10-3 (dsc) [1,721 B]
Get:2 http://mirrors.ustc.edu.cn/debian stable/main hello 2.10-3 (tar) [726 kB]
Get:3 http://mirrors.ustc.edu.cn/debian stable/main hello 2.10-3 (asc) [819 B]
Get:4 http://mirrors.ustc.edu.cn/debian stable/main hello 2.10-3 (diff) [12.7 kB]
Fetched 741 kB in 0s (1,669 kB/s)
dpkg-source: info: extracting hello in hello-2.10
dpkg-source: info: unpacking hello_2.10.orig.tar.gz
dpkg-source: info: unpacking hello_2.10-3.debian.tar.xz

注意,这里是下载到当前目录,因此不需要sudo权限。

yuanjp@debian:~/hello$ ls -1
hello-2.10
hello_2.10-3.debian.tar.xz
hello_2.10-3.dsc
hello_2.10.orig.tar.gz
hello_2.10.orig.tar.gz.asc
  1. 编译

首先进入到上面解压出来的源码目录

$ cd hello-2.10

创建一个编译版本

$ dch -l local hello-mybuild

编译

$ debuild -us -uc

编译完后,在上级目录生成.deb

yuanjp@debian:~/hello/hello-2.10$ ls ../
hello-2.10                  hello_2.10-3local1_amd64.build      hello_2.10-3local1_amd64.deb      hello_2.10.orig.tar.gz
hello_2.10-3.debian.tar.xz  hello_2.10-3local1_amd64.buildinfo  hello_2.10-3local1.debian.tar.xz  hello_2.10.orig.tar.gz.asc
hello_2.10-3.dsc            hello_2.10-3local1_amd64.changes    hello_2.10-3local1.dsc            hello-dbgsym_2.10-3local1_amd64.deb
  1. Package management

  1. Binary package

.deb文件是ar格式,可以使用ar命令解压

yuanjp@debian:~/hello/deb$ ar x ../hello_2.10-3local1_amd64.deb

yuanjp@debian:~/hello/deb$ ls

control.tar.xz data.tar.xz debian-binary

解压出来有3个文件control.tar.xz是meta information文件,包含基本信息和md5sum。data是包的实际文件内容。

yuanjp@debian:~/hello/deb$ ls
control.tar.xz  data.tar.xz  debian-binary
yuanjp@debian:~/hello/deb$ tar xf control.tar.xz
yuanjp@debian:~/hello/deb$ ls
control  control.tar.xz  data.tar.xz  debian-binary  md5sums
yuanjp@debian:~/hello/deb$ cat control | head -n 16
Package: hello
Version: 2.10-3local1
Architecture: amd64
Maintainer: Santiago Vila <sanvila@debian.org>
Installed-Size: 276
Depends: libc6 (>= 2.34)
Conflicts: hello-traditional
Breaks: hello-debhelper (<< 2.9)
Replaces: hello-debhelper (<< 2.9), hello-traditional
Section: devel
Priority: optional
Homepage: https://www.gnu.org/software/hello/
Description: example package based on GNU hello
 The GNU hello program produces a familiar, friendly greeting.  It
 allows non-programmers to use a classic computer science tool which
 would otherwise be unavailable to them.
  1. Source package

通常包含4个文件:

一个.dsc文件,相当于control文件,是一个text文本格式的。

一个.orig.tar.gz文件,这个是源码,debian不改动这个文件。

一个.debian.tar.xz文件,debian的改动和相关信息。

一个.orig.tar.gz.asc文件,似乎存储的是一个PGP签名,文本格式的。

  1. prerequisites

相关工具

dpkg,底层的文件级别的包管理工具。

apt,APT前端CLI工具,包括apt/apt-get/apt-cache

aptitude,APT前端交互式全屏终端工具

apt-file,APT命令行包搜索工具

注意事项

  1. 不要修改/var/lib/dpkg下面的文件

  2. 安装自己从源码编译的软件的时候,不要覆盖系统文件,安装到/usr/local和/opt

  1. Source list

Debian archive通常部署在云端服务器。debian官方archive和mirrors。source list文件用来配置如何从debian archive更新package。

配置文件有/etc/apt/sources.list和/etc/apt/sources.list.d/*.list。

格式有单行格式和deb822-style格式。我们通常使用单行格式:

deb http://deb.debian.org/debian/ bookworm main non-free-firmware contrib non-free
deb-src http://deb.debian.org/debian/ bookworm main non-free-firmware contrib non-free
deb http://security.debian.org/debian-security bookworm-security main non-free-firmware contrib non-free
deb-src http://security.debian.org/debian-security bookworm-security main non-free-firmware contrib non-free

第一个,deb表示binary packages,deb-src表示source packages

第一个参数,表示debian archive的root URI。

第二个参数,表示distribution name,使用 suite name或者codename。可以加上一些后缀,比如:-security

第三个及之后的参数,表示area、或者section。

Suite name有:stable、testing、unstable。

codename是具体的版本代号:bookworm、trixie。

area有:main、non-free-firwmare、contrib、non-free

main是满足DFSG,且不依赖non-free的包

contrib是满足DFSG,但是以来non-free的包

  1. Dependencies

包与包之间存在多种关系。

Depends

这个字段表示依赖的包,必须同时安装或者提前安装。

Pre-Depends

和Depends类似,但是必须提前安装

Recommends

strong的,但不是绝对的依赖。

Suggests

Weak dependency

Enhances

和Suggests一样是弱依赖,作用相反,表示我这个包对别人有作用。

Breaks

表示和一些版本不兼容,解决办法是升级这些包

Conflicts

表示绝对的不兼容(absolute incompatibility)

Replaces

表示安装了这个包之后,替换了这些包

Provides

表示安装这个包之后,提供了这些包的文件和功能

  1. Event flow

包管理的流程

Update

首先是apt update,来从服务器更新元数据到本地。

Upgrade

升级本地包,apt upgrade 或 apt full-upgrade

Install

安装一个包, apt install

从服务器下载binary,解压,执行preinst脚本,安装二进制文件,执行postinst脚本。

Remove

删除一个包,apt remove

运行prerm脚本,删除除配置外的所有文件,执行postrm脚本。

Purge

彻底删除一个包,apt purge

和remove类似,不过配置文件也会删除。

  1. Basic operations

apt-get 和 apt-cache是基本的APT包管理工具。apt是更高级别的命令行接口,它是apt-get/apt-cache等命令的wrapper。

apt install提供安装进度条,apt安装后,会删除下载.deb文件。用户推荐使用apt命令,而脚本,推荐使用apt-get和apt-cache。

apt syntaxaptitude syntaxapt-get/apt-cache syntaxdescription
apt updateaptitude updateapt-get updateupdate package archive metadata
apt install fooaptitude install fooapt-get install fooinstall candidate version of "foo" package with its dependencies
apt upgradeaptitude safe-upgradeapt-get upgradeinstall candidate version of installed packages without removing any other packages
apt full-upgradeaptitude full-upgradeapt-get dist-upgradeinstall candidate version of installed packages while removing other packages if needed
apt remove fooaptitude remove fooapt-get remove fooremove "foo" package while leaving its configuration files
apt autoremoveN/Aapt-get autoremoveremove auto-installed packages which are no longer required
apt purge fooaptitude purge fooapt-get purge foopurge "foo" package with its configuration files
apt cleanaptitude cleanapt-get cleanclear out the local repository of retrieved package files completely
apt autocleanaptitude autocleanapt-get autocleanclear out the local repository of retrieved package files for outdated packages
apt show fooaptitude show fooapt-cache show foodisplay detailed information about "foo" package
apt search regexaptitude search regexapt-cache search regexsearch packages which match regex
N/Aaptitude why regexN/Aexplain the reason why regex matching packages should be installed
N/Aaptitude why-not regexN/Aexplain the reason why regex matching packages can not be installed
apt list --manual-installedaptitude search '~i!~M'apt-mark showmanuallist manually installed packages

  1. Advanced operations

  1. 列出已经安装的package的所有文件

$ dpkg -L vim/.
/usr
/usr/bin
/usr/bin/vim.basic
/usr/share
/usr/share/bug
/usr/share/bug/vim
/usr/share/bug/vim/presubj
/usr/share/bug/vim/script
/usr/share/doc
/usr/share/doc/vim
/usr/share/doc/vim/NEWS.Debian.gz
/usr/share/doc/vim/changelog.Debian.gz
/usr/share/doc/vim/changelog.gz
/usr/share/doc/vim/copyright
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/vim
  1. 查看文件属于哪个已安装的package

$ dpkg -S /usr/bin/vim.basicvim: /usr/bin/vim.basic

注意有些文件是安装脚本生成的,就查找不到,比如/usr/bin/vim就查不到

$ ls /usr/bin/vim -l
lrwxrwxrwx 1 root root 21 May  4  2023 /usr/bin/vim -> /etc/alternatives/vim
$ dpkg -S /usr/bin/vim
dpkg-query: no path found matching pattern /usr/bin/vim
  1. 从archive中查看,文件属于哪个package

需要用到apt-file命令,首先需要执行apt-file update,这个和apt update不同。

$ sudo apt-file update
$ apt-file search /usr/bin/vim.basic
vim: /usr/bin/vim.basic

很奇怪的是,不支持通配符扩展,比如 apt-file search "*/vim.baisc"

  1. 列出archive中package的所有文件

使用apt-file,要列出的包不需要安装,也不需要下载。

$ apt-file list putty
putty: /usr/bin/pageant
putty: /usr/bin/putty
putty: /usr/share/applications/putty.desktop
putty: /usr/share/doc/putty/changelog.Debian.gz
putty: /usr/share/doc/putty/copyright
putty: /usr/share/icons/hicolor/128x128/apps/putty.png
putty: /usr/share/icons/hicolor/16x16/apps/putty.png
putty: /usr/share/icons/hicolor/32x32/apps/putty.png
putty: /usr/share/icons/hicolor/48x48/apps/putty.png
putty: /usr/share/icons/hicolor/scalable/apps/putty.svg
putty: /usr/share/man/man1/pageant.1.gz
putty: /usr/share/man/man1/putty.1.gz
  1. 从archive下载deb,但是不安装它

$ apt download putty
Get:1 http://mirrors.ustc.edu.cn/debian stable/main amd64 putty amd64 0.78-2+deb12u1 [535 kB]
Fetched 535 kB in 1s (729 kB/s)

在当前目录下载了一个putty_0.78-2+deb12u1_amd64.deb

  1. Update alternatives

同一个功能可能有多个package提供,debian有一个机制,可以选择哪一个版本。使用update-alternatives命令,比如,vi。

$ sudo update-alternatives --config vi
[sudo] password for yuanjp:
There are 2 choices for the alternative vi (providing /usr/bin/vi).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/vim.basic   30        auto mode
  1            /usr/bin/vim.basic   30        manual mode
  2            /usr/bin/vim.tiny    15        manual mode

Press <enter> to keep the current choice[*], or type selection number:
  1. Debian Policy

Debian policy是debian的标准规范。

upstream概念

debian的包,绝大多数都来自第三方开源世界的包,这第三方就是upstream。对应的概念有upstream maintainer、upstream releaes、upstream version等。

  1. debian archive

  1. Archive areas

根据包的版权属性、area分为main、non-free-firmware、contrib、non-free。

main全部是开源的包。

  1. Sections

section表示包的类型,比如vim的section是editors。

section的类型一般由官方定义,其它类型有:admin、database、kernel等。

  1. Priorities

每个包必须有一个优先级priority,绝大多数包的优先级是optional。不是optional的包用来包含在标准安装的debian系统中。优先级从高到低有:

required

系统正常运行依赖的包,删除这种包,可能导致系统异常。

只用required的包,可以构建一个正常启动的最小系统,通过这个系统,可以安装更多的软件。

important

The important packages are just a bare minimum of commonly-expected and necessary tools

standard

provide a reasonably small but not too limited character-mode system

默认会安装

standard及以上优先级的两个package不会冲突

optional

默认的优先级

extra

反对,和optional一样的效果

  1. Binary package

  1. Virtual packages

有时,多个不同的包,但是或多或少提供相同的功能,这个时候,就可以定义一个virtual package,它的名字描述了共同的功能。virtual package只是逻辑上存在,没有真实的物理包。

  1. Base System

Base system是Debian system的一个最小子集,在安装其它东西之前,先安装这个小系统。

Base system包含required和important优先级的所有包,它们中许多包都被标记为essential。

  1. Essential packages

essential被定义为系统任何时候都必须要有的功能的最小子集。使用aptitude可以搜索出essential列表

$ aptitude search '?essential'

  1. Source Packages

Source package包含用于构建binary package的原材料。包含一个.dsc (debian source package control files)文件。一个或多个压缩tar文件,还可能依赖其它类型的文件。

分为两种,native和non-native。native就是debian自己开发的package,它只有单个tar文件

Source package完成本地解压后,debian的东西,在源码的debian目录下面

~/hello/hello-2.10$ ls debian/
changelog  control  copyright  rules  rules-old  source  tests  upstream  watch

比较重要的是rules文件,它是主要的编译脚本。

~/hello/hello-2.10$ cat debian/rules
#!/usr/bin/make -f
%:
        dh $@

override_dh_auto_clean:
        [ ! -f Makefile ] || $(MAKE) distclean

override_dh_installdocs:
        dh_installdocs NEWS

upstream目录包含了上游源码的地址等,也很有用。

  1. Control files and their fields

控制文件包含了包的元数据。控制文件包含很多字段。

Source package和binary package都与控制文件。下面是hello包的源控制文件。

~/hello/hello-2.10$ cat debian/control
Source: hello
Section: devel
Priority: optional
Maintainer: Santiago Vila <sanvila@debian.org>
Standards-Version: 4.6.2
Build-Depends: debhelper-compat (= 13), help2man, texinfo
Homepage: https://www.gnu.org/software/hello/
Vcs-Git: https://salsa.debian.org/sanvila/hello.git
Vcs-Browser: https://salsa.debian.org/sanvila/hello
Rules-Requires-Root: no

Package: hello
Architecture: any
Depends: ${misc:Depends}, ${shlibs:Depends}
Conflicts: hello-traditional
Replaces: hello-debhelper (<< 2.9), hello-traditional
Breaks: hello-debhelper (<< 2.9)
Description: example package based on GNU hello
 The GNU hello program produces a familiar, friendly greeting.  It
 allows non-programmers to use a classic computer science tool which
 would otherwise be unavailable to them.
 .
 Seriously, though: this is an example of how to do a Debian package.
 It is the Debian version of the GNU Project's `hello world' program
 (which is itself an example for the GNU Project).
  1. Maintainer script

包在安装和卸载的时候,可以执行脚本,来做相应的工作。主要的脚本有4个:

preinst/postinst,preinst在安装解压之前执行。后者在解压之后执行。

prerm/postrm,

  1. Port debian to arm64 board

使用debootstrap工具制作rootfs。

$ sudo apt install qemu-user-static

$ sudo cdebootstrap -vv -a arm64 -f standard --foreign Debian/bookworm myrootfs

编译后制作ext4 rootfs,烧录到realtek的nas demo板可以正常启动

  1. 参考文档

  1. Debian官方文档

Debian Arm64Port

https://wiki.debian.org/Arm64Port

Debian salsa

https://salsa.debian.org/public

Debian-cd

https://wiki.debian.org/debian-cd

CD images

https://cdimage.debian.org/cdimage/

Debian Live Manual

https://live-team.pages.debian.net/live-manual/html/live-manual/index.en.html

Debian live wiki

https://wiki.debian.org/DebianLive

The Debian GNU/Linux FAQ

https://www.debian.org/doc/manuals/debian-faq

Debian policy

https://www.debian.org/doc/debian-policy/

Debian Reference

https://www.debian.org/doc/manuals/debian-reference/

Debian Developer's Reference

https://www.debian.org/doc/manuals/developers-reference/

Debian Packaging tutorial

https://www.debian.org/doc/manuals/packaging-tutorial/packaging-tutorial.en.pdf

Debian Installer

https://www.debian.org/devel/debian-installer/

Debian Installer wiki

https://wiki.debian.org/DebianInstaller

Packaging

https://wiki.debian.org/Packaging

BuildingTutorial

https://wiki.debian.org/BuildingTutorial

Debian New Maintainers' Guide

https://www.debian.org/doc/manuals/maint-guide/

Debian Repository

https://wiki.debian.org/DebianRepository

Embedded Debian

https://wiki.debian.org/Embedded_Debian

Debian Linux Kernel Handbook

https://kernel-team.pages.debian.net/kernel-handbook/

Installation Guide

https://www.debian.org/releases/stable/installmanual

The Debian Administrator's Handbook

https://www.debian.org/doc/manuals/debian-handbook/

PackageManagement

https://wiki.debian.org/PackageManagement

CrossDebootstrap

https://wiki.debian.org/EmDebian/CrossDebootstrap

  1. 第三方文档

Debian vs Yocto for Embedded Systems

https://www.nwengineeringllc.com/article/yocto-vs-debian-for-embedded-systems-design-and-iot.php

Raspbian

a free operating system based on Debian

optimized for the Raspberry Pi hardware

https://www.raspbian.org/

How To Build Debian Packages From Source

https://ostechnix.com/how-to-build-debian-packages-from-source/

Building arm / aarch64 rootfs

https://unix.stackexchange.com/questions/554391/building-arm-aarch64-rootfs

Complete tutorial to create a Debian Minimal RootFS

https://akhileshmoghe.github.io/_post/linux/debian_minimal_rootfs


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.