Virsh, short for Virtual Shell, is a command line user interface for managing virtual guest machines. Virsh allows you to create, list, edit, start, restart, stop, suspend, resume, shutdown and delete VMs. It currently supports KVM, LXC, Xen, QEMU, OpenVZ, VirtualBox and VMware ESX. In this guide, we will be discussing how to manage KVM virtual machines with Virsh management user interface in Linux.
Make sure you have installed KVM and Virsh utility on your Linux machine. The following guides explains how to install KVM on DEB and RPM-based systems.
- Install And Configure KVM In Ubuntu 20.04 Headless Server
- Install And Configure KVM In CentOS 8 Server
- Install And Configure KVM In OpenSUSE Tumbleweed
Table of Contents
1. Manage KVM Virtual Machines With Virsh command
If you're new to Virsh and KVM, it is better to start with reading the help manual. It will give a basic idea of how to use Virsh.
1.1. Getting help
Enter the following command to view the list of available commands along with brief description:
$ virsh help
You will see an extensive list of available virsh commands to manage KVM virtual machines from command line.
You don't have to memorize everything. Just read the description of a command that you want to run and use it.
The commands are grouped into the following sections:
- Domain Management,
- Domain Monitoring,
- Host and Hypervisor,
- Checkpoint,
- Interface,
- Network Filter,
- Networking,
- Node Device,
- Secret,
- Snapshot,
- Backup,
- Storage Pool,
- Storage Volume,
- Virsh itself.
Each section contains the commands related to do particular set of tasks. You can view help section of a group, for example Networking, like below:
$ virsh help Networking
You will see commands related to do networking tasks:
Networking (help keyword 'network'): net-autostart autostart a network net-create create a network from an XML file net-define define an inactive persistent virtual network or modify an existing persistent one from an XML file net-destroy destroy (stop) a network net-dhcp-leases print lease info for a given network net-dumpxml network information in XML net-edit edit XML configuration for a network net-event Network Events net-info network information net-list list networks net-name convert a network UUID to network name net-start start a (previously defined) inactive network net-undefine undefine a persistent network net-update update parts of an existing network's configuration net-uuid convert a network name to network UUID net-port-list list network ports net-port-create create a network port from an XML file net-port-dumpxml network port information in XML net-port-delete delete the specified network port
You can further display help section of a specific command as well. For example, I am going to display the help section of "net-name" command:
$ virsh help net-name NAME net-name - convert a network UUID to network name SYNOPSIS net-name <network> OPTIONS [--network] <string> network uuid
1.2. List virtual machines
To view list of guest virtual machines in run or suspend mode, execute the following command:
$ virsh list Id Name State --------------------
As you can see, there are no guests in run or suspend mode.
You can use the --inactive option to list inactive guests.
To view all guest machines, run:
$ virsh list --all Id Name State -------------------------------- - centos8-uefi shut off - nginx_centos8 shut off
As you see in the above output, I have two virtual machines namely "centos8-uefi" and "nginx_centos8". Both are powered off.
1.3. Start Virtual machines
To start a virtual machine, for example "centos8-uefi", run:
$ virsh start centos8-uefi
You will see an output like below:
Domain centos8-uefi started
To verify if the VM is running, use "list" command:
$ virsh list Id Name State ------------------------------ 1 centos8-uefi running
1.4. Save Virtual machines
To save the current state of a running VM, run:
$ virsh save centos8-uefi centos8-save Domain centos8-uefi saved to centos8-save
This command stops the guest named "centos8-uefi" and saves the data to a file called "centos8-save". This will take a few moments depending upon the amount of memory in use by your guest machine.
1.5. Restore Virtual machines
To restore the previously saved state of a VM, just specify the file name like below:
$ virsh restore centos8-save Domain restored from centos8-save
Verify if the VM is restored using "list" command:
$ virsh list Id Name State ------------------------------ 4 centos8-uefi running
1.6. Restart Virtual machines
To restart a running VM, run:
$ virsh reboot centos8-uefi Domain centos8-uefi is being rebooted
1.7. Suspend/Pause Virtual machines
To suspend a running VM, do:
$ virsh suspend centos8-uefi Domain centos8-uefi suspended
Verify it with "list" command:
$ virsh list Id Name State ----------------------------- 1 centos8-uefi paused
1.8. Resume Virtual machines
To resume the paused VM, run:
$ virsh resume centos8-uefi Domain centos8-uefi resumed
1.9. Stop active Virtual machines
To forcibly stop an active VM, and leave it in the inactive state, run:
$ virsh destroy centos8-uefi Domain centos8-uefi destroyed
You can also gracefully stop the VM instead of forcing it like below:
$ virsh destroy centos8-uefi --graceful Domain centos8-uefi destroyed
1.10. Shutdown Virtual machines
To power off a running VM, do:
$ virsh shutdown centos8-uefi Domain centos8-uefi is being shutdown
1.11. Retrieve Virtual machines XML dump
To display the XML configuration file of a VM in the standard output, run:
$ virsh dumpxml centos8-uefi
This command will display the complete configuration details (software and hardware) of the virtual machine:
You can also export the XML dump to a file instead of just displaying it in the standard output like below:
$ virsh dumpxml centos8-uefi > centos8.xml
This command will dump the "centos8-uefi" XML file in a file named "centos8.xml" and save it in the current working directory.
1.12. Create Virtual machines with XML dump
You can create a new virtual guest machine using the existing XML from previously created guests. First, create a XML dump as shown above and then create a new VM using the XML file like below:
$ virsh create centos8.xml Domain centos8-uefi created from centos8.xml
This command will create a new VM and start it immediately. You can verify it using command:
1.13. Edit Virtual machines XML configuration file
If you wanted to make any changes in a guest machines, you can simply edit its configuration file and do the changes as you wish. The guests can be edited either while they run or while they are offline.
$ virsh edit centos8-uefi
This command will open the file in your default editor that you set with $EDITOR variable.
1.14. Enable console access for Virtual machines
After creating KVM guest machines, you can access them via SSH, VNC client, Virt-viewer, Virt-manager and Cockpit web console etc. However, you can't access them using "virsh console" command. The console command is used to connect the virtual serial console for the guest. To access KVM guests using "virsh console" command, you need to enable serial console access in the guest machine. Refer the following guide to enable virsh console access:
1.15. Rename Virtual machines
If you ever wanted to rename a virtual machine, refer the following guide.
1.16. Display Domain ID of Virtual machines
To find the domain id of a running guest virtual machine, run:
$ virsh domid centos8-uefi 2
Please note that the guest should be running to get its domain id.
1.17. Display Domain name of Virtual machines
To get the domain name of a running VM, run:
$ virsh domname <domain-id or domain-uuid>
Example:
$ virsh domname 2 centos8-uefi
Here, 2 is the domain id.
1.18. Display UUID of Virtual machines
To find the guest machine UUID, run:
$ virsh domuuid <domain-name or domain-id>
Example:
$ virsh domuuid centos8-uefi
Or,
$ virsh domuuid 2
Sample output:
de4100c4-632e-4c09-8dcf-bbde29170268
1.19. Display Virtual machines details
To display a guest machine's information, use domain name, domain id or domain uuid like below:
$ virsh dominfo centos8-uefi
Or,
$ virsh dominfo 2
Or,
$ virsh dominfo de4100c4-632e-4c09-8dcf-bbde29170268
Sample output:
Id: -
Name: centos8-uefi
UUID: de4100c4-632e-4c09-8dcf-bbde29170268
OS Type: hvm
State: shut off
CPU(s): 2
Max memory: 2097152 KiB
Used memory: 2097152 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: apparmor
Security DOI: 0
1.20. Display KVM host information
To get the information of your host system, run:
$ virsh nodeinfo
Sample output:
CPU model: x86_64
CPU(s): 4
CPU frequency: 1167 MHz
CPU socket(s): 1
Core(s) per socket: 2
Thread(s) per core: 2
NUMA cell(s): 1
Memory size: 8058840 KiB
1.21. Display Virtual CPU information
To display the virtual CPU information, run:
$ virsh vcpuinfo <domain-id or domain-name or domain-uuid>
Example:
$ virsh vcpuinfo centos8-uefi
VCPU: 0
CPU: 3
State: running
CPU time: 5.6s
CPU Affinity: yyyy
VCPU: 1
CPU: 1
State: running
CPU time: 0.0s
CPU Affinity: yyyy
1.22. Find IP address of Virtual machines
Finding the IP address of a virtual machine is not a big deal. If you have console access to the virtual machine, you can easily find its IP address using “ip” command. However, it is also possible to identify a KVM VM’s IP address without having to access its console. The following guide explains how to find the IP address of a KVM virtual machine.
1.23. Delete Virtual machines
If you don't want a VM anymore, simply delete it like below:
$ virsh destroy centos8-uefi
$ virsh undefine centos8-uefi
The first command will forcibly stop the VM if it is already running. And the second command will undefine and delete it completely.
You can further use following options to delete the storage volumes and snapshots as well.
--managed-save remove domain managed state file
--storage remove associated storage volumes (comma separated list of targets or source paths) (see domblklist)
--remove-all-storage remove all associated storage volumes (use with caution)
--delete-storage-volume-snapshots delete snapshots associated with volume(s)
--wipe-storage wipe data on the removed volumes
--snapshots-metadata remove all domain snapshot metadata (vm must be inactive)
2. Manage Virtual networks
Hope you learned how to manage KVM virtual machines with Virsh command in Linux. This section lists the imporant commands to manage KVM virtual networks in Linux using virsh command line utility.
2.1. List virtual networks
To list of available virtual networks, run:
$ virsh net-list
Name State Autostart Persistent
--------------------------------------------
default active yes yes
As you can see, I have only one virtual network which is the default one.
2.2. Display virtual network details
To view the details of a virtual network, run:
$ virsh net-dumpxml default
Replace "default" with your network name in the above command.
Sample output:
<network connections='1'>
<name>default</name>
<uuid>ce25d978-e455-47a6-b545-51d01bcb9e6f</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:ee:35:49'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>
2.3. Start virtual networks
To start an inactive network, run:
$ virsh net-start <Name-Of-Inactive-Network>
To auto-start a network:
$ virsh net-autostart <network-name>
2.4. Create virtual networks XML dump
To create the XML configuration file of an existing virtual network, run:
$ virsh net-dumpxml default > default.xml
The above command will create XML config of the "default" network and save it in a file named "default.xml" in the current directory.
You can view the XML file using cat command:
$ cat default.xml
<network connections='1'>
<name>default</name>
<uuid>ce25d978-e455-47a6-b545-51d01bcb9e6f</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:ee:35:49'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>
2.5. Create new virtual networks from XML file
To create a new virtual network using an existing XML file and start it immediately, run:
$ virsh net-create <Name-of-XMLfile>
If you want to create a network from XML file but don't want to start it automtacially, run:
$ virsh net-define <Name-of-XMLfile>
2.6. Deactivate virtual networks
To deactivate an active network, run:
$ virsh net-destroy <network-name>
2.7. Delete virtual networks
To delete a virtual network, deactivate it frist as shown above and then run:
$ virsh net-undefine <Name-Of-Inactive-Network>
Virsh has a lot of commands and options. Learning to use Virsh command line tool thoroughly is just enough to setup a complete Virtual environment in Linux. You don't need any GUI applications.
For more details, refer virsh man pages.
$ man virsh
3. Manage KVM guests graphically
Remembering all virsh commands is nearly impossible and also unnecessary. If you find it hard to perform all Kvm management tasks from command line, you can try graphical KVM management tools such as Virt-manager and Cockpit.
- Manage KVM Virtual Machines Using Cockpit Web Console
- How To Manage KVM Virtual Machines With Virt-Manager
Conclusion
If you know how to manage KVM virtual machines with Virsh management user interface in Linux, you're half way across to manage an enterprise grade virtualization environment. Setting up KVM and managing KVM virtual machines using virsh command are very important for all Linux administrators.
Featured image by Elias Sch. from Pixabay.