Module 4 · Lesson 84 of 156

Building a Debian rootfs with debootstrap

Build a Debian root filesystem with debootstrap, customise it with extra packages, and keep it reproducible and security-supported for the product's lifetime.

  • Beginner to advanced
  • 8 min read
  • 40 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM with sudo and internet access; debootstrap; qemu-user-static for foreign-architecture builds; Buildroot sources for the comparison exercise

Your place in the module
By the end you can 4 objectives
  • Build a minimal Debian rootfs with debootstrap on Ubuntu 24.04, including a foreign-architecture build with qemu
  • Add extra packages with --include and with chroot plus apt, then trim the result
  • Pin the suite, mirror and package manifest so a rebuild produces the same package versions
  • Choose a Debian release whose LTS window covers the product's support lifetime
Video coming soonBuilding a Debian rootfs with debootstrap
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Build systems and binary distributions
  • Buildroot and Yocto cross-compile every program from source, yielding the smallest images and full control.
  • Debian ships prebuilt .deb packages installed with apt, giving fast development and a maintained security-update stream.
  • A .deb is an ar archive holding control metadata, the files to install, and maintainer scripts.
01

What debootstrap does

4 min read

Binary distributions ship packages, but you still assemble the image yourself. This lesson answers debootstrap with extra packages, and how LTS distributions and security updates give long-term support.

is the standard entry point into the Debian world. It compiles nothing. Every binary it installs was already built by Debian's build machines, signed and published on a mirror. Your job is to choose which release, which architecture and which packages, and debootstrap does the fetching and assembly. On your Ubuntu 24.04 host it is one install away:

bash
sudo apt install debootstrap
sudo debootstrap --arch=amd64 bookworm "$HOME/rootfs" http://deb.debian.org/debian

The selects the release, --arch selects the target architecture, and the last two arguments are the target directory and the mirror. When the command finishes, $HOME/rootfs contains a full directory tree: /bin, /etc, /lib, /var, a dpkg package database, and apt itself.

Internally the work happens in two stages. The first stage runs on your host: it downloads the .deb files and unpacks their contents into the target directory, without running any of the packages' configuration scripts. The second stage runs inside a : dpkg configures every unpacked package, running maintainer scripts exactly as it would on a real Debian machine. When the target architecture matches your host, debootstrap runs both stages automatically and you never notice the split.

The split matters when the architectures differ, which is the normal embedded case: your host is x86-64 but a Raspberry Pi 4 or 5 running 64-bit Debian needs arm64. The arm64 binaries cannot execute on your x86-64 CPU, so the second stage needs emulation:

Step by stepAn arm64 rootfs on an x86-64 host
  1. Install the emulator: sudo apt install qemu-user-static provides /usr/bin/qemu-aarch64-static and registers it with binfmt_misc, so the kernel can run arm64 binaries transparently.

  2. First stage only: sudo debootstrap --arch=arm64 --foreign bookworm rootfs-arm64 http://deb.debian.org/debian downloads and unpacks, then stops.

  3. Copy the emulator in: sudo cp /usr/bin/qemu-aarch64-static rootfs-arm64/usr/bin/ so it is reachable inside the chroot.

  4. Second stage: sudo chroot rootfs-arm64 /debootstrap/debootstrap --second-stage configures every package under emulation.

The --foreign flag means "the target binaries cannot run here, skip the second stage". The second stage is then a manual command inside the chroot, where qemu executes each arm64 program. This is slower than a native build but produces exactly the same rootfs a Pi would install on itself.

From mirror to rootfs: the debootstrap pipelinePick suitebookworm,arch,mirrorFirst stagedownloadand unpack.debSecondstagedpkgconfigurein chrootCustomise--includeor aptPackageimagetar, ext4,SquashFSPick suitebookworm,arch,mirrorFirst stagedownloadand unpack.debSecondstagedpkgconfigurein chrootCustomise--includeor aptPackageimagetar, ext4,SquashFS
The debootstrap pipeline: pick a suite, unpack on the host, configure in a chroot, customise, then package
A directory is not yet an image

debootstrap produces a directory tree. To boot it you still package it: tar it up for an NFS root, or put it into an ext4 or SquashFS image as covered in the filesystem lessons earlier in this module. The rootfs contents and the image format are separate decisions.

02

Customising the rootfs with extra packages

3 min read

A base Debian system has core utilities but none of your product's software. The first route is --include, a comma-separated list with no spaces:

bash
sudo debootstrap --arch=amd64 --include=openssh-server,iproute2,libgpiod2 \
  bookworm "$HOME/rootfs" http://deb.debian.org/debian

debootstrap resolves the dependencies of every listed package, downloads them all, and configures them during the second stage. This is the cleanest route for a fixed package set, because one command describes the whole system.

The second route is to enter the rootfs and use apt as you would on any Debian machine. Name resolution needs the host's DNS configuration copied in first:

bash
sudo cp /etc/resolv.conf "$HOME/rootfs/etc/resolv.conf"
sudo chroot "$HOME/rootfs" /bin/bash
apt update
apt install -y gpiod
echo sensornode > /etc/hostname
passwd
exit

Inside the chroot you are root, so no sudo is needed, and every change lands in the target tree, not on your host. gpiod gives you the gpioget and gpioset tools for the GPIO work later in the module. Setting the hostname and a root password are typical product steps; a serial console login and an SSH server are how you will reach the board.

For a small starting point, add --variant=minbase. The default variant installs the standard priority packages; minbase installs only the essential and required packages plus apt, the smallest official base. As an example from a real run (debootstrap 1.0.134ubuntu2 on Ubuntu 24.04, --arch=amd64 --variant=minbase bookworm, about 49 seconds with a fast link), the finished tree held 88 packages, du -sh reported 206M, 31M of that was the downloaded .deb cache under /var/cache/apt, and debootstrap had written the single line deb http://deb.debian.org/debian bookworm main to /etc/apt/sources.list. Your numbers will differ with the mirror and the date. Trim the caches afterwards, inside the chroot, because they are dead weight on the target:

bash
apt clean
rm -rf /var/lib/apt/lists/*

Now the comparison the lab asks for. A Buildroot image is compiled from source on your host with a C library you choose, and it carries no package manager at all: the target cannot install anything after it is flashed. A Debian rootfs carries glibc, dpkg with its package database, and apt, so it is much larger, but the target can install and upgrade packages for its whole life. Measure both with du -sh and the gap is stark: a minimal Buildroot image can be only a few megabytes, while even a minbase Debian tree is far larger. What you buy with those megabytes is the update path: apt upgrade on the device versus rebuilding and reflashing the whole image, a trade the update lessons later in this module revisit.

Buildroot image versus Debian rootfsBuildrootimageDebian rootfsBuilt fromsource, onhostprebuilt .debpackagesTypical sizea few MiBmuch largerPackage databasenonedpkg statusfileC libraryyour choiceglibcField updatesrebuild andreflashapt upgrade
A Buildroot image and a Debian rootfs compared on build method, size, package database and update path
Script every customisation

Do the chroot steps once by hand to learn them, then move them into a shell script that runs debootstrap, copies files and installs packages. A rootfs you can only rebuild by remembering commands is a rootfs you cannot maintain.

03

Reproducibility, LTS and security updates

4 min read

Run the same debootstrap command six months apart and you get different systems: Debian publishes point releases and security updates continuously, so the mirror's package versions move. That is a feature for a running machine and a problem for a product build. Three pins fix it. First, pin the suite: bookworm, never stable, because the stable alias jumps to a new release every couple of years. Second, pin the mirror content with a timestamped archive from snapshot.debian.org, which keeps every package version Debian ever published:

bash
sudo debootstrap --arch=amd64 bookworm "$HOME/rootfs" \
  https://snapshot.debian.org/archive/debian/YYYYMMDDTHHMMSSZ/

Replace the placeholder with a timestamp that exists in the archive (the snapshot site lists them). Third, record what you got: inside the finished rootfs, dpkg-query -W -f='${Package} ${Version}\n' > manifest.txt writes the exact version of every installed package. Commit the build script, the debootstrap version (dpkg-query -W debootstrap on the host), the snapshot timestamp and the manifest to Git. A rebuild then produces the same package versions, which is what matters for behaviour and for tracking which CVEs affect you; byte-identical images are a stronger property that timestamps and file ordering can still break. Tools wrap this workflow in recipes: debos describes an image in YAML with a debootstrap action, and Isar drives Debian image builds with BitBake. Both sit on the same two-stage mechanism you now know.

Security support is the other half of "supported". debootstrap writes a sources.list containing only the mirror you gave it, with no security repository, so add one:

bash
echo "deb http://deb.debian.org/debian-security bookworm-security main" \
  | sudo tee -a "$HOME/rootfs/etc/apt/sources.list"

With that line in place, apt update && apt upgrade inside the chroot (at build time) or on the device (in the field) pulls security fixes, and the unattended-upgrades package can apply them automatically. This is the practical payoff of the binary-distribution model: a fix for OpenSSL reaches a thousand deployed devices without you rebuilding anything.

The fixes only exist while the release is supported. Debian's security team supports each release for roughly three years, then the LTS team extends security updates to five years total. Bookworm (Debian 12, released June 2023) is in LTS until June 2028; trixie (Debian 13) became stable in August 2025 and its window runs years further. Beyond five years, commercial ELTS offerings continue for some releases. The engineering rule: pick a release whose support window covers your product's planned lifetime, and diary the end date before you ship.

The pins that make a rootfs build repeatablePin suitebookworm,not stableSnapshotmirrortimestampedarchiveRecordmanifestdpkg-query-WScript inGitcommandsandversionsRebuildanytimesamepackageversionsPin suitebookworm,not stableSnapshotmirrortimestampedarchiveRecordmanifestdpkg-query-WScript inGitcommandsandversionsRebuildanytimesamepackageversions
The pins that make a debootstrap build repeatable: suite, snapshot mirror, manifest and a script in Git
Never ship an end-of-life suite

A product built on a suite that has left LTS receives no security fixes at all, and no amount of apt configuration changes that. Check the Debian LTS wiki for the release's end date during design, not after a vulnerability announcement.

Every .deb you pulled in carries a licence, and a minbase tree alone contains hundreds of them. Shipping those binaries in a product creates obligations you must be able to meet. The next lesson, "Open-source licences and GPL obligations", answers what you owe when the distribution's packages become your product's firmware.

Key takeaways

  • debootstrap downloads prebuilt .deb packages and unpacks them in two stages: unpack on the host, configure inside a chroot
  • A foreign-architecture rootfs needs --foreign, qemu-user-static, and a manual --second-stage inside the chroot
  • Plain debootstrap is not reproducible because mirrors move; a snapshot.debian.org timestamp plus a package manifest pins it
  • Debian gives roughly three years of security support plus LTS to five years total; bookworm LTS runs to June 2028
Reflect
  1. Explain to a colleague, in two sentences: build a minimal Debian rootfs with debootstrap on Ubuntu 24.04, including a foreign-architecture build with qemu
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close