How To: Make VirtualBox Use Your Router’s DHCP to get an IP Address r3dux | September 5, 2009
Update: The method below for getting a virtualbox IP from your DHCP works (in linux) – but it turns out there’s an easier way (kindly pointed out by Mike in the comments below). You can just change your VirtualBox network settings from NAT to Bridged Adapter and point it at eth0/wlan0 or whichever connection is being used for networking. Then, optionally, you can configure the MAC address of the bridged adapter and set your router to assign a specific IP to a specific bridged adapter. Also, the built-in Bridged Adapter method works to deploy solutions from XNA Game Studio to my Xbox 360, so I’m rapt! Thanks, Mike!
Note: The below bit is for linux only, the above method should work on any host OS!
VirtualBox is an awesome bit of kit and I <3 it long time ten-dorrah.
But by default when your virtual copy of Windows/Linux/Solaris/Whatever grabs an IP address, it does so through NAT, and at version 3.0.4, this means it gives us a default Category A network address (i.e. 10.x.y.z).
It’s a working cat-A address, as in it’s fully functional and can talk to the Internet and all that, but sometimes life is a lot easier if you have an IP in the same range as the DHCP pool your router is dishing out. For example, my lappy is 192.168.1.101 internally, my Wii might be 192.168.1.102, the NAS .103 etc, so I want my virtualboxen to take addresses like .104, .105 and such.
I’m doing this to bridge my wireless connection on wlan0, if you’re bridging an ethernet connection substitute eth0 or whatever connection as necessary.
Also, to perform the bridging using this method, you’ll need some tools (feel free to sudo apt-get install NAME-OF-TOOL as necessary):
- 1.) uml-utilities
- 2.) parprouted
- 3.) bcrelay
Now, with that lot installed, run the following commands (provided here in bash script form):
#!/bin/bash # To use this script you will need the following utilities installed: # 1.) uml-utilities 2.) parprouted 3.) bcrelay # sudo apt-get install 'em # Enable IP forwarding sudo sysctl net.ipv4.ip_forward=1 # Create a TAP as your current logged in user (replace with -u YOUR-USER-NAME if you want...) sudo tunctl -u $USER # Tell tap0 to respond to ARP (Address Resolution Protocal) packets sudo sysctl net.ipv4.conf.tap0.proxy_arp=1 # Give your TAP (tap0) an IP address and enable it (make sure the IP address is OUTSIDE the DHCP range on your router) sudo ip addr add 192.168.1.150/32 dev tap0 sudo ip link set tap0 up # Run parprouted and make it add routes automatically for wlan0 and tap0 as needed # Make the command "parprouted -d wlan0 tap0" to display routes added and additional info. No sudo necc. for this one. parprouted wlan0 tap0 # Use bcrelay on your net connection and TAP to "rewrite the layer-2 header and forward broadcast messages between network interfaces" # The "-d" in this instance MAKES bcrelay work as a daemon. Need to sudo this or it doesn't work. sudo bcrelay -d -i tap0 -o wlan0
Now, fire up VirtualBox and for your machine of choice change the network selection from NAT to tap0 as shown:
Then boot up your virtual machine and check the IP:
Great. Super. Smashing. =D
Note: The entire reason I wanted to grab an IP from the router was so my virtual copy of XP could be on the same network as my XBox 360, so I could deploy games to it through XNA Studio 3.1, however XNA Studio is very fussy about timing when it comes to registering the 360, and although it can see the 360 using the bridge, and it tries to connect, it times out before it can fully establish a connection. I guess I’ll have to go with an IP routes method of bridging if I want it to work for that purpose, but as yet I haven’t quite figured it all out. Will keep trying when I have time, or if you know a way, feel free to call me technically incompetent and sling a solution in the comments! Cheers!