Thursday, March 26, 2009

Virtual Networking With VirtualBox on OSX

I've finally made the switch, and I'm totally convinced that VirtualBox is my friend. For one thing: I can run a Windows and a Linux guest and keep rocking on OSX with no problems. Less than 700K of real memory used.

So anyway, this took a lot of setting up.
  • You can skip most of this and use host networking on one of the Ethernet adapters. The problem is that if the network goes down, you are hosed.
  • First, get the virtual networking software from http://tuntaposx.sourceforge.net. Restart the computer after install and it should be running (without any changes).
  • Time to set up the virtual network adapter. Note: you'll need to do this BEFORE running the VirtualMachine (otherwise it cannot bind or something) and you need to keep this shell window open. Otherwise you are killing the network adapter. Also note, the first and second group of instructions are from here with no real changes.
sudo bash # we go superuser
# might need to enter pass here
exec 4<>/dev/tap0  # opens device, creates interface tap0
ifconfig tap0 10.10.10.1 10.10.10.255
ifconfig tap0 up
dd of=/dev/null <&4 & # continuously reads from buffer and dumps to null
  • do not close that window (if you type jobs you'll see the buffer reading is continuing in the background)! In fact, leave it alone. Open another terminal window. (Note: make this into a shell script by stripping off the sudo command, which you'll have to type, and dumping the contents in whatever.sh. Then you can run source whatever.sh)
  • Now you need to alter the virtual machine. We do this stuff once to get the VM set up. List the VMs
VBoxManage list vms
  • Now you'll need to pick the name of your machine of interest from the big text wad it throws you. It's the natural name, like you see here:
VBoxManage modifyvm "that craazy linux box mon" -nic2 hostif #make the second network adapter host-networking
VBoxManage modifyvm "that craazy linux box mon" -hostifdev2 tap0: # connect that adapter to tap0: which is our virtual network (exciting, eh?)
  • Now we're almost done. In fact, if your machine boots, it means that the adapter is there and you can almost get moving.
  • Configure Linux for the adapter
# On the guest machine, run
sudo nano /etc/network/interfaces
# add these lines
auto eth1 iface eth1 inet static address 10.10.10.200 netmask 255.255.255.0 # netmask works, but it could be better I think
# restart networking
sudo /etc/init.d/networking restart
  • Now if that worked, you should be able to ping 10.10.10.200 from your host.
  • Edit /etc/hosts and put in a line that says
10.10.10.200      virtbox

  • and now try pinging virtbox (ping virtbox) from the host
  • Now set up MySql so it's friendly (from my old instructions):
  1. Now, getting MySql to work. Change the /etc/mysql/my.cnf
    bind-address = 10.10.10..200
    which used to be localhost
  2. from the MySql client (you must be root!)
    grant all privileges on *.* to 'root'@'10.10.10.200';
  3. Restart MySql: sudo /etc/init.d/mysql restart
  4. Now you can connect with MySql from your host using the command-line thinger or the MySql Query whatsamagig
Thanks to the guy who wrote the post on the VirtualBox forums,  VirtualBox (of course), and most of all, a guy who makes freeware to solve problems, the maker of tun tap, Mattias Nissler (who I don't know).

2 comments:

BabySnake said...

Thanks that's awesome.

But do you know how to do if u want w (or more) virtual servers set up this way?, and you want them to be able to communicate too.

Daniel Rosenstark said...

Using host-only networking on VirtualBox will let the various virtual appliances talk to each other via their IP addresses.

Thanks and good luck!