Table of Contents
In this article I will take you through 18 Useful virsh commands examples for KVM Hypervisor Management. virsh is a free tool available to manage multiple domains deployed with Linux/Other OS in a bare metal with basic hardware resources like cpu, memory etc. It can also be used in ESXi, Virtualbox, QEMU etc to deploy and manage multiple KVM. If you want to understand the virtualization concepts to save the resources and infrastructure operational cost then you have to know the role of virsh commands to create and implement that Virtualization features. You will understand more about virsh commands as you go through this article.
Syntax
virsh [ OPTION ]… [ COMMAND_STRING ]
virsh [ OPTION ]… COMMAND [ ARG ]…
virsh commands examples
Also Read: 20 Useful Linux/Unix ssh-keygen command examples (Cheatsheet)
Example 1: How to Check virsh command version
If you want to check virsh tool version then you need to use virsh -v
command as shown below. As you can see from below output current virsh command version is 3.2.0
.
[root@localhost ~]# virsh -v 3.2.0
-v : Ignore all other arguments, and prints the version of the libvirt library virsh is coming from. More info on virsh man page.
NOTE:
root
user to run all the below commands.You can use any user with sudo
access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo
access to the User.Example 2: How to Check Domain Error using virsh commands
If you want to check any domain error then you need to use below virsh
command. As you can see from below output currently there are no error in the domain test1
.
[root@localhost ~]# virsh domblkerror test1 No errors found
NOTE:
test1
in all the examples given below.Example 3: How to Check Node info using virsh commands
If you want to check node info then you need to use nodeinfo
option with virsh
command as shown below. In this example we are trying to run virsh nodeinfo
command to check the node allocated resources information.
[root@localhost ~]# virsh nodeinfo CPU model: x86_64 CPU(s): 40 CPU frequency: 2086 MHz CPU socket(s): 1 Core(s) per socket: 10 Thread(s) per core: 2 NUMA cell(s): 2 Memory size: 134188412 KiB
Example 4: How to check a domain information using virsh commands
If you want to check any domain information then you need to use virsh dominfo
command as shown below. In this example we are checking the test1
domain information by running virsh dominfo test1
command.
[root@localhost ~]# virsh dominfo test1 Id: 19 Name: test1 OS Type: kvm State: running CPU(s): 36 CPU time: 13195.0s Max memory: 125829120 KiB Used memory: 125829120 KiB Persistent: yes Autostart: enable Managed save: no
Example 5: How to Check Node CPU Stats using virsh commands
If you want to check the current CPU stats of the node then you need to run virsh nodecpustats
command as shown below. You can check user utilization, system utilization, idle and iowait value from below output.
[root@localhost ~]# virsh nodecpustats user: 161657130570000000 system: 44253475840000000 idle: 1333309894940000000 iowait: 4648559550000000
Example 6: How to Check Node Memory Stats using virsh commands
If you want to check current memory utilization of node then you need to use virsh nodememstats
command as shown below. You can check total memory, free memory, buffers and cached memory information from below output.
[root@localhost ~]# virsh nodememstats total : 131832516 KiB free : 14828732 KiB buffers: 0 KiB cached : 11896940 KiB
nodememstats [cell]: Returns memory stats of the node. If cell is specified, this will prints specified cell statistics only. More information can be checked on virsh man page.
Example 7: How to Check Node hostname using virsh commands
If you want to check the hostname of the node then you need to use virsh hostname
command as shown below. You can see from below output current hostname is set to default localhost.localdomain
.
[root@localhost ~]# virsh hostname localhost.localdomain
Example 8: How to Connect domain using virsh commands
If you want to connect to domain test1
then you need to use virsh console test1
command as shown below. As you can see from below output you need to provide login user name and password to connect test1
domain. After successful authentication you will given the prompt of test1
domain.
[root@localhost ~]# virsh console test1 Connected to domain test1 Escape character is ^] CentOS Linux 7 (Core) Kernel 3.10.0-862.el7.x86_64 on an x86_64 test1 login: centos Password: Last login: Thu May 28 15:18:33 from 192.168.0.100 [centos@test1 ~]$
NOTE:
CentOS 7.6
in domain test1
Example 9: How to check domain Disk Usage using virt-df commands
If you want to check disk usage of all the domains then you need to use below virt-df
command. In this example we are using virt-df -d test1 -h
command to check the disk usage of domain test1
in human readable format.
[root@localhost ~]# virt-df -d test1 -h Filesystem Size Used Available Use% test1:/dev/sda2 1014M 102M 912M 11% test1:/dev/sda4 2.0G 198M 1.8G 10%
-d : domain name
Example 10: How to Debug Node Filesystem using virt-df commands
If you need to debug node filesystems then you need to use -v
option with virt-df
command as shown below. Please note that output of below command could be little more descriptive.
[root@localhost ~]# virt-df -v Filesystem 1K-blocks Used Available Use% parallel: creating 1 threads parallel: thread 0 starting parallel: thread 0 waiting to get work parallel: thread 0 taking domain 0 libguestfs: command: run: qemu-img libguestfs: command: run: \ create libguestfs: command: run: \ -f img libguestfs: command: run: \ -o backing_file=/u01/test1,backing_fmt=raw libguestfs: command: run: \ /tmp/libguestfszddZmI/overlay1 libguestfs: launch: identifier=thread_0_domain_0 libguestfs: launch: version=1.36.3rhel=7,release=6.el7_4.3,libvirt libguestfs: launch: backend registered: unix libguestfs: launch: backend registered: uml libguestfs: launch: backend registered: libvirt libguestfs: launch: backend registered: direct libguestfs: launch: backend=libvirt libguestfs: launch: tmpdir=/tmp/libguestfszddZmI libguestfs: launch: umask=0020 libguestfs: launch: euid=0 libguestfs: libvirt version = 3002000 (3.2.0) libguestfs: guest random name = guestfs-d3kmkjvupthg1aab libguestfs: connect to libvirt libguestfs: opening libvirt handle: URI = qemu:///system, auth = default+wrapper, flags = 0 libguestfs: successfully opened libvirt handle: conn = 0x7f9b480032e0 libguestfs: qemu version (reported by libvirt) = 1005003 (1.5.3) libguestfs: get libvirt capabilities libguestfs: parsing capabilities XML libguestfs: build appliance libguestfs: begin building supermin appliance supermin: version: 4.1.20 supermin: rpm: detected RPM version 4.11 supermin: package handler: fedora/rpm supermin: acquiring lock on /var/tmp/.guestfs-0/lock supermin: if-newer: output does not need rebuilding libguestfs: finished building supermin appliance libguestfs: command: run: qemu-img libguestfs: command: run: \ create
-v : Enable verbose messages for debugging.
Example 11: How to Print Filesystems inode using virt-df commands
If you want to display filesystems inode instead of block then you need to use -i
option with virt-df
command as shown below.
[root@localhost ~]# virt-df -i Filesystem Inodes IUsed IFree IUse% test1:/dev/sda2 524288 327 523961 1% test1:/dev/sda4 1072900928 289528 1072611400 1%
-i : Print inodes instead of blocks. Check more on virt-df man page.
Example 12: How to Check other virsh commands version
If you want to check virt-cat
version then you need to use virt-cat --version
command as shown below.
[root@localhost ~]# virt-cat --version virt-cat 1.36.3rhel=7,release=6.el7_4.3,libvirt
If you want to check virt-log
version then you need to use virt-log --version
command as shown below.
[root@localhost ~]# virt-log --version virt-log 1.36.3rhel=7,release=6.el7_4.3,libvirt
If you want to check virt-ls
version then you need to use virt-ls --version
command as shown below.
[root@localhost ~]# virt-ls --version virt-ls 1.36.3rhel=7,release=6.el7_4.3,libvirt
If you want to check virt-tail
version then you need to use virt-tail --version
command as shown below.
[root@localhost ~]# virt-tail --version virt-tail 1.36.3rhel=7,release=6.el7_4.3,libvirt
If you want to check virt-top
version then you need to use virt-top --version
command as shown below.
[root@localhost ~]# virt-top --version virt-top 1.0.8 ocaml-libvirt 0.6.1.4
If you want to check virt-what
version then you need to use virt-what --version
command as shown below.
[root@localhost ~]# virt-what --version 1.13
If you want to check virt-copy-in
version then you need to use virt-copy-in --version
command as shown below.
[root@localhost ~]# virt-copy-in --version virt-copy-in 1.36.3rhel=7,release=6.el7_4.3,libvirt
If you want to check virt-copy-out
version then you need to use virt-copy-out --version
command as shown below.
[root@localhost ~]# virt-copy-out --version virt-copy-out 1.36.3rhel=7,release=6.el7_4.3,libvirt
If you want to check virt-filesystems
version then you need to use virt-filesystems --version
command as shown below.
[root@localhost ~]# virt-filesystems --version virt-filesystems 1.36.3rhel=7,release=6.el7_4.3,libvirt
Example 13: How to Check logs of domain test1 using virt-log command
If you want to check complete log of domain test1
then you need to use virt-log -d test1
command but if you want to go page by page due to large size of the logs then it is better to use it with less
command as shown below.
[root@localhost ~]# virt-log -d test1 | less May 24 03:34:04 test1 dockerd: time="2020-05-24T03:34:04.807702467+04:00" level=warning msg="unknown container" container=55051c37bd24jfhfj891d5e48940e1078c67b4c617e0e2bf33b3abb77d44 module=libcontainerd May 24 03:34:04 test1 dockerd: time="2020-05-24T03:34:04.843705881+04:00" level=warning msg="unknown container" container=55051c37bd24lkjhfuff6a01d5e48940e1078c67b4c617e0e2bf33b3abb77d44 module=libcontainerd May 24 03:34:05 test1 systemd: Removed slice User Slice of root. May 24 03:34:05 test1 systemd: Stopping User Slice of root. May 24 03:34:05 test1 dockerd: time="2020-05-24T03:34:05.775491828+04:00" level=warning msg="unknown container" container=fe41627e48e4ca2fjjgkgfff738aa9e2b4f5baa9058b599cf9131045005c8ac0ab module=libcontainerd May 24 03:34:05 test1 dockerd: time="2020-05-24T03:34:05.807929338+04:00" level=warning msg="unknown container" container=fe41627e48e4ca23e8cfff738aa9e2b4f5baa9058b599cf9131045005c8ac0ab module=libcontainerd May 24 03:34:05 test1 dockerd: time="2020-05-24T03:34:05.943421681+04:00" level=warning msg="unknown container" container=1f3758dc708kfhfkfnmf8d03dc5920545b6b0a2a1729565c68b96ec182939a1 module=libcontainerd May 24 03:34:05 test1 dockerd: time="2020-05-24T03:34:05.974613862+04:00" level=warning msg="unknown container" container=1f3758dc7lkdkdkld8d03dc5920545b6b0a2a1729565c68b96ec182939a1 module=libcontainerd May 24 03:34:19 test1 dockerd: time="2020-05-24T03:34:19.823490001+04:00" level=warning msg="unknown container" container=55051c89jkfdf2c7f6a01d5e48940e1078c67b4c617e0e2bf33b3abb77d44 module=libcontainerd May 24 03:34:19 test1 dockerd: time="2020-05-24T03:34:19.874365921+04:00" level=warning msg="unknown container" container=55051c8snd9d6d46a2c7f6a01d5e48940e1078c67b4c617e0e2bf33b3abb77d44 module=libcontainerd May 24 03:34:20 test1 dockerd: time="2020-05-24T03:34:20.949417132+04:00" level=warning msg="unknown container" container=1f3758dc708e19b9jkjkdh898sndsi545b6b0a2a1729565c68b96ec182939a1 module=libcontainerd May 24 03:34:20 test1 dockerd: time="2020-05-24T03:34:20.981746772+04:00" level=warning msg="unknown container" container=1f3758dc708e1djdkjd91efndkdikd0545b6b0a2a1729565c68b96ec182939a1 module=libcontainerd
-d : domain name
Example 14: How to check domain CPU Utilization using virt-top command
If you want to check run time utilization of domain then you need to use below virt-top
command. This command is very similar to top
command that we use in Linux based systems.
[root@localhost ~]# virt-top -1 virt-top 13:31:23 - x86_64 40/40CPU 1738MHz 131043MB 1 domains, 1 active, 1 running, 0 sleeping, 0 paused, 0 inactive D:0 O:0 X:0 CPU: 2.8% Mem: 122880 MB (122880 MB by guests) PHYCPU %CPU test1 0 4.7 4.7 4.6 1 4.4 4.4 4.4 2 3.6 3.6 3.5 3 2.8 2.8 2.8 4 4.1 4.1 4.1 5 2.9 2.9 2.9 6 2.8 2.8 2.7 7 2.8 2.8 2.8 8 3.1 3.1 3.0 9 8.7 8.7 8.7 10 0.7 0.7 0.7 11 4.6 4.6 4.6 12 0.9 0.9 0.9 13 2.1 2.1 2.1
-1 : Display physical CPUs by default (instead of domains). More info can be checked on virt-top Man Page.
Example 15: How to check Network Interface Utilization using virt-top command
If you want to check network interface stats then you need to use -2
option with virt-top
command as shown below. You can simply run virt-top -2
command to check the test1
network interface utilization.
[root@localhost ~]# virt-top -2 virt-top 13:32:40 - x86_64 40/40CPU 1233MHz 131043MB 1 domains, 1 active, 1 running, 0 sleeping, 0 paused, 0 inactive D:0 O:0 X:0 CPU: 1.8% Mem: 122880 MB (122880 MB by guests) ID S RXBY TXBY RXPK TXPK DOMAIN INTERFACE 19 R 54K 31K 362 342 test1 vnet0
-2 : Display network interfaces by default (instead of domains).
Example 16: How to check Disk Utilization using virt-top command
If you want to check disk utilization of domain test1
then you need to use -3
option with virt-top
command as shown below.
[root@localhost ~]# virt-top -3 virt-top 13:33:26 - x86_64 40/40CPU 1363MHz 131043MB 1 domains, 1 active, 1 running, 0 sleeping, 0 paused, 0 inactive D:0 O:0 X:0 CPU: 1.3% Mem: 122880 MB (122880 MB by guests) ID S RDBY WRBY RDRQ WRRQ DOMAIN DEVICE 19 R 0 92K 0 19 test1 vda
-3 : Display block devices (virtual disks) by default (instead of domains).
Example 17: How to check domain directories using virt-ls command
If you want to check all the files and directories of domain test1
recursively in the path /u01
then you need to use below virt-ls
command. This command is very similar to ls
command in Linux.
[root@localhost ~]# virt-ls -R -d test1 /u01 package-9.2-install-test-9.2.tar.gz package-install-9.2/ package-install-9.2/.resources package-install-9.2/ConfigEditor package-install-9.2/ConfigEditor/deployment_am_config.html package-install-9.2/ConfigEditor/deployment_am_config.html package-install-9.2/ConfigEditor/deployment_cca_config.html package-install-9.2/ConfigEditor/deployment_vi_editor.html package-install-9.2/ConfigEditor/dest package-install-9.2/ConfigEditor/dest/bootstrap.css package-install-9.2/ConfigEditor/dest/saver.min.js package-install-9.2/ConfigEditor/dest/font-awe.css package-install-9.2/ConfigEditor/dest/vieditor.min.js package-install-9.2/ConfigEditor/fonts package-install-9.2/ConfigEditor/fon
Example 18: How to check other virsh commands option
If you want to check all the options of virsh
commands then you need to use virsh --help
command as shown below. You can check all the virsh
command options from below output.
[root@localhost ~]# virsh --help virsh [options]... [<command_string>] virsh [options]... <command> [args...] options: -c | --connect=URI hypervisor connection URI -d | --debug=NUM debug level [0-4] -e | --escape <char> set escape sequence for console -h | --help this help -k | --keepalive-interval=NUM keepalive interval in seconds, 0 for disable -K | --keepalive-count=NUM number of possible missed keepalive messages -l | --log=FILE output logging to file -q | --quiet quiet mode -r | --readonly connect readonly -t | --timing print timing information -v short version -V long version --version[=TYPE] version, TYPE is short or long (default short) commands (non interactive mode): Domain Management (help keyword 'domain') attach-device attach device from an XML file attach-disk attach disk device attach-interface attach network interface autostart autostart a domain blkdeviotune Set or query a block device I/O tuning parameters. blkiotune Get or set blkio parameters blockcommit Start a block commit operation. blockcopy Start a block copy operation. blockjob Manage active block operations blockpull Populate a disk from its backing image. blockresize Resize block device of domain. change-media Change media of CD or floppy drive console connect to the guest console cpu-baseline compute baseline CPU cpu-compare compare host CPU with a CPU described by an XML file cpu-stats show domain cpu statistics create create a domain from an XML file define define (but don't start) a domain from an XML file desc show or set domain's description or title destroy destroy (stop) a domain detach-device detach device from an XML file detach-disk detach disk device detach-interface detach network interface domdisplay domain display connection URI domfsfreeze Freeze domain's mounted filesystems. domfsthaw Thaw domain's mounted filesystems. domfsinfo Get information of domain's mounted filesystems. domfstrim Invoke fstrim on domain's mounted filesystems. domhostname print the domain's hostname domid convert a domain name or UUID to domain id domif-setlink set link state of a virtual interface domiftune get/set parameters of a virtual interface domjobabort abort active domain job domjobinfo domain job information domname convert a domain id or UUID to domain name domrename rename a domain dompmsuspend suspend a domain gracefully using power management functions dompmwakeup wakeup a domain from pmsuspended state domuuid convert a domain name or id to domain UUID domxml-from-native Convert native config to domain XML domxml-to-native Convert domain XML to native config dump dump the core of a domain to a file for analysis dumpxml domain information in XML edit edit XML configuration for a domain event Domain Events inject-nmi Inject NMI to the guest iothreadinfo view domain IOThreads iothreadpin control domain IOThread affinity virsh commands iothreadadd add an IOThread to the guest domain iothreaddel delete an IOThread from the guest domain send-key Send keycodes to the guest virsh commands send-process-signal Send signals to processes lxc-enter-namespace LXC Guest virsh commands Enter Namespace managedsave managed save of a domain state managedsave-remove Remove managed save of a domain memtune Get or set memory parameters
Popular Recommendations:-
How to Cleanup Failed Actions from PCS Status of Cluster
How to Enable or Disable SELinux Temporarily or Permanently on RedHat/CentOS 7/8
10 Popular Examples of sudo command in Linux(RedHat/CentOS 7/8)
How to Install and Use telnet command in Linux (RedHat/Linux 7/8) using 5 Easy Steps