How to detect if a system is running inside a systemd-nspawn container

containerssystemd-nspawn

Quite recently I started using systemd-nspawn to set up other OS instances on my Arch box. One thing I'd like to do is detect if I'm inside a container, and if so, add the distro name (from lsb_release) to the terminal title. On Debian-based systems, the default .bashrc uses debian_chroot for a similar purpose. How do I detect if I am running inside a nspawn container?

Best Answer

systemd-detect-virt can tell you whether your system is running in a VM/container. This requires systemd-detect-virt inside your container, but the systemd documentation on minimal builds suggests that you can just build a package that only includes systemd-detect-virt.

Related Question