How to Manage Containers and Virtual Machines in Linux Using machinectl

Modern Linux servers and workstation development environments frequently leverage OS-level virtualization through lightweight containers and full hardware virtualization via KVM (Kernel-based Virtual Machine) hypervisors. While higher-level orchestration platforms such as Docker, Podman, and Kubernetes dominate cloud container workloads, the native Linux system architecture includes an integrated, lightweight container and virtual machine manager coordinated by systemd-machined.

The native command-line utility machinectl serves as the primary management interface for inspecting, monitoring, and controlling containers registered with systemd-machined, including systemd-nspawn containers, libvirt KVM virtual machines, and OpenVZ environments. With machinectl, administrators can spawn interactive container shells, transfer files between host and guest environments, query resource utilization, and clone OS images directly without requiring external container runtimes.

Understanding systemd-machined and machinectl Architecture

The systemd-machined.service daemon tracks running virtual machines and OS containers as distinct system entities. Unlike raw process IDs or background services, registered machines are allocated their own dedicated control group (cgroup) slice under machine.slice.

When an OS container boots via systemd-nspawn or a virtual machine launches via libvirt, the runtime registers the guest with systemd-machined. This registration provides:

  • Unified Process Isolation: Guest processes are isolated inside dedicated cgroups, allowing administrators to apply systemd resource limits (CPU quotas, memory caps, I/O bandwidth) dynamically.
  • Direct IPC Access: Administrators can establish interactive pseudo-terminal (PTY) shell sessions into guests over D-Bus without running SSH daemons inside the container.
  • Image and Storage Management: Native manipulation of raw disk images, directory trees, Btrfs subvolumes, and QCOW2 files located in /var/lib/machines/.

Listing Active and Inactive Virtual Machines and Containers

To inspect all currently running containers and virtual machines on your Linux host, open a terminal and execute the primary list command:

machinectl list

The command outputs a structured table detailing active guests:

MACHINE    CLASS     SERVICE        OS     VERSION ADDRESSES
web-stage  container systemd-nspawn ubuntu 24.04   192.168.100.15
db-prod    vm        qemu           debian 12      192.168.100.20

The CLASS column distinguishes between OS containers (container) and hypervisor virtual machines (vm). To list all installed guest images available on disk, including those not currently running, execute:

machinectl list-images

Inspecting Machine Status, Control Groups, and Properties

To view deep diagnostic information about an active machine—including its root directory, initial PID, memory consumption, IP addresses, and cgroup path—run the status command:

machinectl status web-stage

The output reports the complete process tree executing inside the guest container:

web-stage(8c37d4f9b8c2415bb63e4811a2f901c2)
           Since: Thu 2026-11-05 09:12:04 UTC
          Leader: 41285 (systemd)
         Service: systemd-nspawn; class container
            Unit: machine-web\x2dstage.scope
                  |-41285 /lib/systemd/systemd
                  `-system.slice
                    |-nginx.service
                    | `-41310 nginx: master process
                    `-systemd-journald.service
                      `-41295 /lib/systemd/systemd-journald

Opening an Interactive Shell Inside a Running Container

One of the most practical administrative capabilities of machinectl is the ability to open an interactive root shell inside a running container without configuring network access or installing an SSH server.

To attach a secure, multiplexed PTY shell session directly into the container, execute:

sudo machinectl shell web-stage

You can also open a shell as a specific unprivileged user inside the guest by supplying the user name:

sudo machinectl shell www-data@web-stage

To terminate the interactive shell session, press Ctrl + ] three times within one second, or type exit.

If you need to execute a single non-interactive command inside the guest and capture the output on the host, run:

sudo machinectl shell web-stage /bin/uname -a

Transferring Files Between Host and Guest

Traditional container workflows often require complex volume mounts or SFTP transfers to exchange files. machinectl provides native bidirectional file copying mechanisms between the host and guest filesystems.

To copy a configuration file from the host filesystem into a running container’s /etc/ directory, execute:

sudo machinectl copy-to web-stage /path/to/local/nginx.conf /etc/nginx/nginx.conf

To retrieve a diagnostic log file or database dump from inside the container back to the host filesystem, execute the reverse operation:

sudo machinectl copy-from web-stage /var/log/nginx/error.log /tmp/container_error.log

Cloning, Renaming, and Removing Machine Images

Guest disk images and root directory trees stored in /var/lib/machines/ can be cloned or snapshot instantly, particularly when running on a Btrfs filesystem with copy-on-write capabilities.

To clone an existing base image to create a staging testbed, run:

sudo machinectl clone ubuntu-base web-staging-copy

To mark an image as read-only to prevent administrative corruption, execute:

sudo machinectl read-only ubuntu-base yes

When an image is no longer required, safely remove it from disk using:

sudo machinectl remove web-staging-copy

Controlling Container Lifecycles and Graceful Shutdowns

To manage guest power states without terminating container processes abruptly, machinectl communicates with the guest’s internal init system:

  • Reboot: Instruct the guest to initiate an orderly restart:
    sudo machinectl reboot web-stage
  • Power Off: Signal the guest init system to gracefully shut down daemons and unmount filesystems:
    sudo machinectl poweroff web-stage
  • Terminate (Kill): If a container locks up or fails to respond to ACPI power-off signals, terminate its cgroup slice forcefully:
    sudo machinectl terminate web-stage

By leveraging machinectl, Linux system administrators gain a robust, dependency-free toolkit for inspecting and orchestrating OS containers and KVM virtual machines natively across the enterprise.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.