{"id":7730,"date":"2021-01-28T22:26:55","date_gmt":"2021-01-28T19:26:55","guid":{"rendered":"https:\/\/kifarunix.com\/?p=7730"},"modified":"2024-03-19T20:39:31","modified_gmt":"2024-03-19T17:39:31","slug":"start-and-stop-kvm-virtual-machines-from-command-line","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/start-and-stop-kvm-virtual-machines-from-command-line\/","title":{"rendered":"Start and Stop KVM Virtual Machines from Command Line"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1010\" height=\"395\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/start-n-stop-kvm-vms.png\" alt=\"Start and Stop KVM Virtual Machines from Command Line\" class=\"wp-image-7801\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/start-n-stop-kvm-vms.png?v=1611861703 1010w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/start-n-stop-kvm-vms-768x300.png?v=1611861703 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/start-n-stop-kvm-vms-150x59.png?v=1611861703 150w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/start-n-stop-kvm-vms-300x117.png?v=1611861703 300w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/start-n-stop-kvm-vms-696x272.png?v=1611861703 696w\" sizes=\"(max-width: 1010px) 100vw, 1010px\" \/><\/figure><\/div>\n\n\n<p>This guide will take you through how to start and stop KVM virtual machines from command line. As much as KVM provides Virtual Machine Manager (<strong>virt-manager<\/strong>), a desktop application for managing virtual&nbsp;machines through libvirt, it also provides a command line utility called <strong><code>virsh<\/code><\/strong> which enables the terminal centric users to manage KVM virtual machines from command line interface.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Starting and Stopping KVM VMss from Command Line<\/h2>\n\n\n\n<p>To control state of KVM VMs, <strong><code>virsh<\/code><\/strong> utility can be used.<\/p>\n\n\n\n<p>According to man pages (<code><strong>man virsh<\/strong><\/code>);<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-style-large td_pull_quote td_pull_center is-layout-flow wp-block-quote-is-layout-flow\">\n<p>The virsh program is the main interface for managing virsh guest domains. The program can be used to create, pause, and shutdown domains. It can also be used to list current domains.<\/p>\n<\/blockquote>\n\n\n\n<p>The basic structure of most virsh usage is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> virsh &#91;OPTION]... &lt;command&gt; &lt;domain&gt; &#91;ARG]...<\/code><\/pre>\n\n\n\n<p>Refer to <strong><code>man virsh<\/code><\/strong> for more details.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"start-kvm-virtual-machine-from-cli\"><a href=\"#start-kvm-virtual-machine-from-cli\" class=\"rank-math-link\">Start KVM Virtual Machines from Command Line<\/a><\/h3>\n\n\n\n<p>To begin with, list the available virtual machines, either running or stopped or paused;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virsh list --all<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code> Id   Name                State\n------------------------------------\n 1    ubuntu20.04         running\n -    kolla-ansible       shut off\n -    ubuntu20.04-clone   shut off<\/code><\/pre>\n\n\n\n<p>As you can see, I currently have three KVM virtual machines; one running and two stopped.<\/p>\n\n\n\n<p>You can start KVM virtual machine using <code><strong>virsh start<\/strong><\/code> command. The <strong><code>virsh start<\/code><\/strong> command can be used to start a domain, either from the last managedsave state, or via a fresh boot if no managedsave state is present.<\/p>\n\n\n\n<p>The basic usage syntax of the virsh start command is;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virsh start --help<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>  NAME\n    start - start a (previously defined) inactive domain\n\n  SYNOPSIS\n    start &lt;domain&gt; &#91;--console] &#91;--paused] &#91;--autodestroy] &#91;--bypass-cache] &#91;--force-boot] &#91;--pass-fds &lt;string&gt;]\n\n  DESCRIPTION\n    Start a domain, either from the last managedsave\n    state, or via a fresh boot if no managedsave state\n    is present.\n\n  OPTIONS\n    &#91;--domain] &lt;string&gt;  name of the inactive domain\n    --console        attach to console after creation\n    --paused         leave the guest paused after creation\n    --autodestroy    automatically destroy the guest when virsh disconnects\n    --bypass-cache   avoid file system cache when loading\n    --force-boot     force fresh boot by discarding any managed save\n    --pass-fds &lt;string&gt;  pass file descriptors N,M,... to the guest<\/code><\/pre>\n\n\n\n<p>Therefore, if you want to start a virtual machine, for example, Ubuntu20.04-clone, as in my case;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virsh start ubuntu20.04-clone<\/code><\/pre>\n\n\n\n<p>You can pass other options as well, if you want.<\/p>\n\n\n\n<p>Your virtual machine should now be running. You can list only running vms using the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virsh list<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code> Id   Name                State\n-----------------------------------\n 1    ubuntu20.04         running\n 3    ubuntu20.04-clone   running<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"stop-kvm-vm-on-command-line\"><a href=\"#stop-kvm-vm-on-command-line\" class=\"rank-math-link\">Stop KVM Virtual Machines from Command Line<\/a><\/h3>\n\n\n\n<p>It is also possible to stop KVM virtual machine from command line using the <code><strong>virsh shutdown<\/strong><\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virsh shutdown --help<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>  NAME\n    shutdown - gracefully shutdown a domain\n\n  SYNOPSIS\n    shutdown &lt;domain&gt; &#91;--mode &lt;string&gt;]\n\n  DESCRIPTION\n    Run shutdown in the target domain.\n\n  OPTIONS\n    &#91;--domain] &lt;string&gt;  domain name, id or uuid\n    --mode &lt;string&gt;  shutdown mode: acpi|agent|initctl|signal|paravirt<\/code><\/pre>\n\n\n\n<p>For example, to shutdown a vm called, <code><strong>ubuntu20.04<\/strong><\/code>;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virsh shutdown Ubuntu20.04<\/code><\/pre>\n\n\n\n<p>You can as well specify the vm UUID or ID instead of the name when shutting it down.<\/p>\n\n\n\n<p>For example, when you run virsh list command to check running vms, the first column of the output shows the vm ID. To use the ID while shutting down a vm;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virsh shutdown &lt;id&gt;<\/code><\/pre>\n\n\n\n<p>for example;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virsh shutdown 3<\/code><\/pre>\n\n\n\n<p>To use UUID while shutting down a vm, you first need to obtain the vm UUID. To get the Universally Unique Identifier (UUID) for a guest virtual machine:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virsh domuuid <em>{domain-id or domain-name}<\/em><\/code><\/pre>\n\n\n\n<p>For example;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virsh domuuid 4<\/code><\/pre>\n\n\n\n<p>Or<\/p>\n\n\n\n<p>virsh domuuid ubuntu20.04<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>b05cdf0c-8516-4f1e-a517-f52aab45cf61<\/code><\/pre>\n\n\n\n<p>Similarly, you can use virsh dominfo to get other details including the UUID of the vm;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virsh dominfo ubuntu20.04<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Id:             -\nName:           ubuntu20.04\nUUID:           b05cdf0c-8516-4f1e-a517-f52aab45cf61\nOS Type:        hvm\nState:          shut off\nCPU(s):         2\nMax memory:     2097152 KiB\nUsed memory:    2097152 KiB\nPersistent:     yes\nAutostart:      disable\nManaged save:   no\nSecurity model: none\nSecurity DOI:   0<\/code><\/pre>\n\n\n\n<p>The shutdown using the UUID;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virsh shutdown b05cdf0c-8516-4f1e-a517-f52aab45cf61<\/code><\/pre>\n\n\n\n<p>You can as well specify other power options while shutting down the vm.<\/p>\n\n\n\n<p>That marks the end of our tutorial on how to control the states KVM virtual machines from command line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other Related Tutorials<\/h3>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/list-running-and-stopped-vms-on-kvm\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">List Running and Stopped VMS on KVM<\/a><\/p>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/how-to-clone-kvm-virtual-machines\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">How to Clone KVM Virtual Machines<\/a><\/p>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/how-to-fix-qemu-kvm-not-connected-error-on-ubuntu-20-04\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">How to fix QEMU\/KVM Not Connected Error on Ubuntu 20.04<\/a><\/p>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/use-virtualbox-vms-on-kvm\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Use VirtualBox VMs on KVM<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-rename-kvm-virtual-machine-with-virsh-command\/\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">How to Rename KVM Virtual Machine with virsh command<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide will take you through how to start and stop KVM virtual machines from command line. As much as KVM provides Virtual Machine Manager<\/p>\n","protected":false},"author":1,"featured_media":7801,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[112,121,36],"tags":[113,3079,3080,3072,3076,3073,3077,3078,3075,3074],"class_list":["post-7730","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kvm","category-howtos","category-virtualization","tag-kvm","tag-poweroff-kvm-vm","tag-shutdown-kvm-vm","tag-start-kvm-virtual-machine-with-virsh-command","tag-start-kvm-vm-from-command-line","tag-stop-kvm-virtual-machine-with-virsh-command","tag-stop-kvm-vm-from-command-line","tag-virsh","tag-virsh-shutdown","tag-virsh-start","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/7730"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=7730"}],"version-history":[{"count":6,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/7730\/revisions"}],"predecessor-version":[{"id":21951,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/7730\/revisions\/21951"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/7801"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=7730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=7730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=7730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}