[EN] What Is Virsh? Tips for Managing Virtual Machines in KVM-Based Virtualization Environments

[EN] What Is Virsh? Tips for Managing Virtual Machines in KVM-Based Virtualization Environments

If you use KVM/QEMU in Linux-based virtualization infrastructures, one of the most common tools you will encounter on the system administration side is the virsh command. Virsh is a powerful command-line tool used to manage virtual machines, virtual networks, storage pools, disk volumes, snapshots, checkpoints, host devices, and hypervisor connections through the libvirt infrastructure.

Virsh should not be considered only as a “virtual machine start and stop command.” This tool provides a very broad management scope, from the virtual machine lifecycle to CPU and memory configuration, disk management, network interface control, backup and migration operations, and QEMU Guest Agent commands.

In the official libvirt documentation, virsh is described as the main interface used to manage guest domains. In this context, the term domain usually means a virtual machine in practical usage. A domain can be managed by its name, ID, or UUID.

The general usage format is as follows:

virsh [OPTION]... <command> <domain> [ARG]...

For example:

virsh list --all
virsh dominfo web01
virsh shutdown web01

In these commands, web01 is the name of the virtual machine to be managed.


1. The Relationship Between Virsh and Libvirt

Behind virsh, there is libvirt. Libvirt is a management layer that allows communication with different virtualization technologies such as KVM/QEMU, Xen, LXC, OpenVZ, VirtualBox, and VMware ESX through a common API.

Thanks to this structure, a system administrator can manage different virtualization infrastructures with a single command-line tool: virsh. In KVM/QEMU environments, virsh mostly communicates with the hypervisor through libvirt daemon services.

The basic flow is as follows:

User -> virsh -> libvirt -> QEMU/KVM -> Virtual Machine

For this reason, libvirt services must be running on the system for virsh commands to work. In the official notes, it is also stated that most virsh operations require a connection to a running libvirtd service, and many commands may require root privileges because of the communication channels used with the hypervisor.


2. Ways to Use Virsh

Virsh can be used in two different ways.

The first method is to run a single command directly from the shell:

virsh list --all

This usage is ideal for scripts, automation, and quick checks.

The second method is to enter the interactive virsh shell:

virsh

Then commands can be executed inside the virsh prompt:

virsh # list --all
virsh # nodeinfo
virsh # quit

To exit interactive mode, use the quit or exit command.


3. Virsh Connection URIs

Virsh can connect to different hypervisor connections using a URI structure. In KVM/QEMU environments, the most commonly used connections are:

virsh -c qemu:///system

This connection is used to manage system-wide QEMU/KVM domains. It is generally preferred in server environments and usually requires root or an authorized user.

virsh -c qemu:///session

This connection is used for QEMU/KVM domains running under a regular user session.

To view the currently connected URI:

virsh uri

Common connection examples listed in the official manpage include xen:///system, qemu:///system, qemu:///session, and lxc:///system.


4. Virsh Global Options

Some global options can be used when running virsh:

OptionDescription
-c, --connect URIConnects to the specified libvirt URI.
-d, --debug LEVELSets the debug level.
-e, --escape stringChanges the escape character for console connections.
-h, --helpDisplays help output.
-k, --keepalive-intervalSets the connection keepalive check interval.
-K, --keepalive-countSets how many unanswered keepalive messages are allowed before the connection is considered lost.
-l, --log FILEWrites log output to a file.
-q, --quietReduces unnecessary informational messages.
-r, --readonlyOpens a read-only connection.
-t, --timingShows the execution time of each command.
--no-pkttyagentDoes not enable the Polkit authentication agent.
-v, --versionShows virsh/libvirt version information.
-V, --version=longShows version information along with build options and driver information.

Examples:

virsh -c qemu:///system list --all
virsh -r list --all
virsh -t dominfo web01
virsh --version
virsh -V

5. Generic Commands

These commands are not directly tied to a single virtual machine. They are generally used for connection management, host information, CPU compatibility, NUMA, hypervisor capabilities, and general virsh session operations. In the official manpage, this command group is listed as “Generic Commands.”

CommandDescription
helpShows all virsh commands or help for a specific command.
quit, exitExits the interactive virsh shell.
versionShows virsh, libvirt, API, and hypervisor version information.
cdChanges the current directory inside the virsh shell.
pwdShows the current working directory.
connectOpens or changes the hypervisor connection.
uriShows the connected hypervisor URI.
hostnameShows the hypervisor host name.
sysinfoShows hypervisor system information in XML format.
nodeinfoShows host CPU, memory, and basic hardware information.
nodecpumapShows the CPU map on the host.
nodecpustatsShows host CPU statistics.
nodememstatsShows host memory statistics.
nodesevinfoProvides information about AMD SEV security features.
nodesuspendPuts the host system into sleep mode.
node-memory-tuneManages KSM/shared memory parameters on the host.
capabilitiesShows hypervisor and host capabilities in XML format.
domcapabilitiesShows domain creation capabilities in XML format.
pool-capabilitiesShows storage pool capabilities.
inject-nmiSends an NMI signal to a domain.
listLists domains.
freecellShows free memory information by NUMA cell.
freepagesShows free page information by NUMA/page size.
allocpagesUsed for hugepage/page allocation operations.
cpu-baselineProduces a common CPU model from multiple CPU definitions.
cpu-compareCompares a given CPU definition with the host CPU.
cpu-modelsLists known CPU models for a specific architecture.
hypervisor-cpu-compareCompares CPU compatibility with hypervisor capabilities.
hypervisor-cpu-baselineProduces a baseline CPU considering hypervisor capabilities.
hypervisor-cpu-modelsLists CPU models known by the hypervisor.

Examples:

virsh help
virsh version --daemon
virsh uri
virsh hostname
virsh nodeinfo
virsh capabilities
virsh list --all
virsh cpu-models x86_64

6. Domain Commands — Virtual Machine Management Commands

The most commonly used part of virsh is the Domain Commands group. In libvirt terminology, a domain usually means a virtual machine in practical usage. These commands are used to start and stop VMs, modify resources, retrieve disk and network information, manage snapshots, backups, migrations, guest agent operations, events, and debugging.


6.1 Lifecycle and Basic VM Management

CommandDescription
startStarts a defined but powered-off domain.
shutdownAttempts a graceful shutdown by communicating with the guest OS.
destroyForces the VM to power off. It should be considered similar to a physical power-off.
rebootReboots the VM.
resetApplies a hardware-like reset to the VM.
suspendPauses a running domain in memory.
resumeResumes a suspended domain.
saveSaves the running VM state to a file.
restoreRestores a VM state previously created with save.
managedsaveSaves the VM state in a libvirt-managed way.
managedsave-removeDeletes the managed save image.
managedsave-defineChanges the managed save state XML definition.
managedsave-dumpxmlShows the managed save XML information.
managedsave-editEdits the managed save XML information.
undefineRemoves the domain definition from libvirt.
autostartConfigures the VM to start automatically when the host boots.
domrenameRenames an inactive domain.

Examples:

virsh start web01
virsh shutdown web01
virsh destroy web01
virsh reboot web01
virsh suspend web01
virsh resume web01
virsh autostart web01
virsh autostart web01 --disable

shutdown attempts a graceful shutdown, while destroy forces the VM to power off. Therefore, in production systems, shutdown should be tried first, and destroy should be used only as a last resort.


6.2 Domain Creation and XML Management

CommandDescription
createStarts a temporary or one-time domain from an XML file.
defineRegisters the domain definition from an XML file in libvirt but does not start it.
dumpxmlShows the domain XML configuration.
editEdits the domain XML configuration with an editor.
descShows or changes the domain description or title.
metadataManages custom XML metadata on the domain.
domxml-from-nativeConverts native hypervisor format to libvirt XML format.
domxml-to-nativeConverts libvirt XML format to native hypervisor format.

Examples:

virsh dumpxml web01 > web01.xml
virsh define web01.xml
virsh create web01.xml
virsh edit web01
virsh desc web01

The define command creates a persistent definition, while create starts a domain from XML. When XML changes are made on a running domain, some settings may only take effect after the next reboot.


6.3 Domain Information and Status Commands

CommandDescription
dominfoShows basic information about a domain.
domstateShows the state of a domain.
domstatsShows CPU, memory, disk, and network statistics.
domidFinds the ID from a domain name or UUID.
domnameFinds the domain name from a domain ID or UUID.
domuuidShows the domain UUID.
domhostnameShows the domain hostname.
domtimeGets or sets guest time information.
domcontrolShows the state of the domain control interface.
domdisplayShows graphical console connection information.
domdisplay-reloadReloads graphical display configuration.
ttyconsoleShows the domain TTY console device.
vncdisplayShows VNC display information.
dommemstatShows domain memory statistics.

Examples:

virsh dominfo web01
virsh domstate web01 --reason
virsh domstats web01
virsh domuuid web01
virsh dommemstat web01
virsh vncdisplay web01

6.4 CPU, Memory, NUMA, and Performance Commands

CommandDescription
cpu-statsShows domain CPU statistics.
vcpucountShows the number of vCPUs on the domain.
vcpuinfoLists vCPU details.
vcpupinPins vCPUs to specific physical CPUs.
setvcpusChanges the number of vCPUs for a domain.
setvcpuEnables or disables specific vCPUs.
maxvcpusShows the maximum number of vCPUs supported by the hypervisor.
emulatorpinPins emulator threads to specific CPUs.
iothreadaddAdds an IOThread to the domain.
iothreaddelRemoves an IOThread from the domain.
iothreadinfoShows IOThread information.
iothreadpinPins an IOThread to a CPU.
iothreadsetChanges IOThread parameters.
memtuneShows or changes domain memory tuning parameters.
setmemChanges the domain memory.
setmaxmemSets the maximum memory limit for the domain.
numatuneConfigures the NUMA memory placement policy.
perfManages domain performance event counters.
schedinfoShows or changes scheduler parameters.

Examples:

virsh vcpuinfo web01
virsh vcpucount web01
virsh setvcpus web01 4 --config
virsh setmem web01 8G --config
virsh setmaxmem web01 16G --config
virsh vcpupin web01 0 0-3
virsh numatune web01
virsh perf web01

When changing CPU and memory settings, the effect of --live, --config, and --current parameters must be well understood. --live affects the running VM, while --config affects the persistent configuration.


6.5 Disk, Block Device, and I/O Commands

CommandDescription
domblklistLists disks attached to the domain.
domblkinfoShows capacity and size information for a specific disk.
domblkstatShows disk I/O statistics.
domblkerrorShows disk-related errors.
domblkthresholdConfigures threshold events for a block device.
blkdeviotuneSets I/O limits for a specific block device.
blkiotunePerforms block I/O tuning at the domain level.
blockresizeResizes the disk of a running domain.
blockcopyCopies a running domain disk to another target.
blockcommitCommits backing chain content to another layer.
blockpullPulls data from the backing chain into the active image.
blockjobMonitors, cancels, or manages ongoing block jobs.
domthrottlegroupsetConfigures a disk throttle group.
domthrottlegroupdelDeletes a disk throttle group.
domthrottlegroupinfoShows throttle group information.
domthrottlegrouplistLists throttle groups.

Examples:

virsh domblklist web01
virsh domblklist web01 --details
virsh domblkinfo web01 vda
virsh domblkstat web01 vda
virsh domblkerror web01
virsh blockresize web01 vda 100G --extend
virsh blockjob web01 vda --info

When a disk is expanded with blockresize, partition and filesystem expansion must also be performed inside the guest OS. The --extend option is important to prevent accidental shrinking and possible data loss.


6.6 Network Interface Commands

CommandDescription
domiflistLists network interfaces attached to the domain.
domifaddrShows domain interface IP/MAC addresses.
domifstatShows interface traffic statistics.
domiftuneManages interface bandwidth/tuning settings.
domif-getlinkShows interface link status.
domif-setlinkSets interface link status up or down.
domifannounceAnnounces interface information on the network.

Examples:

virsh domiflist web01
virsh domifaddr web01
virsh domifstat web01 vnet0
virsh domif-getlink web01 vnet0
virsh domif-setlink web01 vnet0 down
virsh domif-setlink web01 vnet0 up

The domifaddr command may retrieve IP information from DHCP leases, ARP, or QEMU Guest Agent. Therefore, in some environments, the guest agent must be installed for accurate results.


6.7 Guest Agent and Retrieving Information from the Guest

CommandDescription
guestinfoShows OS, disk, filesystem, and interface information through the guest agent.
guestvcpusShows or changes vCPU status from the guest OS perspective.
guest-agent-timeoutManages timeout values for guest agent commands.
get-user-sshkeysRetrieves SSH key information for a user inside the guest.
set-user-sshkeysChanges SSH key information for a user inside the guest.
set-user-passwordChanges the password of a user inside the guest.

Examples:

virsh guestinfo web01
virsh guestvcpus web01
virsh guest-agent-timeout web01
virsh get-user-sshkeys web01 root
virsh set-user-password web01 root 'NewPassword'

Many of these commands produce meaningful results only when QEMU Guest Agent is installed and running inside the guest.


6.8 Filesystem Commands

CommandDescription
domfsfreezeFreezes guest filesystems. Used before backup operations.
domfsthawThaws frozen filesystems.
domfsinfoShows the list of mounted filesystems inside the guest.
domfstrimRuns TRIM/discard on the guest filesystem.

Examples:

virsh domfsfreeze web01
virsh domfsthaw web01
virsh domfsinfo web01
virsh domfstrim web01

For backup and snapshot operations, domfsfreeze and domfsthaw are important for filesystem consistency.


6.9 Domain Commands Related to Backup and Checkpoints

CommandDescription
backup-beginStarts a backup job for the domain.
backup-dumpxmlShows XML information for an ongoing or created backup job.

Examples:

virsh backup-begin web01 backup.xml
virsh backup-dumpxml web01

These commands are generally used in incremental backup, checkpoint-based backup, and automation scenarios.


6.10 Migration Commands

CommandDescription
migrateMigrates a domain to another host.
migrate-compcacheManages migration compression cache settings.
migrate-getmaxdowntimeShows maximum downtime information for live migration.
migrate-setmaxdowntimeSets maximum downtime for live migration.
migrate-getspeedShows migration bandwidth/speed limit.
migrate-setspeedSets migration bandwidth/speed limit.
migrate-postcopySwitches an ongoing migration to post-copy mode.

Examples:

virsh migrate --live web01 qemu+ssh://kvm02/system
virsh migrate-getspeed web01
virsh migrate-setspeed web01 100
virsh migrate-getmaxdowntime web01
virsh migrate-setmaxdowntime web01 500
virsh migrate-postcopy web01

In migration operations, CPU compatibility, storage architecture, network bandwidth, libvirt/QEMU version compatibility, and shared storage status must be carefully evaluated.


6.11 Console, Event, Debug, and Advanced Domain Commands

CommandDescription
consoleOpens a serial console connection to the domain.
screenshotTakes a screenshot of the domain.
send-keySends a keyboard key combination to the guest.
send-process-signalSends a signal to a process inside the guest.
dumpTakes a domain memory/core dump.
eventMonitors domain events.
awaitWaits for a specific event or state on the domain.
domjobinfoShows information about a job running on the domain.
domjobabortCancels an ongoing domain job.
domdirtyrate-calcCalculates dirty page rate for live migration.
domlaunchsecinfoShows launch security information.
domsetlaunchsecstateSets launch security state.
set-lifecycle-actionChanges domain lifecycle event behavior.
dompmsuspendPuts the guest into power management suspend mode.
dompmwakeupWakes a guest from power management suspend state.

Examples:

virsh console web01
virsh screenshot web01 /tmp/web01.ppm
virsh send-key web01 KEY_LEFTCTRL KEY_LEFTALT KEY_DELETE
virsh event web01 --loop
virsh domjobinfo web01
virsh domjobabort web01
virsh domdirtyrate-calc web01

7. Device Commands — Virtual Machine Device Management

Device commands are used to add, remove, and update disks, network interfaces, CD-ROMs, PCI devices, or other devices defined by XML.

CommandDescription
attach-deviceAdds a device definition from an XML file to the domain.
attach-diskAdds a disk to the domain.
attach-interfaceAdds a network interface to the domain.
detach-deviceRemoves a device specified by XML from the domain.
detach-device-aliasRemoves a device by alias.
detach-diskRemoves a disk from the domain.
detach-interfaceRemoves a network interface from the domain.
update-deviceUpdates an existing device configuration using XML.
update-memory-deviceUpdates memory device configuration.
change-mediaChanges removable media such as CD-ROM or floppy.
dom-fd-associateAssociates a file descriptor with a domain.

Examples:

virsh attach-disk web01 /var/lib/libvirt/images/data.qcow2 vdb --persistent
virsh detach-disk web01 vdb --persistent
virsh attach-interface web01 network default --model virtio --persistent
virsh detach-interface web01 network --mac 52:54:00:aa:bb:cc --persistent
virsh change-media web01 hdc /iso/ubuntu.iso

8. Nodedev Commands — Host Hardware Devices

Nodedev commands are used to manage physical or virtual devices on the host. They are important in scenarios such as PCI passthrough, SR-IOV, and mediated devices.

CommandDescription
nodedev-createCreates a transient node device from XML.
nodedev-updateUpdates node device information.
nodedev-destroyStops or destroys a node device.
nodedev-defineCreates a node device definition.
nodedev-undefineRemoves a node device definition.
nodedev-startStarts a defined node device.
nodedev-detachDetaches a device from the host driver and prepares it for VM passthrough.
nodedev-dumpxmlShows node device XML output.
nodedev-infoShows information about a node device.
nodedev-listLists node devices on the host.
nodedev-reattachReattaches a detached device to the host driver.
nodedev-resetResets a node device.
nodedev-eventMonitors node device events.
nodedev-autostartManages node device autostart settings.

Examples:

virsh nodedev-list
virsh nodedev-dumpxml pci_0000_03_00_0
virsh nodedev-info pci_0000_03_00_0
virsh nodedev-detach pci_0000_03_00_0
virsh nodedev-reattach pci_0000_03_00_0
virsh nodedev-reset pci_0000_03_00_0

Detaching the wrong device may affect host access or storage/network connectivity. These commands must be used carefully.


9. Virtual Network Commands

Libvirt can create NAT, routed, isolated, or bridge-based virtual network structures for virtual machines. These structures can be managed with virsh.

CommandDescription
net-autostartConfigures the network to start automatically when the host boots.
net-createStarts a temporary network from an XML file.
net-defineDefines a persistent network from an XML file.
net-descManages the network description or title.
net-destroyStops an active network.
net-dumpxmlShows network XML information.
net-editEdits network XML configuration.
net-eventMonitors network events.
net-infoShows basic information about a network.
net-listLists networks.
net-metadataManages custom XML metadata for a network.
net-nameFinds the network name from a UUID.
net-startStarts a defined network.
net-undefineDeletes the network definition.
net-uuidShows the network UUID.
net-updateUpdates network configuration at runtime or config level.
net-dhcp-leasesShows DHCP lease records on the network.

Examples:

virsh net-list --all
virsh net-info default
virsh net-start default
virsh net-autostart default
virsh net-dumpxml default
virsh net-edit default
virsh net-dhcp-leases default

net-dhcp-leases is very useful for viewing IP addresses assigned to VMs in NAT network environments.


10. Network Port Commands

These commands are used to manage libvirt network port objects.

CommandDescription
net-port-listLists ports on a specific network.
net-port-createCreates a network port from XML.
net-port-dumpxmlShows network port XML output.
net-port-deleteDeletes a network port.

Examples:

virsh net-port-list default
virsh net-port-dumpxml default <PORT_UUID>
virsh net-port-delete default <PORT_UUID>

11. Interface Commands — Host Network Interface Management

These commands are used to manage network interface configurations on the host. Incorrect bridge or interface operations may cut off host access.

CommandDescription
iface-bridgePlaces a physical interface into a bridge.
iface-defineDefines an interface from XML.
iface-destroyStops an interface.
iface-dumpxmlShows interface XML information.
iface-editEdits interface XML configuration.
iface-listLists host interfaces.
iface-nameFinds the interface name from a MAC address.
iface-macFinds the MAC address from an interface name.
iface-startStarts an interface.
iface-unbridgeRemoves bridge configuration.
iface-undefineDeletes the interface definition.
iface-beginStarts a transaction for interface changes.
iface-commitApplies interface transaction changes.
iface-rollbackRolls back interface transaction changes.

Examples:

virsh iface-list --all
virsh iface-dumpxml eno1
virsh iface-bridge eno1 br0
virsh iface-unbridge br0
virsh iface-begin
virsh iface-commit
virsh iface-rollback

On production servers managed remotely, these commands should not be used outside a maintenance window.


12. Storage Pool Commands

A storage pool is the logical storage area where libvirt keeps disk images or volumes. Different pool types can be used, such as directory, filesystem, iSCSI, LVM, RBD, ZFS, or Gluster.

CommandDescription
find-storage-pool-sourcesDiscovers sources for a specific pool type.
find-pool-sources-asSearches for pool sources using parameters.
pool-autostartConfigures the pool to start automatically.
pool-buildPrepares the required infrastructure for the pool.
pool-createCreates and starts a temporary pool from XML.
pool-create-asCreates a temporary pool using parameters.
pool-defineDefines a persistent pool from XML.
pool-define-asDefines a persistent pool using parameters.
pool-destroyStops the pool; it does not delete the data inside.
pool-deleteDeletes pool sources; this may be irreversible.
pool-dumpxmlShows pool XML information.
pool-editEdits pool XML configuration.
pool-infoShows basic information about a pool.
pool-listLists pools.
pool-nameFinds the pool name from a UUID.
pool-refreshRefreshes the volume list inside the pool.
pool-startStarts a defined but inactive pool.
pool-undefineDeletes the inactive pool definition.
pool-uuidShows the pool UUID.
pool-eventMonitors pool events.

Examples:

virsh pool-list --all
virsh pool-info default
virsh pool-refresh default
virsh pool-start default
virsh pool-autostart default
virsh pool-dumpxml default
virsh pool-edit default

pool-destroy only stops the pool, while pool-delete may delete the pool resources. This distinction is critical to prevent data loss.


13. Volume Commands — Disk Image and Volume Management

A volume is a disk image or block device located inside a storage pool.

CommandDescription
vol-createCreates a volume from XML.
vol-create-fromCreates a new volume from an existing volume.
vol-create-asCreates a volume using parameters.
vol-cloneClones a volume.
vol-deleteDeletes a volume.
vol-uploadUploads data from a file into a volume.
vol-downloadDownloads volume content to a file.
vol-wipeSecurely wipes volume content.
vol-dumpxmlShows volume XML information.
vol-infoShows information about a volume.
vol-listLists volumes inside a pool.
vol-poolShows the pool to which the volume belongs.
vol-pathShows the volume path.
vol-nameShows the volume name.
vol-keyShows the volume key.
vol-resizeResizes a volume.

Examples:

virsh vol-list default
virsh vol-info web01.qcow2 --pool default
virsh vol-path web01.qcow2 --pool default
virsh vol-create-as default data01.qcow2 100G --format qcow2
virsh vol-clone web01.qcow2 web01-clone.qcow2 --pool default
virsh vol-delete data01.qcow2 --pool default

Since vol-delete may directly delete a disk image, the pool name, volume name, and path information must be verified before running it.


14. Secret Commands

The libvirt secret structure is used to store credentials in scenarios such as Ceph/RBD, iSCSI CHAP, disk encryption, and similar use cases.

CommandDescription
secret-defineDefines a secret from XML.
secret-dumpxmlShows secret XML information.
secret-eventMonitors secret events.
secret-set-valueSets the secret value.
secret-get-valueShows the secret value.
secret-undefineDeletes the secret record.
secret-listLists secrets.

Examples:

virsh secret-list
virsh secret-dumpxml <SECRET_UUID>
virsh secret-set-value <SECRET_UUID> --file secret.txt
virsh secret-get-value <SECRET_UUID>

Providing sensitive values such as passwords or keys directly on the command line is not recommended for security reasons. Passing them through a file is a better approach.


15. Snapshot Commands

A snapshot is used to save the disk, memory, and device state of a domain at a specific point in time. It is useful for creating a rollback point before updates or for quick recovery in test environments.

CommandDescription
snapshot-createCreates a snapshot using an XML file.
snapshot-create-asCreates a snapshot using parameters.
snapshot-currentShows or changes the current snapshot information.
snapshot-editEdits snapshot XML information.
snapshot-infoShows information about a snapshot.
snapshot-listLists domain snapshots.
snapshot-dumpxmlShows snapshot XML output.
snapshot-parentShows the parent snapshot information.
snapshot-revertReverts the domain to a snapshot.
snapshot-deleteDeletes a snapshot.

Examples:

virsh snapshot-create-as web01 before-update "Snapshot before patching"
virsh snapshot-list web01
virsh snapshot-info web01 before-update
virsh snapshot-dumpxml web01 before-update
virsh snapshot-revert web01 before-update
virsh snapshot-delete web01 before-update

snapshot-revert may cause changes made after the snapshot to be lost. It must be used carefully in production systems.


16. Checkpoint Commands — Checkpoint Management for Incremental Backup

A checkpoint is mainly used in incremental backup scenarios to track which blocks have changed.

CommandDescription
checkpoint-createCreates a checkpoint from XML.
checkpoint-create-asCreates a checkpoint using parameters.
checkpoint-editEdits checkpoint XML information.
checkpoint-infoShows information about a checkpoint.
checkpoint-listLists checkpoints.
checkpoint-dumpxmlShows checkpoint XML output.
checkpoint-parentShows parent checkpoint information.
checkpoint-deleteDeletes a checkpoint.

Examples:

virsh checkpoint-create-as web01 backup-checkpoint-01
virsh checkpoint-list web01
virsh checkpoint-info web01 backup-checkpoint-01
virsh checkpoint-dumpxml web01 backup-checkpoint-01
virsh checkpoint-delete web01 backup-checkpoint-01

While snapshots are used as rollback points, checkpoints are more important for incremental backup and changed block tracking.


17. NWFilter Commands — Network Filter Management

NWFilter is used to apply filtering rules on virtual machine network traffic. It can be used in security scenarios such as preventing MAC spoofing or IP spoofing.

CommandDescription
nwfilter-defineDefines a network filter from XML.
nwfilter-undefineDeletes a network filter.
nwfilter-listLists network filters.
nwfilter-dumpxmlShows network filter XML output.
nwfilter-editEdits network filter XML information.

Examples:

virsh nwfilter-list
virsh nwfilter-dumpxml clean-traffic
virsh nwfilter-edit clean-traffic
virsh nwfilter-define filter.xml
virsh nwfilter-undefine filter-name

An incorrect NWFilter rule may cut off network access for a VM. XML output should be backed up before making changes.


18. NWFilter Binding Commands

These commands manage binding relationships between a network port and a network filter.

CommandDescription
nwfilter-binding-createCreates a binding between a network port and a network filter.
nwfilter-binding-deleteRemoves the filter binding relationship from a port.
nwfilter-binding-listLists ports with applied filter bindings.
nwfilter-binding-dumpxmlShows binding XML information.

Examples:

virsh nwfilter-binding-list
virsh nwfilter-binding-dumpxml <PORT_DEVICE>
virsh nwfilter-binding-create binding.xml
virsh nwfilter-binding-delete <PORT_DEVICE>

These commands are generally used in advanced network security and custom TAP/port filtering scenarios.


19. Hypervisor-Specific Commands

These commands directly interact with the hypervisor layer, such as QEMU or LXC. They are not intended for daily use; they are mostly used for advanced troubleshooting and special scenarios.

CommandDescription
qemu-attachAttaches an externally started QEMU process to libvirt management.
qemu-monitor-commandRuns a QEMU monitor command.
qemu-agent-commandSends a direct command to QEMU Guest Agent.
qemu-monitor-eventMonitors QEMU monitor events.
lxc-enter-namespaceEnters the namespace of an LXC domain.

Examples:

virsh qemu-monitor-command web01 --hmp "info block"
virsh qemu-agent-command web01 '{"execute":"guest-info"}'
virsh qemu-monitor-event web01 --loop
virsh lxc-enter-namespace container01 -- /bin/bash

These commands may bypass libvirt’s normal management layer and lead to unexpected results. Therefore, they should only be used by experienced administrators and only when necessary.


20. Most Useful Virsh Commands for Daily Use

The following commands are among the most commonly used by a KVM/libvirt administrator in daily operations:

virsh list --all
virsh dominfo VM_NAME
virsh domstate VM_NAME --reason
virsh start VM_NAME
virsh shutdown VM_NAME
virsh reboot VM_NAME
virsh destroy VM_NAME
virsh dumpxml VM_NAME > VM_NAME.xml
virsh edit VM_NAME
virsh autostart VM_NAME
virsh domiflist VM_NAME
virsh domifaddr VM_NAME
virsh domblklist VM_NAME
virsh domblkinfo VM_NAME vda
virsh vcpuinfo VM_NAME
virsh setvcpus VM_NAME 4 --config
virsh setmem VM_NAME 8G --config
virsh console VM_NAME
virsh snapshot-list VM_NAME
virsh net-list --all
virsh pool-list --all
virsh vol-list default

These commands form the basic toolset for checking VM status, resource usage, disks, networking, snapshots, and storage.


21. Practical Virsh Flow for Troubleshooting

When a VM-related issue occurs, the following sequence can be used for initial checks:

virsh list --all

Checks whether the VM is running, powered off, or paused.

virsh dominfo VM_NAME

Retrieves basic VM information.

virsh domstate VM_NAME --reason

Checks the reason for the VM state.

virsh domblklist VM_NAME --details

Checks disks and attached block devices.

virsh domblkerror VM_NAME

Checks whether there are disk-related errors.

virsh domiflist VM_NAME

Examines network interface connections.

virsh domifaddr VM_NAME

Attempts to retrieve the VM IP address.

virsh pool-list --all

Checks storage pool status.

virsh vol-list default

Examines disk volumes.

virsh dumpxml VM_NAME > VM_NAME.xml

Backs up the VM XML configuration.

This flow is very useful for the initial analysis of most basic KVM/libvirt issues.


22. Critical Commands to Use Carefully

Virsh is a very powerful tool. Therefore, some commands may cause data loss, service interruption, or loss of management access.

CommandRisk
destroyForces the VM to power off and may cause data loss.
undefineRemoves the domain definition; with additional options, it may also affect disks.
pool-deleteMay delete storage pool resources.
vol-deleteMay delete disk images.
snapshot-revertReverts the VM to an older state; later changes may be lost.
iface-bridge / iface-unbridgeIncorrect usage may cut off host network access.
nodedev-detachDetaching the wrong device may affect host hardware access.
qemu-monitor-commandMay bypass the libvirt management layer and cause unexpected results.
domjobabortMay interrupt an ongoing critical job.

Before running these commands, XML backups should be taken if possible, storage paths should be verified, and maintenance windows should be preferred in production environments.


23. Conclusion

Virsh is one of the most comprehensive and flexible command-line management tools in libvirt-based virtualization environments. In KVM/QEMU infrastructures, it is used not only to start or stop virtual machines, but also to manage CPU, memory, disk, networking, storage pools, volumes, snapshots, checkpoints, backups, migrations, guest agents, and many hypervisor-level operations.

In small environments, a few basic commands may be enough. However, in large KVM infrastructures, virsh becomes an essential part of automation, troubleshooting, migration, backup integration, performance management, and daily operations.

When used correctly, it is fast and powerful. When used incorrectly, it may cause outages, data loss, or management access problems in running systems. Therefore, commands such as destroy, undefine, pool-delete, vol-delete, snapshot-revert, iface-*, nodedev-*, and hypervisor-specific commands must be used carefully.

In summary, virsh is a professional management tool that provides full command-line control in the KVM/libvirt world. Learning virsh commands provides a significant advantage for anyone who wants to better understand virtualization infrastructure, solve problems faster, and strengthen operations with automation.