KVM: Setup on Ubuntu 16.04

January 2, 2018 in hypervisor, kvm, linux ‐ 2 min read

This article describes how to:

  • Install KVM
  • Set up a virtual Ubuntu Server
  • Configure portforwarding to the virtual machine

The benefits of virtualizing are:

  • Secure the host- and guestservers from one another
  • Run multiple services such as databases without running into unwanted synergy effects

Canonical is releasing a so-called Ubuntu “Cloud Image”. This is a slim, optimized for virtualisation image of the ubuntu server.

Also we will take a look at the very powerfull tools Libvirt and ukv-kvm that automize a lot of the work setting up virtual machines.

 

I****nstall KVM

SSH to your server with a user that has sudo. Before doing any work we update the packet sources and install pending updates.

sudo apt-get update && sudo apt-get upgrade

Not every CPU does support virtualisation. To check if your precessor works with kvm we install the “cpu-checker”.

sudo apt-get install cpu-checker

Now you can check if your CPU meets the requirements using  kvm-ok.

image

Now that we are sure that we can work with KVM we install the uvtool.

sudo apt-get install uvtool

This provides us with two new tools with lots of parameters:

  • uvt-simplestreams-libvirt → Download and update images (synchronize)
  • uvt-kvm → Manage VMs

Lets download a Ubuntu 16.04 (xenial) 64-bit image. Even if the cloud images are relativley small, compared to the original server images, this might take a few minutes depending on your internet connection.

uvt-simplestreams-libvirt sync release=xenial arch=amd64

 

Create Virtual Machine

To be able to connect to the VM we have to provide a public ssh key that gets implanted into the VM when setting it up. In this example we create a new key, you can use your existing one however, if you want.

Remember to give the key another name then “id_rsa”, to not override any existing key.

ssh-keygen

image

Now we use uvt-kvm to set up a ne VM with the name „vm_xenial“ and provide it with the generated public key. (The default user is “Ubuntu”. The key will be placed inside the vm at /home/ubuntu/.ssh/Authorized_keys )

uvt-kvm create vm_xenial release=xenial --ssh-public-key-file ~/.ssh/vm_xenial_id_rsa.pub

Using virsh we check the running VMs.

virsh list

image

Check for the IP…

uvt-kvm ip xenial

image

… and ssh to the VM using the private key.

ssh -i ./vm_xenial_id_rsa ubuntu@<ip>

image

Cheers, Ori

Lots of thanks to Fl@ and beegeees, who provided significant help in writing this article.