I've been trying for weeks to figure out the right network configurtion for sharing a range of public IPs with KVM virtual machines running on my server,
but so far with little luck and with the help of the friendly ServerFault community, I've managed to make it work. You can find my working setup below:
My ISP routes all the traffic to
192.168.8.118 (so that needs to be the primary IP of eth0), but I have
192.168.239.160/28 to my disposition.
/etc/network/interfaces on the host machine:
# Loopback device: auto lo iface lo inet loopback # device: eth0 auto eth0 iface eth0 inet static address 192.168.8.118 broadcast 192.168.8.127 netmask 255.255.255.224 gateway 192.168.8.97 pointopoint 192.168.8.97 # This device acts as gateway for the bridge, so provide a route. up ip route add 192.168.8.118/32 dev eth0 scope host # device: br0 auto br0 iface br0 inet static bridge_stp off bridge_maxwait 0 bridge_fd 0 address 192.168.239.174 broadcast 192.168.239.175 netmask 255.255.255.240 gateway 192.168.8.118 # Create and destroy the bridge automatically. pre-up brctl addbr br0 post-down brctl delbr br0 # Our additional IPs are allocated on the bridge. up ip route add to 192.168.239.160/28 dev br0 scope host
I have configured a virtual machine like this:
sudo ubuntu-vm-builder kvm precise \ --domain pippin \ --dest pippin \ --hostname pippin.hobbiton.arnor \ --flavour virtual \ --mem 8196 \ --user mikl \ --pass hest \ --bridge=br0 \ --ip 192.168.239.162 \ --mask 255.255.255.240 \ --net 192.168.239.160 \ --bcast 192.168.239.175 \ --gw 192.168.239.174 \ --dns 220.127.116.11 \ --components main,universe \ --addpkg git \ --addpkg openssh-server \ --addpkg vim-nox \ --addpkg zsh \ --libvirt qemu:///system ;
If I inspect the virtual machine's XML definition, its network interface is defined like this:
<interface type='bridge'> <mac address='52:54:00:b1:e9:52'/> <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface>
When I (re)start the virtual machine,
/var/log/syslog receives these lines:
Jul 20 03:13:02 olin kernel: [ 4084.652906] device vnet0 entered promiscuous mode Jul 20 03:13:02 olin kernel: [ 4084.686388] br0: port 2(vnet0) entering forwarding state Jul 20 03:13:02 olin kernel: [ 4084.686394] br0: port 2(vnet0) entering forwarding state
My server is running Ubuntu 12.04 64-bit with kernel 3.2.0-26-generic (from Ubuntu). I'm running
libvirt-bin 0.9.8-2ubuntu1 and
iptables on the host machine is currently set up to allow all traffic (to eliminate that as a problem source), and I have enabled forwarding of both ipv4 and ipv6 traffic.
When I log in to the guest via SSH from the host, I have no internet connection inside the guest OS. The guest’s
/etc/network/interfaces looks like this:
auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.239.162 netmask 255.255.255.240 network 192.168.239.160 broadcast 192.168.239.175 gateway 192.168.239.174 # dns-* options are implemented by the resolvconf package, if installed dns-nameservers 18.104.22.168 dns-search pippin
Now it works
The configuration outline above actually works as I want it to. Refer to the edit history if you want to see my earlier attempts.