VirtualboxHostNetworkingAndWIFI

Differences between revisions 5 and 6
Revision 5 as of 2008-06-08 09:40:30
Size: 4711
Editor: dyn-91-164-194-237
Comment:
Revision 6 as of 2008-06-08 11:17:29
Size: 4718
Editor: dyn-213-36-168-219
Comment:
Deletions are marked like this. Additions are marked like this.
Line 10: Line 10:
Tested with Innotek Virtualbox 1.5.6, Sun xVM Virtualbox 1.6.0 and Ubuntu 8.04 (Hardy) Tested with Innotek Virtualbox 1.5.6, Sun xVM Virtualbox 1.6.0, 1.6.2 and Ubuntu 8.04 (Hardy)

Virtualbox host networking and WIFI

Scope of this howto

While being connected to your router (thus the Internet) via your computer WIFI card, enable host networking on a different subnet by bridging your unused ethernet card.

Your Virtualbox machines will be connected to the Internet as well.

Tested with Innotek Virtualbox 1.5.6, Sun xVM Virtualbox 1.6.0, 1.6.2 and Ubuntu 8.04 (Hardy)

Pre-requisites

One computer with:BR WIFI controller eth0BR ethernet controller eth1BR VirtualboxBR Internet connection thru eth0BR

All the bridge creation and host interface creation knowledge comes from Virtualbox help contents. (see chapter Host Interface Networking and bridging on Linux hosts)

How to - by example

Consider a connection to the internet via eth0 (WIFI) with IP 192.168.0.2 . BR The bridge IP address will be 192.168.1.254 (note the difference of subnet between the wifi and the bridge). BR Bridge br0 will include eth1 (ethernet card) and host interface tap0. BR

I will assume that virtualbox is up and running for the user joe.

Install the necessary tools

sudo apt-get install bridge-utils uml-utilities
sudo gpasswd -a $USER uml-net

At this stage /dev/net/tun owners should be root.uml-net, change accordingly if this is not the case.

ls -l /dev/net/tun 
crw-rw---- 1 root uml-net 10, 200 2008-04-11 23:55 /dev/net/tun

Edit /etc/network/interfaces

A fresh /etc/network/interfaces should look like the following (or be quite similar)

auto lo
iface lo inet loopback
auto eth1

Edit it and change it to

auto lo
iface lo inet loopback

auto tap0
iface tap0 inet manual
up ifconfig $IFACE 0.0.0.0 up
down ifconfig $IFACE down
tunctl_user joe    ###replace joe with the name of your user member of vboxusers and uml-net groups##

auto br0
iface br0 inet static
address 192.168.1.254
netmask 255.255.255.0
    bridge_ports eth1 tap0
    bridge_maxwait 0

auto eth1

Add as many host interfaces as desired on the same principle.

Restart networking

sudo invoke-rc.d networking restart

Enable IP forwarding

Enable IP forwarding by uncommenting line 38 in /etc/sysctl.conf

net.ipv4.ip_forward=1

Create a firewall service

While it is not necessary to create a firewall service to enable masquerading, it will be much easier to activate if you do so.

sudo vi /etc/init.d/firewall
#/bin/bash

start() {
        echo "Creating iptables rule"
        iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
}

stop() {
        echo "Flushing iptables"
        iptables -P INPUT ACCEPT
        iptables -P FORWARD ACCEPT
        iptables -P OUTPUT ACCEPT
        iptables -t nat -P PREROUTING ACCEPT
        iptables -t nat -P POSTROUTING ACCEPT
        iptables -t nat -P OUTPUT ACCEPT
        iptables -F
        iptables -t nat -F
        iptables -X
        iptables -t nat -X
}

case $1 in
        start)  start;;
        stop)   stop;;
        restart) stop;
                        start;;
        status) /sbin/iptables -L
                /sbin/iptables -t nat -L
                exit 0;;
        *)      echo "Usage: firewall {start|stop|restart|status}"
                exit 1
esac
exit

Make the file executable

sudo chmod u+x /etc/init.d/firewall

Create links for the service to be started/stopped automatically

sudo update-rc.d firewall defaults

Start the service

sudo invoke-rc.d firewall start

Host interface IP in the virtual machine

Assign tap0 as a host interface to your virtual machine.

2 possibilities (at least) for the host interface IP address:

No DHCP server

If you have no dhcp server serving the 192.168.1.0 range, you will need to assign an IP address in the 192.168.1.0 range to your virtual machine once it is started.

sudo ifconfig eth0 192.168.1.253

It will be necessary to add 192.168.1.254 as a default route as well

sudo route add default gw 192.168.1.254

If you want to make it permanent in you virtual machine, edit /etc/network/interfaces of your virtual machine.

auto eth0
iface eth0 inet static
address 192.168.1.253
netmask 255.255.255.0
gateway 192.168.1.254

DHCP server

You have a dhcp server serving on the 192.168.1.0 range either locally (laptop): make sure the gateway given by dhcp is the ip of your bridge br0

or on your ethernet LAN connection: nothing to do (in that case, your access to the internet will occur thru ethernet most certainly and not thru WIFI)

James Dupin


CategoryNetworking

VirtualboxHostNetworkingAndWIFI (last edited 2009-12-05 10:39:43 by 229)