{"id":12090,"date":"2021-11-18T05:52:21","date_gmt":"2021-11-18T05:52:21","guid":{"rendered":"https:\/\/linuxways.net\/?p=12090"},"modified":"2024-03-25T02:10:31","modified_gmt":"2024-03-25T02:10:31","slug":"how-to-create-a-virtual-machine-in-kvm-on-the-command-line","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-to-create-a-virtual-machine-in-kvm-on-the-command-line\/","title":{"rendered":"How to create a virtual machine in KVM on the command line"},"content":{"rendered":"<p>In the previous guide, we looked at how to install the KVM hypervisor on Rocky Linux \/ AlmaLinux. We went further and demonstrated how you can create and manage virtual machines using the<a href=\"https:\/\/virt-manager.org\/\"> virt-manager<\/a>. This is a desktop application that provides a GUI interface that allows users to intuitively create and manage virtual machines.<\/p>\n<p>In this guide, we shift focus slightly and walk you through how you can create a virtual machine on command line.<\/p>\n<h2><strong>Prerequisites<\/strong><\/h2>\n<p>For this to be successful, you need to have KVM installed on your Linux instance. We already have a guide on how to install KVM on Rocky Linux \/ AlmaLinux.<\/p>\n<p>Additionally, ensure that you have a sudo user configured on your system for performing elevated tasks.<\/p>\n<h2><strong>How to create a virtual machine in KVM on Command line<\/strong><\/h2>\n<p>In this example, we are going to create a virtual machine from a Debian 10 ISO image located in the \u2018Downloads\u2019 folder in the home directory. To achieve this, launch the terminal and execute the following command:<\/p>\n<pre>\r\n<strong>$ sudo virt-install --name=debian-10 \\<\/strong>\r\n\r\n<strong>--os-type=Linux \\<\/strong>\r\n\r\n<strong>--os-variant=debian10 \\<\/strong>\r\n\r\n<strong>--vcpu=2 \\<\/strong>\r\n\r\n<strong>--ram=2048 \\<\/strong>\r\n\r\n<strong>--disk path=\/var\/lib\/libvirt\/images\/debian.img,size=15 \\<\/strong>\r\n\r\n<strong>--graphics vnc,listen=0.0.0.0 \\<\/strong>\r\n\r\n<strong>--location=\/home\/james\/Downloads\/debian-10.1.0-amd64-netinst.iso \\<\/strong>\r\n\r\n<strong>--network bridge:virbr0<\/strong>\r\n<\/pre>\n<p>Let\u2019s briefly expound on the options used:<\/p>\n<p><strong> &#8211;name <\/strong>: This is the name of the virtual machine, in our case &#8211; <strong>debian-10.<\/strong><\/p>\n<p><strong>&#8211;os-type: <\/strong>The option indicates the operating system.<\/p>\n<p><strong>&#8211;os-variant: <\/strong> This indicates the flavor or version of your OS. You can get a comprehensive list of all he supported OS variants by running the <strong>osinfo-query os <\/strong>command.<\/p>\n<p><strong>&#8211;vcpu: <\/strong>The number of virtual CPUs allocated to the virtual machine.<\/p>\n<p><strong>&#8211;ram: <\/strong>The amount of RAM in Megabytes allocated to the virtual machine.<\/p>\n<p><strong>&#8211;disk path: <\/strong>The option specifies the path of the virtual machine image. The <strong>size <\/strong>option specifies the size of the image in GB.<\/p>\n<p><strong>&#8211;graphics:<\/strong> This specifies the graphical mode used to access the virtual machine. In this case, we have specified <strong>vnc<\/strong> as our preferred option.<\/p>\n<p><strong>&#8211;location: <\/strong>This points to the location of the ISO image used for creating the virtual machine.<\/p>\n<p>Here is some output generated from running the command:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"847\" height=\"398\" class=\"wp-image-12091\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-281.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-281.png 847w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-281-300x141.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-281-768x361.png 768w\" sizes=\"auto, (max-width: 847px) 100vw, 847px\" \/><\/p>\n<h2><a id=\"post-12090-_ygneznpfwd74\"><\/a><strong>Accessing the virtual machine <\/strong><\/h2>\n<p>At this point, the virtual machine is running. But how do you access its graphical interface? There are two main ways of going about this. You can use a VNC client or simply use the Virtual machine manager.<\/p>\n<p>To use VNC, you need to install a VNC client such as TigerVNC. To install it, simply run the command:<\/p>\n<pre>\r\n<strong>$ sudo dnf install tigervnc<\/strong>\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"847\" height=\"451\" class=\"wp-image-12092\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-282.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-282.png 847w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-282-300x160.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-282-768x409.png 768w\" sizes=\"auto, (max-width: 847px) 100vw, 847px\" \/><\/p>\n<p>Next, run the following command to find out which vnc port the virtual machine is listening to:<\/p>\n<pre>\r\n<strong>$ sudo virsh vncdisplay debian-10<\/strong>\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"842\" height=\"177\" class=\"wp-image-12093\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-283.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-283.png 842w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-283-300x63.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-283-768x161.png 768w\" sizes=\"auto, (max-width: 842px) 100vw, 842px\" \/><\/p>\n<p>Next, use the application manager to launch TigerVNC.Type in your IP address followed by the port.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"207\" class=\"wp-image-12094\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-284.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-284.png 470w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-284-300x132.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/p>\n<p>This opens the TigerVNC graphical viewer as shown.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"527\" class=\"wp-image-12095\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-285.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-285.png 640w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-285-300x247.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<h2><a id=\"post-12090-_4jh47xkjk936\"><\/a><strong>Managing the virtual machines on command-line<\/strong><\/h2>\n<p>The <strong>virsh <\/strong>utility is a command-line tool that is used to manage virtual machines. You can perform various operations as we shall see shortly.<\/p>\n<p>To list currently running virtual machines, run the command:<\/p>\n<pre>\r\n<strong>$ sudo virsh list<\/strong>\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"842\" height=\"249\" class=\"wp-image-12097\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-286.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-286.png 842w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-286-300x89.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-286-768x227.png 768w\" sizes=\"auto, (max-width: 842px) 100vw, 842px\" \/><\/p>\n<p>To list all the virtual machines, including those that have been powered off use the <strong>&#8211;all <\/strong>option at the end. Since we have only deployed a since VM, the output will remain the same.<\/p>\n<pre>\r\n<strong>$ sudo virsh list --all<\/strong>\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"850\" height=\"212\" class=\"wp-image-12098\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-287.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-287.png 850w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-287-300x75.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-287-768x192.png 768w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p>To poweroff a vm, use the syntax:<\/p>\n<pre>\r\n<strong>$ sudo virsh shutdown vm<\/strong>\r\n<\/pre>\n<p>For example, to poweroff the virtual machine, run:<\/p>\n<pre>\r\n<strong>$ sudo virsh shutdown debian-10<\/strong>\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"849\" height=\"173\" class=\"wp-image-12099\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-288.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-288.png 849w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-288-300x61.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-288-768x156.png 768w\" sizes=\"auto, (max-width: 849px) 100vw, 849px\" \/><\/p>\n<p>To start the virtual machine execute:<\/p>\n<pre>\r\n<strong>$ sudo virsh start debian-10<\/strong>\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"837\" height=\"174\" class=\"wp-image-12100\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-289.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-289.png 837w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-289-300x62.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-289-768x160.png 768w\" sizes=\"auto, (max-width: 837px) 100vw, 837px\" \/><\/p>\n<p>To reboot the virtual machine, run:<\/p>\n<pre>\r\n<strong>$ sudo virsh reboot debian-10<\/strong>\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"188\" class=\"wp-image-12102\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-290.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-290.png 787w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-290-300x72.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-290-768x183.png 768w\" sizes=\"auto, (max-width: 787px) 100vw, 787px\" \/><\/p>\n<p>To suspend the VM, run the command:<\/p>\n<pre>\r\n<strong>$ sudo virsh suspend debian-10<\/strong>\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"845\" height=\"157\" class=\"wp-image-12103\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-291.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-291.png 845w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-291-300x56.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-291-768x143.png 768w\" sizes=\"auto, (max-width: 845px) 100vw, 845px\" \/><\/p>\n<p>To resume the vm, execute:<\/p>\n<pre>\r\n<strong>$ sudo virsh resume debian-10<\/strong>\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"839\" height=\"185\" class=\"wp-image-12105\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-294.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-294.png 839w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-294-300x66.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-294-768x169.png 768w\" sizes=\"auto, (max-width: 839px) 100vw, 839px\" \/><\/p>\n<p>And finally, you can delete to destroy the virtual machine:<\/p>\n<pre>\r\n<strong>$ sudo virsh destroy debian-10<\/strong>\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"847\" height=\"192\" class=\"wp-image-12108\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-296.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-296.png 847w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-296-300x68.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-296-768x174.png 768w\" sizes=\"auto, (max-width: 847px) 100vw, 847px\" \/><\/p>\n<h2><a id=\"post-12090-_5t1ffwerjtkl\"><\/a><strong>Conclusion<\/strong><\/h2>\n<p>In this guide, we have shown you how to create and manage guest virtual machines on KVM from the command-line. We hope you have grasped the basic concepts of creating anf managing the state of virtual machines straight from the terminal console.<\/p>","protected":false},"excerpt":{"rendered":"<p>In the previous guide, we looked at how to install the KVM hypervisor on Rocky Linux \/ AlmaLinux. We went further and demonstrated how you can create and&hellip;<\/p>","protected":false},"author":110,"featured_media":12096,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,168,2],"tags":[],"class_list":["post-12090","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-scripting","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/12090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=12090"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/12090\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/12096"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=12090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=12090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=12090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}