systemd-nspawn is the container tool already on every modern Linux host. It ships as part of systemd itself, integrates with machinectl for lifecycle, integrates with journalctl --machine= for logs, and runs a full system container with a one-line invocation. For one-off development VMs, golden-image testing, and the kind of “spin up a sealed Ubuntu rootfs and try something” need that Docker doesn’t fit, it is the right pick.
How to verify
The tool is present whenever systemd is, but the machinectl helper and systemd-container package are not always installed by default:
systemd-nspawn --version
which machinectl
apt list --installed 2>/dev/null | grep systemd-container
ls /var/lib/machines/
machinectl list
/var/lib/machines/ is the standard image store. machinectl list shows running containers; machinectl list-images shows available rootfs images.
What’s happening
systemd-nspawn creates a chroot-on-steroids: a new mount namespace, PID namespace, UTS namespace, and (with -U) user namespace, then execs the target’s init system inside. By default the target’s PID 1 is systemd itself — your container boots like a real machine, with journal, units, and systemctl working inside. nspawn then proxies the inner journal back to the host so journalctl --machine=mycontainer shows the inner logs.
This is the simplest container model: no daemon, no image format conversion, no registry. You point at a rootfs directory (or a .raw btrfs/.tar.xz image), and it boots. The trade-off is no built-in networking beyond a host-bridge join or a private veth, no resource limits unless you wrap with systemd-run --scope, and no public image ecosystem — you bring your own rootfs.
Pair with machinectl pull-tar or machinectl pull-raw to fetch from a curated image source, or use debootstrap/mkosi to build your own rootfs locally. Pair with systemd-portable for the “portable service” pattern (a sealed app delivered as a .raw image and attached to the host).
The procedure
- Install
systemd-containerif missing —machinectlandsystemd-nspawnlive in this package on Debian/Ubuntu:
sudo apt-get install -y systemd-container debootstrap
- Build a minimal Ubuntu rootfs with debootstrap. This is your container image:
sudo mkdir -p /var/lib/machines/web1
sudo debootstrap --include=systemd,dbus,iproute2,iputils-ping \
noble /var/lib/machines/web1 \
http://archive.ubuntu.com/ubuntu/
- Set a root password so you can log in, then boot the container into systemd as PID 1:
sudo systemd-nspawn -D /var/lib/machines/web1 passwd
sudo systemd-nspawn -b -D /var/lib/machines/web1
The -b flag means “boot — execute the container’s /sbin/init”. You will land at the container’s login prompt. Press Ctrl + ]]] (three times) to detach without stopping it.
- Manage the container as a long-running unit.
machinectl startcallssystemd-nspawn@.service:
sudo machinectl start web1
machinectl list
machinectl shell web1
machinectl status web1
sudo machinectl stop web1
- Give the container its own network namespace and a host-side veth pair. By default nspawn shares the host’s network namespace, which is rarely what you want:
sudo mkdir -p /etc/systemd/nspawn
cat | sudo tee /etc/systemd/nspawn/web1.nspawn <<'EOF'
[Network]
Private=yes
VirtualEthernet=yes
EOF
sudo machinectl restart web1
ip link show | grep ve-web1
The container now has its own routing table; you bridge ve-web1 into your network setup as needed.
- Apply a resource limit via systemd’s transient unit. Unlike LXC, nspawn does not embed resource limits in its config — you set them through the systemd slice the container belongs to:
sudo systemctl set-property systemd-nspawn@web1.service \
MemoryMax=2G CPUQuota=200%
sudo systemctl daemon-reload
Common pitfalls
- Running
systemd-nspawn -D /pathwithout-band getting just a shell in the rootfs — useful for one-off commands, useless for long-running services.-bis what boots the container. - Forgetting that nspawn containers share the host’s clock and
/proc/sysby default — kernel sysctls inside the container modify the host’s. Use--read-only --tmpfs=/proc/sysif that is unwanted. - Treating
/var/lib/machines/as if it were/var/lib/docker/— there is no garbage collection, no layer dedup, and no daemon to track in-use images. Clean up manually. - Trying to run nspawn rootless. It cannot — the namespace setup requires capabilities that user namespaces alone do not grant. For rootless, use Podman instead.
- Mixing privileged (
-Uabsent) and unprivileged (-Upresent) containers from the same image. The UID shifts mean the filesystem ownership stops making sense across the two modes.
Stack Harbor uses systemd-nspawn on hosts where a separate container runtime would be redundant — see managed operations for how we use nspawn for golden-image build hosts, portable-service deployments, and short-lived isolation boundaries within a larger systemd estate.