How To Create Virtual Machine In Linux With KVM?

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. 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 of 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 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 a 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-get 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


Set up internet access to virtual machine

Before creating a virtual network for guest OS take the backup of your current network interfaces by using the following command in your terminal-

sudo cp /etc/network/interfaces /etc/network/interfaces.bakup-feb20

and then open /etc/network/interfaces using a text editor in your terminal –

sudo nano /etc/network/interfaces

Either comment or replace the existing configuration with the following-

auto lo
iface lo inet loopback

auto br0
iface br0 inet static
        address 192.168.0.10
        network 192.168.0.0
        netmask 255.255.255.0
        broadcast 192.168.0.255
        gateway 192.168.0.1
        dns-nameservers 192.168.0.5 8.8.8.8
        bridge_ports eth0
        bridge_stp off
        bridge_fd 0
        bridge_maxwait 0

For DHCP use,

auto lo
iface lo inet loopback

auto br0
iface br0 inet dhcp
        bridge_ports eth0
        bridge_stp off
        bridge_fd 0
        bridge_maxwait 0

This will create a virtual network interface br0.

Now restart the networking by using the following command

sudo /etc/init.d/networking restart

or reboot your system with –

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.

Previous How To Install and Configure Z Shell (zsh) in Ubuntu/Linux Mint?
Next How To Setup Nginx As Reverse Proxy?

About author

You might also like

Administration 0 Comments

Using DD to backup your drive

This post was last updated on July 9th, 2020 at 03:47 pmHow to backup your hard drive (the type of format doesn’t matter) using dd. Boot to some rescue mode

Administration 0 Comments

How To Install And Use Docker In Ubuntu/Linux Mint?

This post was last updated on May 5th, 2020 at 12:58 pmUnlike the Virtual Machine in Linux which uses all the resources of the host machine, Docker is a platform

Administration 0 Comments

How To Setup Nginx Server In Linux?

This post was last updated on March 1st, 2020 at 03:15 amNginx, which is pronounced as ‘engine x’ is a free, opensource and high-performance HTTP server. It is among the

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply