Hetzner - KVM - libvirt - Ubuntu walkthrough to routed network for VMs

Kernel Virtual Machines

The target of this article is to help people who have a dedicated server in Hetzner and want to run VMs through KVM and libvirt on it.

We assume that you:

  • have a dedicated server which runs Ubuntu
  • have ordered a subnet - or an additional IP
  • want to run Ubuntu on VMs
  • To order a subnet in Hetzner is quite easy, login to Robot Console and navigate where it says "Servers", at the end of the page there is the relevant link.

install these packages in the host server


bridge-utils kvm libvirt-bin xauth x11-apps qemu-kvm virtinst virt-viewer libcap2-bin

now the setcap

setcap cap_net_admin=ei /usr/bin/qemu-system-x86_64


adduser {your_username} kvm


open the file



and add the following line in it

cap_net_admin YOUR_USERNAME


Now make backup of these files as you are going to edit them.

sudo cp /etc/sysctl.conf /etc/sysctl.conf.bak sudo cp /etc/network/interfaces /etc/network/interfaces.bak


ok, now open /etc/sysctl.conf and uncomment these lines

#net.ipv4.conf.all.rp_filter=1 #net.ipv4.ip_forward=1 #net.ipv6.conf.all.forwarding=1


now let's make some directories

sudo mkdir /var/VMs 
sudo mkdir /var/VMs/ISOs 
sudo mkdir /var/VMs/VHDs

navigate to /var/VMs/ISOs and wget ubuntu server .iso image, make sure in the vminstallation.sh script the name meets this .ISO

now create the xml file for the network

nano /root/hetzner.xml


and add the following content. Please note this "your_IP_from_subnet", there put your subnet IP.

 <forward dev='eth0' mode='route'/>
 <bridge name='virbr2' stp='off' forwardDelay='0' />
 <ip address='<your_IP_from_subnet>' netmask=''/>

 join it to virsh

virsh net-define /root/hetzner.xml

 and make it autostart

virsh net-autostart Hetzner

ok, now let's take care the file /etc/network/interfaces
open it and make it like this, (note: replace XX:YY with yours IP)

### Hetzner Online AG - installimage
# Loopback device:
auto lo
iface lo inet loopback
# device: eth0
auto  eth0
iface eth0 inet static
  address   {YOUR_MAIN_IP}
  broadcast {YOUR_BROADCAST_IP, this means leave it as it is}
  gateway   {YOUR_GATEWAY_IP, this means leave it as it is}
  pointopoint {GATEWAY_IP, here add your GATEWAY_IP}
  # default route to access subnet
  up route add -net {LEAVE_IT_AS_IT_IS_IP} netmask gw {GATEWAY_IP} eth0
  # ip addr add {additional IP}/32 dev eth0 uncomment this line if you are going to use additional IP and NOT subnet IPs

iface eth0 inet6 static
  address 2a01:4f8:XX:YY::2
  netmask 128
  gateway fe80::1

auto virbr2
iface virbr2 inet static
  address {YOUR_MAIN_IP}
  bridge_ports none  
  bridge_stp off
  bridge_fd 0
  pre-up brctl addbr virbr2 # comment this line if you are giong to use additional IP and NOT subnet IPs
  up ip route add {YOUR_ADDITIONAL_OR_FROM_SUBNET}/32 dev virbr2
  down ip route del {YOUR_ADDITIONAL_IP_FROM_SUBNET}/32 dev virbr2  # comment this line if you are giong to use additional IP and NOT subnet IPs

iface virbr2 inet6 static
  address 2a01:4f8:XX:YY::2
  netmask 64

note. by default to edit virsh files it is done through VIM editor, if you like nano editor better then add the following lines in the file.bashrc

alias virsh='EDITOR=/usr/bin/nano virsh'

and to take effect immediately

source .bashrc

reboot host now

now let's create the script that will do the installation of the guest server.

sudo nano vminstall.sh

and add the following

virt-install --name 12.04 --file /var/VMs/VHDs/12.04 --vnc --cdrom /var/VMs/ISOs/ubuntu-12.04.4-server-amd64.iso --ram 1024 --connect qemu:///system --file-size 5 --nonsparse --network network=Hetzner --os-type=linux --os-variant=ubuntuprecise 

 note that the the above script will create the guest with the following characteristics

-the downloaded distro is ubuntu-12.04.4-server-amd64.iso
-1024MB RAM will be applied
-5GB in the hard disk

but you can make your own decision, just check out this manual http://linux.die.net/man/1/virt-install

open a terminal  and connect to your server with X forwarding
{ssh -X} with that way
virt-viewer --connect qemu:///system 12.04

and from another terminal start the operation
virsh start 12.04


now it is time to execute the installation script, do it and pass through the installation steps of the Ubuntu server. Somewhere in the middle it will say that the network is unconfigured, leave it as it is and go on to finish the installation.

When finished (in the guest OS) edit the file /etc/network/interfaceswith the following
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
  address {YOUR_SUBNET_IP}
  pointopoint {YOUR_MAIN_SERVER_IP}
iface eth0 inet6 static
  address 2a01:4f8:XX:YY::4
  netmask 64
  gateway 2a01:4f8:XX:YY::2

now (in the guest OS) edit the file /etc/hosts	localhost

# The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters

ok we are almost done, run these commands for the network to take effect.
sudo ifdown eth0
sudo ifup etho

OK that was it, now update-upgrade and install openssh-server in your brand new instance

NOTE: don't forget to make your new instance autostart,
in the host system run
virsh autostart 12.04



Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.