virsh Command Examples in Linux

Many virtualization host servers will run Linux without a graphical user interface. Such a configuration enables more hardware resources to be available to the virtual machines. Management of the virtual machines must then occur at the command-line. The virsh command is an interactive shell to KVM virtual machines. The following are some subcommands you can use within virsh.

Option Used To
help Get help with the virsh command.
list Get a list of recognized VMs.
shutdown {VM} Gracefully shut down a VM.
start {VM} Start a VM.
reboot {VM} Reboot a VM.
create {XML file name} Create a VM from an XML file.
save {VM} {file name} Save the state of a VM with the given file name.
console {VM} Open a console to a VM.

Linux virtualization solutions are built on top of libvirt, an application programming interface (API) that provides the software building blocks for developers to write their own virtualization solutions. Solutions can also be composed of a daemon and a management interface. Several hypervisors, including VMware ESXi, KVM, QEMU, etc., are all built using libvirt. It provides a solid foundation for Linux-based virtualization. For example, the virsh tool is a part of the libvirt API.

virsh Command Examples

1. Connect to a hypervisor session:

# virsh connect qemu:///system

2. List all domains:

# virsh list --all

3. Dump guest configuration file:

# virsh dumpxml guest_id > path/to/guest.xml

4. Create a guest from a configuration file:

# virsh create path/to/config_file.xml

5. Edit a guest’s configuration file (editor can be changed with $EDITOR):

# virsh edit guest_id

6. Start/reboot/shutdown/suspend/resume a guest:

# virsh command guest_id

7. Save the current state of a guest to a file:

# virsh save guest_id filename

8. Delete a running guest:

# virsh destroy guest_id && virsh undefine guest_id
Related Post