Skip to content

Instantly share code, notes, and snippets.

@jaysridhar
Last active March 14, 2023 18:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaysridhar/6bb18638364a57cc9a8a6a8b6d227f4a to your computer and use it in GitHub Desktop.
Save jaysridhar/6bb18638364a57cc9a8a6a8b6d227f4a to your computer and use it in GitHub Desktop.
Setting up virtual machines on Ubuntu using KVM

Started with Ubuntu 18.04 LTS minimal install on KVM host

Installed the following KVM related packages

Running as root, obviously:

apt install -y qemu-kvm libvirt-bin bridge-utils virtinst

Reboot and check that virtualization daemon is running

systemctl status libvirtd

A virtual bridge must now be present:

brctl show

Create virtual hard drive for the VM

cd /var/lib/libvirt
qemu-img create -f qcow2 images/hellovm-5.qcow2 10G

This creates a hard drive of size 10 gigs, enough for our sample

Download and store the Ubuntu guest ISO under images

cd images && wget https://releases.ubuntu.com/18.04/ubuntu-18.04.5-live-server-amd64.iso && cd ..

Create virtual machine now

virt-install --virt-type kvm \
  --name hellovm-5 \
  --ram 2048 \
  --disk images/hellovm-5.qcow2,format=qcow2 \
  --network network=default \
  --graphics vnc,listen=0.0.0.0 \
  --noautoconsole \
  --cdrom boot/ubuntu-18.04.5-live-server-amd64.iso

Most important:

  1. without --noautoconsole, the creation process hung for more than 15 minutes.
  2. without listen=0.0.0.0, I was not able to connect to the guest using VNC.

Which port is VNC listening on?

virsh vncdisplay hellovm-5

Make a note of the port. I installed TigerVNC on my Windows desktop to complete the installation.

The VNC port from the above command was 5900.

Use putty to make an SSH tunnel

Use puttygen to create an SSH key and save the private key so it can be used by pageant.

 "c:\Program Files (x86)\PuTTY\puttygen.exe"

save putty private key

The server hosting the KVM machines is hostA below.

Copy the exported OpenSSH key over to root@hostA.example.com:.ssh/authorized_keys

generate openssh key

The following command (on Windows) opens an SSH tunnel to hostA

"c:\Program Files (x86)\PuTTY\plink.exe" -v -N -L 5900:hostA.example.com:5900 root@hostA.example.com

Using TigerVNC to connect to the KVM guest

We connect to the VNC server on 5900 on the localhost thanks to the tunnel established above.

VNC connection

All set now. You can proceed with the installation of the guest OS.

After installation, change root password of guest VM

You don't have to do this, but in case you need to:

On the host:

apt install libguestfs-tools

Ensure that the guest is shutdown.

``shell virsh shutdown


Now you can update the root password on the guest with (setting password to `PASSWORD`):

```shell
virt-customize -a <path to guest disk file> --root-password password:PASSWORD

Deleting a guest VM

First shutdown the VM

virsh shutdown <vmID>

Next, destroy the VM

virsh destroy <vmName>

And finally, undefine it (somewhat wonky, I know)

virsh undefine <vmName>

And now you have given it the final rites.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment