25.7. Running Debian System Containers
An application container runs a process; a system container boots a
machine. The distinction from Section 25.1.2 becomes concrete here: a
directory tree built by debootstrap, an init system inside, a virtual
ethernet link outside, and machinectl as the front door. System
containers suit work that needs a full Debian userspace - integration
tests against a real systemd, a build environment with many services, a
legacy stack that assumes it owns an operating system - at a fraction of
a virtual machine's cost, because there is no second kernel.
25.7.1. Creating a Debian Root Filesystem with debootstrap
debootstrap installs a Debian base system into an ordinary directory -
the same machinery the Debian installer uses, driven by hand. Install the
tools. On ctr1, as john:
$ sudo apt install debootstrap systemd-container
The command prompts for confirmation. systemd-container provides
systemd-nspawn and machinectl;
debootstrap needs no configuration. Machines live under
/var/lib/machines, which systemd treats as its machine directory; on a
host that keeps machines on their own storage, that path is a mount point
(Chapter 11 covers the layout decision). Build
the tree. On ctr1, as root:
# debootstrap --variant=minbase --include=systemd,systemd-sysv,dbus,iproute2,iputils-ping,ca-certificates trixie /var/lib/machines/handbookctr http://deb.debian.org/debian
I: Target architecture can be executed
I: Retrieving InRelease
I: Checking Release signature
I: Valid Release signature (key id 41587F7DB8C774BCCF131416762F67A0B2C39DE4)
I: Retrieving Packages
I: Validating Packages
I: Resolving dependencies of required packages...
I: Resolving dependencies of base packages...
I: Checking component main on http://deb.debian.org/debian...
I: Retrieving base-files 13.8+deb13u8
I: Validating base-files 13.8+deb13u8
I: Retrieving systemd 257.13-1~deb13u1
I: Validating systemd 257.13-1~deb13u1
I: Retrieving iproute2 6.15.0-1
I: Validating iproute2 6.15.0-1
I: Retrieving iputils-ping 3:20240905-3
I: Validating iputils-ping 3:20240905-3
I: Unpacking the base system...
I: Unpacking adduser...
I: Unpacking ca-certificates...
I: Unpacking dbus...
I: Unpacking iputils-ping...
I: Unpacking systemd-sysv...
I: Configuring the base system...
I: Configuring iproute2...
I: Configuring libc-bin...
I: Configuring ca-certificates...
I: Base system installed successfully.
Selected output; the full log lists every retrieved package and every configure step, and the middle entries are omitted. The signature check at the top is the archive trust model of Chapter 6 in miniature: the Release file was verified against the Debian archive keyring before any package was accepted.
| Option | Description |
|---|---|
--variant=minbase |
the smallest usable base: apt, dpkg, and the essentials, no priority-heavy package set |
--include=LIST |
install additional packages alongside the variant (systemd and its deps, D-Bus, and the network tools) |
trixie |
the suite to install - Debian 13 |
/var/lib/machines/handbookctr |
the target directory; it is created |
http://deb.debian.org/debian |
the mirror to fetch from |
The --include list is what turns a bare tree into a bootable machine:
systemd and systemd-sysv provide PID 1, dbus the bus it needs,
iproute2 and iputils-ping the network tools the exercises use, and
ca-certificates the trust store for any later HTTPS. The finished tree
is a complete Debian root filesystem, inspectable with ordinary tools -
ls /var/lib/machines/handbookctr shows a normal /etc, /usr, /var.
Before it can boot, it needs a name and a network configuration.
25.7.2. Booting and Managing It with systemd-nspawn
The machine's identity and its inside-the-machine settings are plain files in the tree. Set the hostname and hosts entries. On ctr1, as root:
# printf 'handbookctr\n' > /var/lib/machines/handbookctr/etc/hostname
# printf '127.0.0.1 localhost\n127.0.1.1 handbookctr\n' > /var/lib/machines/handbookctr/etc/hosts
Both writes produce no output; the redirections build the two files.
Enable systemd-networkd inside the machine - the tree has no
systemctl state yet, so the enable is made with symlink, exactly the
mechanism systemctl enable performs on a running system. On ctr1, as
root:
# ln -sf /usr/lib/systemd/system/systemd-networkd.service /var/lib/machines/handbookctr/etc/systemd/system/multi-user.target.wants/systemd-networkd.service
# ln -sf /usr/lib/systemd/system/systemd-networkd.socket /var/lib/machines/handbookctr/etc/systemd/system/sockets.target.wants/systemd-networkd.socket
The nspawn configuration lives beside the machine, in
/etc/systemd/nspawn/handbookctr.nspawn:
[Exec]
PrivateUsers=no
Boot=yes
[Network]
VirtualEthernet=yes
[Files]
Bind=/srv/machine-data:/srv/data
| Section | Setting | Effect |
|---|---|---|
[Exec] |
Boot=yes |
run systemd as PID 1 inside, not a single command |
[Exec] |
PrivateUsers=no |
do not place the machine in a user namespace; see below |
[Network] |
VirtualEthernet=yes |
create a host-to-machine virtual ethernet pair |
[Files] |
Bind= |
bind a host directory into the machine |
PrivateUsers=no pins the machine out of a user namespace so that the
root-owned bind mount is writable by the machine's root. That is a
deliberate lab trade: with user namespaces enabled - the more confined
posture, and the right one for a machine that might run untrusted
software - the machine's root maps to an unprivileged host UID range, and
a bind-mounted tree owned by host root is not writable unless its
ownership is mapped to that range first; systemd-nspawn can remap the
tree at creation time, at the cost of a one-time chown over every file.
Choose per machine: untrusted work gets the namespace and the remapping;
a trusted integration machine can skip both, as here.
Prepare the bind-mounted data - host-side, as an ordinary directory. On ctr1, as root:
# install -d -m 755 /srv/machine-data
# printf 'persistent machine data\n' > /srv/machine-data/state.txt
One more set of files belongs to the first boot: the machine's
20-host0.network, the host-side address helper
/usr/local/sbin/handbookctr-network, and its service drop-in
/etc/systemd/system/systemd-nspawn@handbookctr.service.d/network.conf.
Their contents are shown and explained in Section 25.7.3; install them
now - the network file into the machine tree at
/var/lib/machines/handbookctr/etc/systemd/network/20-host0.network, the
helper under /usr/local/sbin/, the drop-in under
/etc/systemd/system/systemd-nspawn@handbookctr.service.d/ - and make
the helper executable:
# chmod 0755 /usr/local/sbin/handbookctr-network
| Subcommand | Effect |
|---|---|
machinectl start NAME |
boot the machine (an nspawn unit, generated from the .nspawn file) |
machinectl list |
list running machines with their addresses |
machinectl stop NAME |
shut the machine down |
machinectl shell NAME |
open an interactive shell inside, as a login session |
Reload systemd so the nspawn template picks up the configuration, then boot the machine. On ctr1, as root:
# systemctl daemon-reload
# machinectl start handbookctr
Both commands produce no output; the start returns once the machine's init is up. On ctr1, as root:
# machinectl list
MACHINE CLASS SERVICE OS VERSION ADDRESSES
handbookctr container systemd-nspawn debian 13 10.88.0.2…
1 machines listed.
The machine runs under the systemd-nspawn service, Debian 13 is what it
booted, and its address comes from the network files installed a moment
ago - the mechanism is the next section's subject. Running commands
inside the machine noninteractively is systemd-run with -M; the next
section puts the four options to work in a single inspection command:
| Option | Effect |
|---|---|
-M NAME |
run in the named machine |
--pipe |
connect the command's standard streams to this terminal |
--wait |
wait for the command and pass its exit status through |
--collect |
clean up the transient unit afterward |
For interactive work, machinectl shell handbookctr opens a login shell
in the machine; leave it with exit or Ctrl-D, the same way any shell is
left. The noninteractive form is what scripts and this chapter's
exercises use, because it needs no terminal of its own.
25.7.3. Machine Networking and Persistent Storage
With VirtualEthernet=yes, nspawn creates a point-to-point pair: the
machine-side interface is always host0, and the host side is named
ve-<machine>. The pair is connected but unconfigured - systemd does
not invent addresses - so both ends need one. Inside the machine,
systemd-networkd takes the machine side; this is the
20-host0.network file installed before the first boot:
[Match]
Name=host0
[Network]
Address=10.88.0.2/24
A static address on host0, nothing else - the machine talks to its host
and, with routing added on the host, through it. The host side is where a
plain network configuration would not survive: the interface is created
when the machine boots and vanishes when it stops, so the address must be
applied at boot time by the machine's own service unit. A small script,
/usr/local/sbin/handbookctr-network:
#!/bin/sh
set -eu
case "${1:-}" in
up)
for _ in 1 2 3 4 5; do
[ -e /sys/class/net/ve-handbookctr ] && break
sleep 1
done
ip link set ve-handbookctr up
ip addr replace 10.88.0.1/24 dev ve-handbookctr
;;
down)
ip addr del 10.88.0.1/24 dev ve-handbookctr 2>/dev/null || true
;;
*)
echo "usage: $0 up|down" >&2
exit 2
;;
esac
The up path waits briefly for the host-side interface to appear, brings
it up, and assigns 10.88.0.1/24; the down path removes the address,
tolerating an interface that has already vanished - the one place a
discarded error is correct, because the pair's teardown races the unit
stop. The unit drop-in,
/etc/systemd/system/systemd-nspawn@handbookctr.service.d/network.conf,
calls the script from the machine's service:
[Service]
ExecStartPost=/usr/local/sbin/handbookctr-network up
ExecStopPost=/usr/local/sbin/handbookctr-network down
ExecStartPost runs once nspawn has created the pair, ExecStopPost
when it is about to disappear; the machine's service unit is the one
lifecycle both events share. With the machine running, inspect the whole
picture in one command: name, address, mounted data, and reachability
of the host end. On ctr1, as root:
# systemd-run -M handbookctr --pipe --wait --collect /bin/sh -c 'hostname; ip -br a show host0; cat /srv/data/state.txt; ping -c1 -W2 10.88.0.1'
Running as unit: run-p18642-i18942.service
handbookctr
host0@if15 UP 10.88.0.2/24 fe80::90c1:a2ff:fe2e:506e/64
persistent machine data
PING 10.88.0.1 (10.88.0.1) 56(84) bytes of data.
64 bytes from 10.88.0.1: icmp_seq=1 ttl=64 time=0.094 ms
--- 10.88.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.094/0.094/0.094/0.000 ms
Finished with result: success
Selected output; systemd-run's accounting lines are truncated. Four facts
in one transcript: the machine knows its name (handbookctr), its host0
holds 10.88.0.2/24 and is up, the bind mount delivered the host's
state.txt at /srv/data, and the host's end of the pair answers at
10.88.0.1. The machine can now reach its host; giving it reach beyond
that is ordinary host routing - a forward rule and NAT for the lab
network, per Chapter 18 and
Chapter 19 - none of which the exercise needs.
Persistence across the machine's lifecycle is the last property to verify. Write state from inside. On ctr1, as root:
# systemd-run -M handbookctr --pipe --wait --collect /bin/sh -c 'printf persisted-machine-state > /srv/data/saved.txt; cat /srv/data/saved.txt'
Running as unit: run-p18678-i18978.service
persisted-machine-state
Selected output; the success and accounting lines are omitted as before.
The machine wrote persisted-machine-state through the bind mount and read
it back in the same command. Stop the machine, confirm the list is empty,
then boot it again. On ctr1, as root:
# machinectl stop handbookctr
# machinectl list
No machines.
# machinectl start handbookctr
The stop produces no output beyond the empty listing; the start is silent. Read the data back. On ctr1, as root:
# systemd-run -M handbookctr --pipe --wait --collect /bin/sh -c 'cat /srv/data/saved.txt; hostname'
Running as unit: run-p18773-i19073.service; invocation ID: 9fcc3fbf22b44999b3f5577e54445c65
persisted-machine-statehandbookctr
Selected output; the accounting lines are omitted as before. The file
survived the stop/start cycle, because it was written through
the bind mount to /srv/machine-data on the host - the machine's own
tree could be deleted and rebuilt by debootstrap without losing it.
That is the system-container version of the volume rule from
Section 25.3.2: state that matters lives outside the thing that gets
replaced, whether the thing is an image or an entire root filesystem.
25.7.4. Choosing System Containers for Integration Work
The decision is "how much of an operating system does this workload need, and how much do you trust it?" - the same two questions as for application containers, with different thresholds.
| Workload | Better fit | Why |
|---|---|---|
| One service, image-distributed, updated by redeployment | Application container (Section 25.2) | seconds to start, replaceable, no OS to maintain |
| Debian userland integration: package builds, systemd unit tests, service stacks that assume a machine | System container | full apt tree and init at container cost |
| Untrusted code, custom kernels, kernel modules, foreign filesystems | Virtual machine (Chapter 24) | hardware-level isolation survives a kernel bug |
| Multi-host orchestration, scaling, rolling updates | Cluster (Chapter 40) | scheduling and reconciliation are the problem being solved |
The system container's sweet spot is the middle row: work that needs a
real Debian - a dpkg-buildpackage run with many build dependencies, a
migration rehearsal that installs and configures a whole service stack, a
legacy application that starts three daemons and expects to own
/etc. Inside a system container those run exactly as on a installed
host, with apt upgrades (Chapter 6) and
service management (Chapter 8) working unchanged,
while the host keeps the machine's disk as one directory it can snapshot,
archive, or delete. The costs are equally concrete: a kernel panic or a
kernel privilege escalation on the host reaches every machine, the
machine's kernel is the host's choice, and - as with all containers -
what you gain over a virtual machine is paid for with a shared kernel.