October 4, 2008

Host Networking for VirtualBox

Seems like about once or twice a month I have somebody contact me about Host Networking in Sun’s Virtual Box product. Everybody knows that when you install VB and then create an instance of a guest OS that it by default selects NAT as the network method. The problem of course is its a one way communication path. The solution of course is setting up host networking.

How? Well I do it by a prebuilt bash script I have developed. Its a 3 step process and is reproducible every time for most Linuxes –

  1. Run the script as root to set up the necessary tap/tun relationships. Like, ./bridge.sh . Now by default my script sets up 3 taps. Does 90% of what I need for tests, etc.
  2. Take your guest OS and open up the options tab in VB. Go to netwoking and select host networking and add tapX. Where X is a value from 0 to 2. Make sure you don’t launch 2 guest OS’s with the same tap.
  3. Launch the guest OS. If you want do a ifconfig from command line and see what IP address it has gathered.

Couple of requirements. 1) Bash must be on your system. 2) Once you have copied the script and saved it you must make it executable. 3) You must have a visible DHCP service internal to your LAN for the script to work. 4) You must have the dhcp client and bridge-utils loaded. They are available in the repositories for Ubuntu/Debian. 5) DO change the code segment — ‘ -u rootuser’ — to an account resident on your machine.

Here it is –
————————————————————————————————————–
#!

#bridging script for virtualbox host networking

modprobe tun
tunctl -t tap0 -u rootuser
tunctl -t tap1 -u rootuser
tunctl -t tap2 -u rootuser
chmod 666 /dev/net/tun

ifconfig eth0 0.0.0.0 promisc
ifconfig tap0 0.0.0.0 promisc
ifconfig tap1 0.0.0.0 promisc
ifconfig tap2 0.0.0.0 promisc

brctl addbr br0
brctl addif br0 eth0
brctl addif br0 tap0
brctl addif br0 tap1
brctl addif br0 tap2
dhclient br0

————————————————————————————————————–

The script could be expanded reasonably by adding more ifconfig, tunctl and brctl lines to the above. But with this set up I have 3 virtual devices — 3 Guests + 1 Host. Also realize that by opening this up you have to apply the same reasonable security to the Guests as you would the Host.

I have had 1 or 2 folks ask why I run this as a separate script. Simplicity is the primary one. When I fire up the Host in the morning I know it comes up in a std mode of operation without the bridging code in the way. I only need turn on the virtual environment when I will work in that mode. One mouse click and two keystroke at command line and I am ready for the day. The other is I build Guests to do certain things. Servers like DB, mail and web usually are set to tap0 or tap1. I then reserve tap2 for any specialized client I need.

Try it. I think you will find it useful and takes the mystery out of Host networking without really trying. Its a good thing.

Filed under Applications, Freebies, Networking, tech tips by Dr. Dog

Permalink Print Comment

Comments on Host Networking for VirtualBox »

October 4, 2008
(Pingback)

Host Networking for VirtualBox · networking-the.info @ 4:53 pm

[...] Here is the original:  Host Networking for VirtualBox [...]

October 6, 2008

Mark Baaijens @ 2:58 am

There’s another way for accessing virtual machines from the outside: port forwarding.
Here’s some background: http://vboxtool.sourceforge.net/portforwarding.html

Benefits
- works with Network Manager on Ubuntu
- easy to configure, just three commands per session to apply
- very flexible: configuration can take place on the fly
- client session does not have to be configured with a static IP-address
- no need to retrieve the session’s IP address
- flexible portability; when the session is moved to another server, it is very easy to re-configure because port forwarding configuration is no part of the session (vdi file)
- hides implementation; from the client’s view, the server just publishes a network service

For easy configuration, I developed VBoxTool, with which it is possible to define port forwarding for any number of guests, with as many port pairs as needed, all in one command (’vbox autostart’) and one config file. Feel free to test VBoxTool and to provide any feedback on this.
http://vboxtool.sourceforge.net/

Leave a Comment