LinuxCommandLibrary

machinectl

Control and inspect virtual machines/containers

TLDR

Start a machine as a service using systemd-nspawn

$ sudo machinectl start [machine_name]
copy

Stop a running machine
$ sudo machinectl stop [machine_name]
copy

Display a list of running machines
$ machinectl list
copy

Open an interactive shell inside the machine
$ sudo machinectl shell [machine_name]
copy

SYNOPSIS

machinectl [OPTIONS...] COMMAND [MACHINE...]

Common commands include: list, show, start, stop, poweroff, reboot, login, shell, copy-to, pull-tar, import-tar, export-tar, unregister, clean.

PARAMETERS

-a, --all
    Show all machines or images, including those that are stopped or unregistered.

-l, --full
    Do not abbreviate output, showing full unit names or paths.

-H, --host=USER@HOST
    Operate on a remote host via SSH. This option can be specified multiple times.

-M, --machine=NAME
    Execute a command on a specific machine, similar to login or shell without entering a full session.

--no-pager
    Do not pipe output into a pager.

--no-legend
    Do not print the column headers in table output.

--json=MODE
    Output in JSON format. Modes include 'pretty', 'short', 'compact', 'oneline'.

-p, --property=NAME
    Show only specified properties when using the 'show' command. Can be specified multiple times.

-f, --force
    Force an operation, e.g., when unregistering a machine or performing a potentially destructive action.

-q, --quiet
    Suppress output messages, useful for scripting.

DESCRIPTION

machinectl is a command-line utility designed for controlling and introspecting virtual machines and containers managed by systemd. It communicates with the systemd-machined.service to provide a unified interface for various container technologies such as systemd-nspawn, LXC, and even integration with virtualization solutions like QEMU. Users can employ machinectl to list active machines, inspect their properties, start, stop, power off, reboot, and log into a specific machine. Furthermore, it facilitates file transfer operations between the host and machines, as well as managing machine images. Its primary objective is to streamline the management of lightweight virtualized environments directly within the systemd ecosystem.

CAVEATS

machinectl relies on the systemd-machined.service being active and running. Many operations, especially those involving system-wide changes or direct machine manipulation, typically require root privileges.

While machinectl can manage various container types, its deepest integration and most seamless functionality are often with systemd-nspawn containers. Integration with other technologies like LXC or QEMU might require additional setup or specific configurations. File transfer operations (copy-to, copy-from) depend on the guest machine running a compatible systemd version that can communicate with the host's systemd-machined.

MACHINE IMAGE MANAGEMENT

machinectl provides powerful capabilities for managing container images. Users can pull-tar or pull-raw images from remote sources, and import-tar or import-raw local image files. Similarly, images can be export-tar or export-raw for backup or transfer, facilitating easy deployment and replication of containerized environments.

DIRECT MACHINE ACCESS

The command offers convenient ways to interact directly with running machines. The login command allows users to open an interactive login session inside a specified machine, while shell executes a command directly within the machine's environment, similar to SSH but for local containers and VMs.

SYSTEMD INTEGRATION

Deeply integrated with the systemd ecosystem, machinectl leverages systemd's robust cgroup management for resource control and isolation of machines. It also utilizes journald for centralized logging, making it easy to inspect logs from individual machines using journalctl.

HISTORY

machinectl was introduced as an integral part of the systemd project, specifically designed to extend systemd's capabilities to manage lightweight container environments. Its development paralleled that of systemd-nspawn, providing a cohesive management interface for virtualized systems. The command emerged as a means to unify the control of containers and VMs within the broader systemd framework, leveraging its existing cgroup and service management infrastructure to offer a consistent approach to system virtualization.

SEE ALSO

systemd-nspawn(1), systemd-machined.service(8), systemctl(1), journalctl(1)

Copied to clipboard