{"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":"
\n
\"Start<\/figure><\/div>\n\n\n

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 (virt-manager<\/strong>), a desktop application for managing virtual machines through libvirt, it also provides a command line utility called virsh<\/code><\/strong> which enables the terminal centric users to manage KVM virtual machines from command line interface.<\/p>\n\n\n\n

Starting and Stopping KVM VMss from Command Line<\/h2>\n\n\n\n

To control state of KVM VMs, virsh<\/code><\/strong> utility can be used.<\/p>\n\n\n\n

According to man pages (man virsh<\/strong><\/code>);<\/p>\n\n\n\n

\n

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

The basic structure of most virsh usage is:<\/p>\n\n\n\n

 virsh [OPTION]... <command> <domain> [ARG]...<\/code><\/pre>\n\n\n\n

Refer to man virsh<\/code><\/strong> for more details.<\/p>\n\n\n\n

Start KVM Virtual Machines from Command Line<\/a><\/h3>\n\n\n\n

To begin with, list the available virtual machines, either running or stopped or paused;<\/p>\n\n\n\n

virsh list --all<\/code><\/pre>\n\n\n\n
 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

As you can see, I currently have three KVM virtual machines; one running and two stopped.<\/p>\n\n\n\n

You can start KVM virtual machine using virsh start<\/strong><\/code> command. The 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

The basic usage syntax of the virsh start command is;<\/p>\n\n\n\n

virsh start --help<\/code><\/pre>\n\n\n\n
  NAME\n    start - start a (previously defined) inactive domain\n\n  SYNOPSIS\n    start <domain> [--console] [--paused] [--autodestroy] [--bypass-cache] [--force-boot] [--pass-fds <string>]\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    [--domain] <string>  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 <string>  pass file descriptors N,M,... to the guest<\/code><\/pre>\n\n\n\n

Therefore, if you want to start a virtual machine, for example, Ubuntu20.04-clone, as in my case;<\/p>\n\n\n\n

virsh start ubuntu20.04-clone<\/code><\/pre>\n\n\n\n

You can pass other options as well, if you want.<\/p>\n\n\n\n

Your virtual machine should now be running. You can list only running vms using the command below;<\/p>\n\n\n\n

virsh list<\/code><\/pre>\n\n\n\n
 Id   Name                State\n-----------------------------------\n 1    ubuntu20.04         running\n 3    ubuntu20.04-clone   running<\/code><\/pre>\n\n\n\n

Stop KVM Virtual Machines from Command Line<\/a><\/h3>\n\n\n\n

It is also possible to stop KVM virtual machine from command line using the virsh shutdown<\/strong><\/code> command.<\/p>\n\n\n\n

virsh shutdown --help<\/code><\/pre>\n\n\n\n
  NAME\n    shutdown - gracefully shutdown a domain\n\n  SYNOPSIS\n    shutdown <domain> [--mode <string>]\n\n  DESCRIPTION\n    Run shutdown in the target domain.\n\n  OPTIONS\n    [--domain] <string>  domain name, id or uuid\n    --mode <string>  shutdown mode: acpi|agent|initctl|signal|paravirt<\/code><\/pre>\n\n\n\n

For example, to shutdown a vm called, ubuntu20.04<\/strong><\/code>;<\/p>\n\n\n\n

virsh shutdown Ubuntu20.04<\/code><\/pre>\n\n\n\n

You can as well specify the vm UUID or ID instead of the name when shutting it down.<\/p>\n\n\n\n

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

virsh shutdown <id><\/code><\/pre>\n\n\n\n

for example;<\/p>\n\n\n\n

virsh shutdown 3<\/code><\/pre>\n\n\n\n

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

virsh domuuid {domain-id or domain-name}<\/em><\/code><\/pre>\n\n\n\n

For example;<\/p>\n\n\n\n

virsh domuuid 4<\/code><\/pre>\n\n\n\n

Or<\/p>\n\n\n\n

virsh domuuid ubuntu20.04<\/p>\n\n\n\n

b05cdf0c-8516-4f1e-a517-f52aab45cf61<\/code><\/pre>\n\n\n\n

Similarly, you can use virsh dominfo to get other details including the UUID of the vm;<\/p>\n\n\n\n

virsh dominfo ubuntu20.04<\/code><\/pre>\n\n\n\n
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

The shutdown using the UUID;<\/p>\n\n\n\n

virsh shutdown b05cdf0c-8516-4f1e-a517-f52aab45cf61<\/code><\/pre>\n\n\n\n

You can as well specify other power options while shutting down the vm.<\/p>\n\n\n\n

That marks the end of our tutorial on how to control the states KVM virtual machines from command line.<\/p>\n\n\n\n

Other Related Tutorials<\/h3>\n\n\n\n

List Running and Stopped VMS on KVM<\/a><\/p>\n\n\n\n

How to Clone KVM Virtual Machines<\/a><\/p>\n\n\n\n

How to fix QEMU\/KVM Not Connected Error on Ubuntu 20.04<\/a><\/p>\n\n\n\n

Use VirtualBox VMs on KVM<\/a><\/p>\n\n\n\n

How to Rename KVM Virtual Machine with virsh command<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

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}]}}