The virsh command provides hundreds of options to manage every aspect of your virtual machines. By using virsh, you can quickly connect to a server using secure shell (SSH) and perform operations on your VMs without access to a graphical interface.
Virsh
The virsh
program is the main interface for managing virsh guest domains. It can be used to create, pause, and shutdown domains. It can also be used to list current domains.
Libvirt
is a C toolkit to interact with the virtualization capabilities of recent versions of Linux (and other OSes). The library aims at providing a long term stable C API. It currently supports Xen, QEMU, KVM, LXC, OpenVZ, VirtualBox and VMware ESX.
The basic structure of most virsh usage is:
virsh [OPTION]... <command> <domain> [ARG]... |
virsh list
The virsh list
command lists all running VMs:
# virsh list |
List all available VMs by adding the --all
option:
# virsh list --all |
Shut down, start or reboot VM :
# virsh shutdown vm-prod01 |
You can start it up again using the start
subcommand:
# virsh start vm-prod01 |
virsh domifaddr
The domifaddr
subcommand lists all the IP addresses configured for all virtual interfaces in a given VM.
# virsh domifaddr vm-prod01 |
By default, it lists the IP address leased by a DHCP server. If the hypervisor does not provide this information, you can also use the option --source agent
to query the guest OS directly (requires virtualization agent installed in the guest OS).
virsh dumpxml
The dumpxml
subcommand dumps the XML configuration for a given VM.
You can use it to export the configuration to a file to make changes to an existing VM or use it as a template to create another VM with a similar configuration.
# virsh dumpxml vm-prod01 |
virsh edit
The edit
subcommand opens the current XML configuration in your default $EDITOR, allowing you to make live modifications in a VM:
# virsh edit vm-prod01 |
After making your modifications, save the file to apply them. Some modifications may only take effect after a reboot.
virsh net-edit
The net-edit
subcommand allows you to make live modifications to a virtual network configuration.
# virsh net-edit --network hostonly |
You can also use other network-related subcommands starting with net-
to manage different aspects of the hypervisor virtual networks.
Documentation
https://libvirt.org/
https://libvirt.org/uri.html
https://libvirt.org/manpages/virsh.html
https://www.redhat.com/sysadmin/virsh-subcommands
https://computingforgeeks.com/virsh-commands-cheatsheet/?expand_article=1