Linux : Configuring a network bridge for your Virtual Machines

My new center of interest those days being virtualization, I tried quite a few software starting with Xen, then QEMU, then KVM, and finally VirtualBox. But as far as giving a network access to the VM is concerned, I’ve always sticked to a network bridge for the reason that this makes the VM appear on the network just like any other computer of your network.

This post provided a sample script to setup a bridge suitable to use with all of the named virtualization softwares.

Here is the script I use for setting up the networking of a VM :

#! /bin/sh

# creating tap0 (a TAP device) and setting owner as the non-privileged user who
# will run VirtualBox
tunctl -u kattoo

# creating the bridge
brctl addbr br0

# stopping the initial networking
/etc/init.d/net.eth0 stop

# bringing up physical and virtual network interfaces
ifconfig tap0 up
ifconfig eth0 up
ifconfig br0 up

# connecting the TAP device and the physical NIC onto the bridge
brctl addif br0 tap0
brctl addif br0 eth0

# starting the bridge
/etc/init.d/net.br0 start

As you can see, this is pretty simple and straightforward, and there is much room for improvement 😉

A TAP device is a level 2 (think ethernet) virtual network interface which has one side connected to a process (in our case this will be VirtualBox or any other virtualization software such as Xen, QEMU, KVM …) and the other side is connected to the hosting Linux Kernel. In our case that end will be plugged in the network bridge so that every packet entering the bridge will be forwarded to the VM as well (and the other way round too).

This example is made with Gentoo Linux in mind, so you may have to adapt a bit for your own Linux distribution.

If you use VirtualBox, you can then set the network parameters for your VM as follow :

VirtualBox parameters for a VM using bridged networking
VirtualBox parameters for a VM using bridged networking

Should you have any question or improvement to suggest, feel free to hit the comments !

9 thoughts on “Linux : Configuring a network bridge for your Virtual Machines”

  1. First of all: THANK YOU!

    Second: HELP!
    I have a Debian web server VM I run from my ubuntu laptop. I would like to access it through the web browser. Note: I use a Linksys router at home, and it’s MAC-address filtered (only my laptop is allowed to use the router).

    What changes should I do on the batch file.
    Do I have to add the VM’s MAC address on the router?

    1. Hi Lamcro,

      I’m not completely sure I understood what you want to do. Let me rephrase what I believe is your situation : you have a laptop running Ubuntu on top of which you are running a Debian VM. That VM is a web server. The Ubuntu laptop is connected to a Linksys router through a WiFi connection and you want to access the web server from the Internet (that is from the “other side” of the router).

      Is that correct ?

      If it is, the you’ll need to put some kind of port forwarding at the router level, so that incoming connections to the port HTTP (tcp/80) will be redirected to the VM’s IP.

      Note : I don’t own a Linksys router myself, but I think the MAC filtering is only there to prevent the association to the Access Point, not to filter packets. It doesn’t hurt to allow the VM’s MAC though, but I don’t think it is needed.

      Let me know if you got it to work, or if I misunderstood you 🙂

      Stéphane

  2. The latest version of VirtualBox (2.2 at the time of writing) does not require setting up a bridge and attaching the physical and virtual network interfaces to it. You just select ‘bridged networking’ in the VM’s network settings and it Just Works(TM).

    I used to have various similar scripts for QEMU, Xen and pre-2.2 VirtualBox. I am happy that I will now put them into the archive 🙂

  3. Hello George,

    I haven’t have a chance to try VirtualBox 2.2 yet … so thanks for the information !

    My main problem is that it is not possible to give access to more than 1 CPU to a VirtualBox VM, so under heavy I/O load (Network + Disk), the performance crashes …

    => As you tried VirtualBox 2.2, did you notice any performance improvement ?

    Stéphane

  4. Kattoor,
    You are almost correct: I want to access the web server from my laptop’s web browser. I’m not even thinking of the “outer limits” right now. I just want to have a personal web server I can play with, regardless of having interness access or not.

    1. Hello Lamcro,

      Then there should not be any problem with the script provided in this post (except that you’ll probably have to adapt it for Ubuntu). Your webserver should be accessible from the Ubuntu host (as well as from any host on the same LAN should it matter).

      The networking parameters obviously have to be correctly set on the webserver though (at least an IP on the same network than the Ubuntu host).

      A little checklist to help troubleshooting your problem :
      1) Run an “ifconfig -a” on the Guest and on the Host, check they have IP addresses on the same network
      2) If you use bridged networking, then you’ll need to adapt/run the script, then check your VirtualBox settings for the network.

      => I’ve attached my VirtualBox networking parameters in the main post for your reference.

      Please keep us posted with the resolution of your problem.

      Stéphane

  5. Notaras,
    Ubuntu 8.10 has VBox 2.0.4 in their repository.

    I’ll have to download it myself.

    Thanks for the info.

  6. Hi Stéphane,

    Unfortunately, I have never really stressed a virtual machine in Virtualbox, so I cannot comment on its performance. Generally speaking, Vbox 2.2 seems like the best release so far.

    Also, it is the first time I use a pre-compiled version of Virtualbox in order to make use of the SATA controller, which is a closed source feature, but it is said to improve the overall virtual disk performance.

    I was not aware of the issue with the utilization of more than 1 cpu.

  7. LINUX bridge is a way to connect two Ethernet segments together in a protocol independent way.Packets are forwarded based on Ethernet address, rather than IP address (like a router). Since forwarding is done at Layer 2, all protocols can go transparently through a bridge.

Comments are closed.