Background & Why Use systemd-nspawn Instead of Docker?
Whenever you want to tinker with a new Linux distribution or test unfamiliar software, two approaches usually come to mind: spinning up a KVM/VirtualBox virtual machine or pulling a Docker image. However, both options have drawbacks. Virtual machines consume gigabytes of RAM and take up to a minute to boot. Meanwhile, Docker is designed around the OCI standard for microservices: running only a single process without a full init system (systemd).
This is where systemd-nspawn shines as the perfect middle ground. Built directly into core systemd, it lets you spawn isolated OS containers. It leverages namespaces and cgroups to share the host kernel just like regular containers, but boots a complete init process tree just like a real virtual machine. Managing it is equally effortless thanks to machinectl — the bundled CLI featuring an intuitive syntax similar to Docker.
On my ThinkPad running Fedora 41, an nspawn container uses only about 35–45MB of idle RAM and boots in just 1.8 seconds. Need a clean sandbox to test an RPM build or debug a service without cluttering your host system? A single command gives you a fresh environment instantly.
Installing the Required Tools on Fedora
Both systemd-nspawn and machinectl are packaged inside systemd-container. To support bootstrapping Debian or Ubuntu later on, you should also install debootstrap.
Open your terminal and run:
sudo dnf install -y systemd-container debootstrap
Next, check the status of systemd’s VM and container management daemon:
sudo systemctl status systemd-machined
If the service is inactive, enable and start it right away:
sudo systemctl enable --now systemd-machined
Creating and Running Containers
1. Bootstrap a Minimal Root Filesystem (Rootfs)
The standard directory for machinectl to automatically detect containers is /var/lib/machines/. You can use the host’s dnf package manager to bootstrap a minimal Fedora rootfs directly into this directory:
# Create a container directory named fedora-sandbox
sudo mkdir -p /var/lib/machines/fedora-sandbox
# Bootstrap a minimal rootfs (downloads ~180MB packages, extracts to ~650MB disk space)
sudo dnf --installroot=/var/lib/machines/fedora-sandbox \
--releasever=41 \
--disablerepo='*' \
--enablerepo=fedora,updates \
install -y systemd passwd dnf bash coreutils vim-minimal
After just 2 to 3 minutes of downloading packages, you have a complete Fedora rootfs directory tree ready to boot.
2. Set the Root Password Before Booting
The newly installed OS does not have any usable accounts yet. Quickly chroot into the container using systemd-nspawn to set a password for the root user:
sudo systemd-nspawn -D /var/lib/machines/fedora-sandbox passwd
Enter and confirm the new password when prompted in the terminal.
3. Boot the Container with a Full Init System
To boot the container with a full systemd init system (PID 1) rather than just spawning an isolated shell, pass the -b (or --boot) flag:
sudo systemd-nspawn -b -D /var/lib/machines/fedora-sandbox
The familiar [ OK ] Started... boot logs will fly by in seconds. Once the login prompt appears, simply enter root and the password you just configured.
To exit the container console and return to your host system, press the key combination: Ctrl + ] three times quickly (within one second).
4. Configure Networking and Run the Container as a Service
Starting containers manually in a terminal is inconvenient. The standard approach is to run the container in the background as a systemd service and provision a virtual network interface (Virtual Ethernet — veth).
Create a configuration file at /etc/systemd/nspawn/fedora-sandbox.nspawn:
sudo mkdir -p /etc/systemd/nspawn
sudo tee /etc/systemd/nspawn/fedora-sandbox.nspawn <<EOF
[Exec]
Boot=yes
[Network]
VirtualEthernet=yes
EOF
This configuration automatically creates a host0 interface inside the container and bridges it with the ve-fedora-sandbox interface on the host. Now, simply enable and start the service:
sudo systemctl enable --now [email protected]
Managing and Monitoring Containers with machinectl
Once the container is running in the background, machinectl serves as a powerful replacement for the Docker CLI.
1. List Active Containers
machinectl list
The output displays the container status and IP addresses (typically IPv4LL or DHCP leased from the host):
MACHINE CLASS SERVICE OS VERSION ADDRESSES
fedora-sandbox container systemd-nspawn fedora 41 169.254.112.85...
2. Accessing the Container Shell
Without opening any SSH ports, you can access the container directly from the host in two ways:
# Open an independent shell session (equivalent to docker exec -it ... bash)
machinectl shell fedora-sandbox
# Or attach to the virtual console TTY login
machinectl login fedora-sandbox
3. Monitoring CPU, RAM, and cgroups
Quickly inspect resource utilization and active services:
machinectl status fedora-sandbox
To inspect real-time CPU and RAM consumption per process inside the container (similar to htop for cgroups), run:
systemd-cgtop /machine.slice/machine-fedora\x2dsandbox.scope
4. Stopping and Terminating Containers
When finished, you can safely shut down the container:
# Send a graceful shutdown signal (via SIGINT/systemd)
machinectl poweroff fedora-sandbox
# Force terminate immediately if the container hangs
machinectl terminate fedora-sandbox
Using only native built-in system tools, you get a clean, isolated sandbox that boots in under 2 seconds, consumes less than 50MB of RAM, and completely frees you from depending on the Docker daemon.
