LinuxCommandLibrary

virsh

Manage virtual machines from the command line

TLDR

Connect to a hypervisor session

$ virsh connect [qemu:///system]
copy

Activate a network named default
$ sudo virsh net-start [default]
copy

List all domains
$ virsh list --all
copy

Create a guest from a configuration file
$ virsh create [path/to/config_file.xml]
copy

Edit a guest's configuration file (editor can be changed with $EDITOR)
$ virsh edit [guest_id]
copy

Start/reboot/shutdown/suspend/resume a guest
$ virsh [command] [guest_id]
copy

Save the current state of a guest to a file
$ virsh save [guest_id] [filename]
copy

Delete a running guest
$ virsh destroy [guest_id] && virsh undefine [guest_id]
copy

SYNOPSIS

virsh [OPTION...] COMMAND [ARG...]

PARAMETERS

-c URI
    Connects to the specified hypervisor URI (e.g., qemu:///system for local KVM).

-r
    Connects to the hypervisor in read-only mode.

-q
    Suppresses informational output, showing only errors or requested data.

-t
    Prints timing information for operations.

command (e.g., list)
    virsh operates as a shell, executing various subcommands. Here are some of the most commonly used subcommands:

list [--all | --inactive | --active]
    Lists virtual machines (domains). Options filter by status (all, inactive, or active).

start domain
    Starts an inactive virtual machine by its name or ID.

shutdown domain
    Gracefully shuts down a running virtual machine.

destroy domain
    Forcefully stops a running virtual machine, without graceful shutdown.

define file.xml
    Defines a virtual machine from an XML configuration file, but does not start it.

undefine domain
    Removes the definition of an inactive virtual machine from libvirt.

create file.xml
    Creates (defines and starts) a virtual machine from an XML file.

dominfo domain
    Displays detailed information about a specific virtual machine.

net-list [--all]
    Lists virtual networks configured in libvirt.

vol-list pool
    Lists storage volumes within a specified storage pool.

pool-list [--all]
    Lists storage pools managed by libvirt.

capabilities
    Displays the capabilities of the hypervisor and host system.

version
    Displays libvirt, hypervisor, and driver version information.

console domain
    Connects to the serial console of a running virtual machine.

dumpxml domain
    Dumps the XML configuration of a specified virtual machine.

DESCRIPTION

virsh is a powerful command-line interface tool used to manage virtual machines and hypervisors. It acts as the primary interface for the libvirt virtualization management library, providing a unified way to control various virtualization technologies, most notably KVM/QEMU, but also Xen, LXC, and others. Administrators use virsh to perform a wide range of tasks, including creating, starting, stopping, pausing, and deleting virtual machines (domains), managing virtual networks, storage pools, and storage volumes, and monitoring the status of guests and the hypervisor itself.

It supports both interactive and non-interactive modes, making it highly suitable for scripting automation. Its versatility and comprehensive feature set make it an essential tool for virtualization administrators working with libvirt-managed environments.

CAVEATS

  • libvirtd Service: virsh relies on the libvirtd service running on the host. Ensure it's active.
  • Permissions: Many operations require root privileges or membership in the libvirt system group.
  • XML Configuration: Advanced management often necessitates a deep understanding of libvirt's XML format for domain, network, and storage definitions.
  • Connection: Default connection is to the local system (e.g., qemu:///system). Remote management requires proper URI specification and authentication setup.

INTERACTIVE MODE

When invoked without any arguments (simply virsh), the command enters an interactive shell mode, allowing users to type subcommands at a prompt.

SCRIPTING CAPABILITIES

Due to its clear command structure and the ability to suppress verbose output with -q, virsh is exceptionally well-suited for shell scripting and automation of virtualization tasks.

XML CONFIGURATION

Libvirt extensively uses an XML format for defining virtual machines (domains), networks, and storage. virsh commands like define, dumpxml, and edit directly interact with these XML definitions, providing powerful configuration options.

HISTORY

virsh is an integral part of the libvirt project, which was initiated in 2005. Libvirt was conceived to provide a stable and unified API for managing diverse virtualization technologies, abstracting away hypervisor-specific complexities. virsh quickly became the primary command-line interface for interacting with this powerful API. Its development has consistently mirrored the evolution of libvirt itself, continuously expanding its capabilities to support new hypervisors (like KVM/QEMU, Xen), features, and storage backends. Today, it stands as a de facto standard for command-line virtualization management on Linux.

SEE ALSO

qemu-kvm(1), virt-manager(1), brctl(8), ip(8), lxc(1)

Copied to clipboard