Creating Sandboxes with systemd-nspawn and debootstrap
Exploring new #Linux features is exciting, but it can be risky! I sometimes break my system while testing packages. To avoid this, I recently tried #systemd-nspawn with #debootstrap - it's a lightweight #container that works well for isolated testing.
#Debian users, this guide shows you how to get systemd-#nspawn up and running, no fuss.
Installing the packages
First things first, we need to install two packages: systemd-container
and debootstrap
:
sudo apt install systemd-container debootstrap
debootstrap lets you spin up a lightweight Debian right on your host, and systemd-container utilites such as systemd-nspawn and machinectl manage the OS in a lightweight container.
Create a Debian virtual machine
Let's generate a minimal Debian image called debian-testing with the following command:
sudo debootstrap --include=systemd,dbus stable /var/lib/machines/debian-testing
To verify successful installation, run machinectl list-images
. Look for 'debian-testing' in the output.
Logging into virtual machine
Use the following command to start the debian-testing container.
sudo systemd-nspawn -D /var/lib/machines/debian-testing
Since you're now inside your virtual machine, let's set a password for the root user. This will come in handy when you want to manage the container using machinectl.
To swiftly terminate the container, press the Ctrl
+]
key combination three times in quick succession while inside the container.
Running a graphical application in vm
To run graphical apps like Chromium within the container, we need to set up display sharing. First, gracefully shut down the container. Then, use this command to establish the connection:
xhost local:; sudo systemd-nspawn -E DISPLAY="$DISPLAY" -D /var/lib/machines/debian-testing
Now that you're logged in, it's time to fire up Chromium! Just type the following commands to install and open it:
apt update
apt install chromium
chromium --no-sandbox
References