Lukas Z's Blog

How to Set Up Networking With VMs on a Hetzner Server Running Ubuntu and KVM

I’ve recently rented a new server from Hetzner and decided to run virtual machines on it.

Here’s how to set up networking so the vms can connect to the Internet (and the other vms or host) while being reachable from the Internet themselves.

The assumptions are:

  • Host with one public ip (IP_HOST here)
  • Two additional public ips (IP2** + **IP3)
  • Ubuntu 12.04 LTS on all systems
  • Virtualization with KVM

On the host:

Install kvm, create your VMs (you can connect to them using the visual admin tool (virt-manager) and ssh -X) and configure your host:

host:/etc/network/interfaces

auto lo
iface lo inet loopback

auto  eth0
iface eth0 inet static
  address   IP_HOST
  broadcast IP_HOST_BROADCAST
  netmask   IP_HOST_NETMASK
  gateway   IP_HOST_GATEWAY
  pointopoint   IP_HOST_GATEWAY

up route add -net IP_HOST_NETWORK netmask IP_HOST_NETMASK gw IP_HOST_GATEWAY eth0

auto virbr1
iface virbr1 inet static
  address IP_HOST
  netmask 255.255.255.255
  bridge_ports none
  bridge_stp off
  bridge_fd 0
  pre-up brctl addbr virbr1
  up ip route add IP_1/32 dev virbr1

auto virbr2
iface virbr2 inet static
  address IP_HOST
  netmask 255.255.255.255
  bridge_ports none
  bridge_stp off
  bridge_fd 0
  pre-up brctl addbr virbr2
  up ip route add IP_2/32 dev virbr2

On Virtual Machine 1:

vm1:/etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
    address IP_1
    netmask 255.255.255.255
    gateway IP_1_GATEWAY
    pointopoint IP_1_GATEWAY
    dns-nameservers 213.133.98.98 213.133.99.99 # Hetzner's

On Virtual Machine 2

vm2:/etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
    address IP_2
    netmask 255.255.255.255
    gateway IP_2_GATEWAY
    pointopoint IP_2_GATEWAY
    dns-nameservers 213.133.98.98 213.133.99.99

And that’s basically it. However, in KVM config (host) you should match the MAC-addresses of your interfaces like this:

On the host run ifconfig to obtain the MACs, you should see something like this:

host:shell
virbr1    Link encap:Ethernet  HWaddr HW_1
          inet addr: IP_HOST  Bcast:0.0.0.0  Mask:255.255.255.255
          inet6 addr: IPV6_ADDR_VIRBR1/64 Scope:Link
          ...

virbr2    Link encap:Ethernet  HWaddr HW_2
          ...

Copy the HWaddr-values to the config files for the respective VMs, for example in my case for VM1 to /etc/libvirt/quemu/vm1.xml:

host:/etc/libvirt/quemu/vm1.xml
...
</controller>
<interface type='bridge'>
  <mac address='HW_1'/>
  <source bridge='virbr1'/>
  <model type='virtio'/>
  <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<video>
...

And do the same with Virtual Machine 2.

Restart networking (and virtual machines if needed) and everything should work. And you can reach your virtual machines from the outside (f.ex. with ssh) and they can connect out, as well.

P.S.: You can follow me on Twitter.

Comments

Webmentions