How to Install and Configure KVM on Ubuntu 18.04 LTS Server

KVM (Kernel-based Virtual Machine) is an open source full virtualization solution for Linux like systems, KVM provides virtualization functionality using the virtualization extensions like Intel VT or AMD-V.  Whenever we install KVM on any linux box then it turns it into the hyervisor by loading the kernel modules like kvm-intel.ko( for intel based machines) and kvm-amd.ko ( for amd based machines).

KVM allows us to install and run multiple virtual machines (Windows & Linux). We can create and manage KVM based virtual machines either via virt-manager graphical user interface or virt-install & virsh cli commands.

In this article we will discuss how to install and configure KVM hypervisor on Ubuntu 18.04 LTS server. I am assuming you have already installed Ubuntu 18.04 LTS server on your system. Login to your server and perform the following steps.

Step:1 Verify Whether your system support hardware virtualization

Execute below egrep command to verify whether your system supports hardware virtualization or not,

[email protected]:~$ egrep -c '(vmx|svm)' /proc/cpuinfo
1
[email protected]:~$

If the output is greater than 0 then it means your system supports Virtualization else reboot your system, then go to BIOS settings and enable VT technology.

Now Install “kvm-ok” utility using below command, it is used to determine if your server is capable of running hardware accelerated KVM virtual machines

[email protected]:~$ sudo apt install cpu-checker

Run kvm-ok command and verify the output,

[email protected]:~$ sudo kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
[email protected]:~$

Step:2 Install KVM and its required packages

Run the below apt commands to install KVM and its dependencies

[email protected]:~$ sudo apt update
[email protected]:~$ sudo apt install qemu qemu-kvm libvirt-bin  bridge-utils  virt-manager

Once the above packages are installed successfully, then your local user (In my case linuxtechi) will be added to the group libvirtd automatically.

Step:3 Start & enable libvirtd service

Whenever we install qemu & libvirtd packages in Ubuntu 18.04 Server then it will automatically start and enable libvirtd service, In case libvirtd service is not started and enabled then run beneath commands,

[email protected]:~$ sudo service libvirtd start
[email protected]:~$ sudo update-rc.d libvirtd enable

Now verify the status of libvirtd service using below command,

[email protected]:~$ service libvirtd status

Output would be something like below:

libvirtd-command-ubuntu18-04

Step:4 Configure Network Bridge for KVM virtual Machines

Network bridge is required to access the KVM based virtual machines outside the KVM hypervisor or host. In Ubuntu 18.04, network is managed by netplan utility, whenever we freshly installed Ubuntu 18.04 server then a file with name “/etc/netplan/50-cloud-init.yaml” is created automatically, to configure static IP and bridge, netplan utility will refer this file.

As of now I have already configured the static IP via this file and content of this file is below:

network:
    ethernets:
        ens33:
            addresses: [192.168.0.51/24]
            gateway4: 192.168.0.1
            nameservers:
              addresses: [192.168.0.1]
            dhcp4: no
            optional: true
    version: 2

Let’s add the network bridge definition in this file,

[email protected]:~$ sudo vi /etc/netplan/50-cloud-init.yaml
network:
  version: 2
  ethernets:
    ens33:
      dhcp4: no
      dhcp6: no

  bridges:
    br0:
      interfaces: [ens33]
      dhcp4: no
      addresses: [192.168.0.51/24]
      gateway4: 192.168.0.1
      nameservers:
        addresses: [192.168.0.1]

As you can see we have removed the IP address from interface(ens33) and add the same IP to the bridge ‘br0‘ and also added interface (ens33) to the bridge br0. Apply these changes using below netplan command,

[email protected]:~$ sudo netplan apply
[email protected]-04:~$

If you want to see the debug logs then use the below command,

[email protected]:~$ sudo netplan --debug  apply

Now Verify the bridge status using following methods:

[email protected]:~$ sudo networkctl status -a

networkctl-command-output-ubuntu18-04

[email protected]:~$ ifconfig

ifconfig-command-output-ubuntu18-04

Start:5 Creating Virtual machine (virt-manager or virt-install command )

There are two ways to create virtual machine:

  • virt-manager (GUI utility)
  • virt-install command (cli utility)

Creating Virtual machine using virt-manager:

Start the virt-manager by executing the beneath command,

[email protected]:~$ sudo virt-manager

Start-Virt-Manager-Ubuntu18-04

Create a new virtual machine

ISO-file-Virt-Manager

Click on forward and select the ISO file, in my case I am using RHEL 7.3 iso file.

Select-ISO-file-virt-manager-Ubuntu18-04-Server

Click on Forward

In the next couple of windows, you will be prompted to specify the RAM, CPU and disk for the VM.

Now Specify the Name of the Virtual Machine and network,

VM-Name-Network-Virt-Manager-Ubuntu18-04

Click on Finish

RHEL7-3-Installation-Virt-Manager

Now follow the screen instruction and complete the installation,

Read More On : “How to Create, Revert and Delete KVM Virtual machine (domain) snapshot with virsh command

Creating Virtual machine from CLI using virt-install command,

Use the below virt-install command to create a VM from terminal, it will start the installation in CLI, replace the name of the VM, description, location of ISO file and network bridge as per your setup.

[email protected]:~$ sudo virt-install  -n DB-Server  --description "Test VM for Database"  --os-type=Linux  --os-variant=rhel7  --ram=1096  --vcpus=1  --disk path=/var/lib/libvirt/images/dbserver.img,bus=virtio,size=10  --network bridge:br0 --graphics none  --location /home/linuxtechi/rhel-server-7.3-x86_64-dvd.iso --extra-args console=ttyS0

That’s conclude the article, I hope this article help you to install KVM on your Ubuntu 18.04 Server. Apart from this, KVM is the default hypervisor for Openstack.

Read More on : How to Install and Configure KVM on OpenSUSE Leap 15

20 Responses

  1. Mac says:

    Great article.. straight to the point..

  2. Steve says:

    Helpful – but virt-install wasn’t recognised for me, I had to install virtinst also:
    sudo apt install virtinst
    Also I needed to replace –location with –cdrom and remove the extra-args (even though I was not using a cd rom, otherwise I got a ‘Couldn’t find hvm kernel .. error)

    • Smarty says:

      Its because of wrong ISO! Do not use the cloud based standard version – instead use the alternativ: at the moment:
      ‘http://cdimage.ubuntu.com/releases/18.04.2/release/ubuntu-18.04.2-server-amd64.iso’
      If you get an Error 404 there is a newer version. Check instead
      ‘http://cdimage.ubuntu.com/releases’

  3. Gannet says:

    I think it is better to remove netplan and use networkd to configure network including bridges. But thanks for how to use netplan for creating bridges. Now I know it.

  4. Chitzle says:

    Sorry if this is sill question but why do we configure the br0 with a static IP and not the hardware interface ens33? Does this result in the host and the guest OS using the same IP?

    • Pradeep Kumar says:

      Hi Chitzle,

      If you want your virtual machines to be accessed from outside of KVM host then you need to create a bridge, remove the IP from eth0 or ens33 and assign the same ip to bridge interface.
      Map VMs interface to the bridge either via command line or virt-manager.After that you can assign the IP from the VLAN which is associated to bridge.

      • Eric says:

        Could one instead configure the host machine as a router, forwarding packets between its physical interface and a separate virtual interface, the latter of which would be the gateway for guest machines’ interfaces?

  5. MikeP says:

    Everything seemed to work, but I don’t see the new vm assigned its own IP via dhcp as I expected.
    ‘This is my xml dump

    virsh dumpxml Node01

    Node01
    dc38066a-f428-4990-b2d1-cf88c6b346a2
    4194304
    4194304
    2

    /machine

    hvm

    Opteron_G5
    AMD

    destroy
    restart
    destroy

    /usr/bin/kvm-spice

    libvirt-dc38066a-f428-4990-b2d1-cf88c6b346a2
    libvirt-dc38066a-f428-4990-b2d1-cf88c6b346a2

    +64055:+115
    +64055:+115

  6. MAksym says:

    GUYS,

    ALWAYS use YAML validators BEFORE you make ANY changes in vi /etc/netplan/50-cloud-init.yaml

    Thanks author for article.

  7. Arvind Pal says:

    Very nice article, step by step very helpful, Thanks

  8. Michael Cooper says:

    You must have installed this on a desktop because I cannot get virt-manager to work I get an error that it cannot start because of something with GTK-warning

    [email protected]:~$ sudo virt-manager
    [email protected]:~$ Unable to init server: Could not connect: Connection refused
    Unable to init server: Could not connect: Connection refused
    Unable to init server: Could not connect: Connection refused

    (virt-manager:1875): Gtk-WARNING **: 17:59:02.848: cannot open display:

    Anyone have a luce as to why it’s not starting?

    Thanks,
    Michael

  9. anonym says:

    The following packages have unmet dependencies:
    virt-manager : Depends: python2.7 but it is not going to be installed
    Depends: python:any (>= 2.7.5-5~)
    Depends: python-gi but it is not going to be installed
    Depends: python-gi-cairo but it is not going to be installed
    Depends: python-dbus but it is not going to be installed
    Depends: python-requests but it is not going to be installed
    Depends: python-libvirt (>= 0.7.1) but it is not going to be installed
    Depends: virtinst (>= 1:1.5.1-0ubuntu1) but it is not going to be installed
    E: Unable to correct problems, you have held broken packages.
    And I can not install python2.7 🙁

  10. JV says:

    Step:4 .. are you adding or editing your current file?
    if I add the suggested settings then I lose network connection.
    How does the final full file of 50-cloud-init.yaml look like?

    • Pradeep Kumar says:

      Hi ,

      You should not loose your network connection as we are assigning the same IP of our Lan card to a bridge. In my case i am editing the existing file.

      Make sure you put lines in correct format as it a YAML file.

  11. Askar says:

    To check virtualization support the command should be …
    egrep -c ‘(vmx|svm)’ /proc/cpuinfo

  12. Cristian Arriaza says:

    You are the best thank you very much, better than on the manufacturer’s website 🙂

  13. Eric says:

    Do I understand correctly from Step 5 that one can be logged into a headless U 18.04 server via ssh and run the graphical virt-manager utility? Even in the absence of a local graphical environment?

    If so, how must ssh be configured? Is it just a question of setting X11Forwarding=yes in the respective ssh_config and sshd_config files?

  14. Tony says:

    You’ll also need to install a X server on your host machine (for Windows, I recommend vcxsrv, open source and updated).

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest