Tuesday, January 31, 2012

Setting Up a Bridged Network Interface for KVM in Ubuntu

The process for setting up a network bridge for KVM in Ubuntu is actually fairly simple. For the purposes of this demo, we will assume that you already have KVM/QEMU installed from the Ubuntu Repos and that you are not using Network Manager. If you are using Network Manager, you must disable it prior to following these instructions. I am using  Ubuntu Server 11.10 (Oneiric Ocelot), but this should work for 10.04 (Lucid Lynx), 10.10 (Maverick Meerkat) and 11.04 (Natty Narwhal).

Bridged mode is really important if you plan on using your VMs for any sort of server. In NAT mode they will not be able to communicate with any other machines on your local network. Bridged mode makes it so your VMs share the physical interface of your KVM host.


First, we need to install some software:
sudo apt-get install libcap2-bin bridge-utils


From (https://help.ubuntu.com/community/KVM/Networking):


Give qemu the inheritable CAP_NET_ADMIN capability, for 64-bit:
sudo setcap cap_net_admin=ei /usr/bin/qemu-system-x86_64


or for 32-bit:
sudo setcap cap_net_admin=ei /usr/bin/qemu


We will now edit /etc/network/interfaces to add the bridge. Assuming your current config looks like this:
auto lo
iface lo inet loopback


auto eth0
iface eth0 inet static
        address 192.168.0.10
        network 192.168.0.0
        netmask 255.255.255.0
        broadcast 192.168.0.255
        gateway 192.168.0.1


Change it to look like this:
auto lo
iface lo inet loopback


auto eth0
iface eth0 inet manual


auto br0
iface br0 inet static
        address 192.168.0.10
        network 192.168.0.0
        netmask 255.255.255.0
        broadcast 192.168.0.255
        gateway 192.168.0.1
        bridge_ports eth0
        bridge_stp off
        bridge_fd 0
        bridge_maxwait 0


If you are using DHCP, you would want it to look something like this:
auto lo
iface lo inet loopback


auto eth0
iface eth0 inet manual


auto br0
iface br0 inet dhcp
        bridge_ports eth0
        bridge_stp off
        bridge_fd 0
        bridge_maxwait 0


Now we just need to restart networking:
sudo /etc/init.d/networking restart


That's it. You can now start using your new bridge with your VMs. All you have to do is select the bridge created in the VM settings in virt-manager.







4 comments:

  1. Hi; when I try and run sudo setcap cap_net_admin=ei /usr/bin/qemu on my (fairly clean) Ubuntu 12.04, I get the error "Failed to set capabilities on file `/usr/bin/qemu' (No such file or directory)
    ". What I tried instead was sudo setcap cap_net_admin=ei /usr/bin/qemu-system-i386. And then proceeded as directed.

    Perhaps you can add a section at the end to explain how to test that everything is working properly?

    ReplyDelete
  2. When I try the approach above, my network stops working ie. my browser is no longer able to access common sites like Google. I reverted all the changes.

    ReplyDelete
    Replies
    1. I'm not sure what's wrong. I followed my How-To when I set up some 12.04 boxes and I don't remember having to change anything. That was a few months ago though.

      Delete
  3. I'm using Virt-Manager and stuck after setting the bro interface. Your screenshot helps me. Thanks.

    ReplyDelete