How To Create Virtual Machine In Linux With KVM?


Practically a virtual machine is used to make some resource(s) to be used by multiple users. The use of Virtual Machine in Linux is more common as the latter is a widely used Operating System in the data center, Cloud industry, and Hosting industry.

Typical user computer systems comprise 4 to 32 GB of RAM, a single processor, and a hard drive with few TBs of storage. But the computers used in datacenters have somewhere around 256GB to many TBs of RAM, multi-drive hard disks, multiple processors, multiple network controllers, etc.

In such huge computer systems, deploying virtualization technology such as Xen virtualization or KVM, etc. is inevitable to make the best use of resources.

Shared hosting or VPS hosting is such an example where it uses Virtual Machine on Linux or with other OS.

What is Virtualization and/or a Virtual Machine?

Virtualization means creating a virtual instance of any resource such as storage devices, network resources, etc. Just creating a network bridge in Linux to have multiple instances of the network controller is virtualization.

Similarly, a Virtual Machine is software that creates a layer between the actual OS running on the system (known as Host OS) to allow other OS (known as Guest OS) to run on the same system or hardware resources.

This article does not explain how virtualization works rather in this article we will discuss how to use KVM to create a virtual machine in Linux.

What is KVM?

Kernel-based virtual machine or KVM is a virtualization module in the Linux Kernel which turns the Linux kernel into a hypervisor.

KVM requires hardware with virtualization extensions like Intel-VT or AMD-V to provide hardware-assisted virtualization. It also provides paravirtualization support for Linux, FreeBSD, OpenBSD, Windows, etc using VirtIO API.

How to Install KVM in Linux?

So before you start the installation you should check if your CPU supports virtualization or not. Use the following command to verify the same:

lscpu

lscpu virtualization check

You can see the above command shows the availability of virtualization in the CPU. You can now proceed to install KVM and do rest of the setup.

Now install the kvm-ok utility which determines if your system or server is capable of running a hardware-accelerated KVM virtual machine. Use the following commands to install and verify it –

sudo apt-get install cpu-checker
sudo kvm-ok

Use the following command to install  KVM and required packages in Ubuntu/Linux Mint –

sudo apt install qemu qemu-kvm libvirt-bin bridge-utils virt-manager gir1.2-spiceclientgtk-3.0

Press y if ask for confirmation.

Now the next step is to start and enable the libvirtd service. Generally, it gets automatically started but if not you can use the following command to start and enable-

sudo service libvirtd start
sudo update-rc.d libvirtd enable

Now check the status of libvirtd by using-

service libvirtd status


Finally, restart your system to make all the changes effective.

reboot

How to Create A Virtual Machine in Linux?

First, download the ISO file of the guest operating system that you are going to use in KVM. Then open your terminal and execute the following command to open the virtual machine manager GUI –

virt-manager

This will open a window like given below. Click on the icon highlighted in the red –

This will open another window. Now choose an option for installation and click on the forward button. Since we are going to install OS using ISO image I will choose the first option i.e. Local install media (ISO image or CDROM).

 

Select, Use ISO image and click on Browse

Now again this will open another window. Now click on Browse Local

 

And select your ISO file of OS and click on the Open button –

Now your ISO file is selected click on forward to proceed –

Here you have to assign the amount of RAM and number of CPUs and then click on the forward button.

Now assign the size of the disk image that is to be created for the virtual machine. And then click on the forward button to proceed-

Here you can edit the name of the virtual machine,  Click on the network selection and choose the virtual network ‘default’:NAT  and then click on the finish button.

Now it will start the guest OS you can choose among the options to use it live or install it in KVM-

Some Useful Commands to Manage Guest OS Domains

List the running virtual machines/domains –

sudo virsh list

Shutdown a virtual machine named KaliLinux-

sudo virsh shutdown KaliLinux

Start a virtual machine or domain named KaliLinux-

sudo virsh start  KaliLinux

Safe reboot a virtual machine KaliLinux-

sudo virsh reboot  KaliLinux

To see a detailed list of commands use to manage virtual machines use the following command-

man virsh

or

virsh help

That’s all on creating virtual machine in Linux. If you have any doubt on this topic, leave a comment to get a reply instantly. Stay tuned to get more of such updates.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.