Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy

How To: Make VirtualBox Use Your Router’s DHCP to get an IP Address in Linux

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.

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 forwardingsudo 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)sudoip addr add 192.168.1.150/32 dev tap0
sudoip linkset 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!

14 thoughts on “How To: Make VirtualBox Use Your Router’s DHCP to get an IP Address in Linux”

Thanks! This is great info. I have a problem though. I followed these steps, but for other reasons I now want to roll back these changes (in other words, everything that was in the “bash script form” box). Could you please help me out with the reverse sequence?

Then you could restart the networking part of linux with sudo /etc/init.d/networking restart, this will leave bcrelay and parprouted still running, which you can then kill through the System Monitor, or by typing ps -A | grep bcrelay and ps -A | grep parprouted to find the process IDs and then kill [process-ID] or killall -9 [process-ID them. (i.e. kill 5217 or such).

Finally, to make VirtualBox use NAT again just shut down the virtual machine (not suspend/save status) and change the network from “Attached to: Bridged Adapter” back to “Attached to: NAT” in the virtual machine settings.

I also had this same problem. When using NAT I was unable to browse the other Windows systems on my home network, access my networked printers, etc.

But instead of doing what you are proposing I simply use the “Bridge” mode of network device. If you then click on the little gear to the right of the drop-down menu for “Attached to:” you can also specify a specific MAC address. I set this so that each VM has a unique MAC address and I then configure my firewall router to assign it a specific IP address.

Using specific IP addresses allows me to have a hosts file that lists all possible host addresses for the various *nix systems, or in a NIS/DNS/Whatever database.

I run VirtualBox on top of Solaris. I have not tried it on top of Linux, as my versions of Linux run inside of Virtual Box VMs. I just wanted to mention this in case it is also an option when running VB on Linux systems. It could save you a lot of trouble.

How would I do this with a Windows XP host and a Linux guest? Or Solaris guest. I have no problem with the NAT option, but whenever I select Bridged, Internal, or host-only, the names come back as “Not Selected” with no drop down options. In VMware, it created virtual adapters to get around this problem but no obvious solution with Windows XP.

Using VirtualBox 4.0 on Windows7 with Ubuntu 10.10, I just needed to change Bridge Mode.
Didn’t need to run any commands. After pressing OK Ubuntu recognized a change in the network and automatically updated everything, including getting an IP from the DHCP.

Now I can seemlessly continue using Windows7 for my daily stuff and also set up a Web Server in Ubuntu, which is easier to handle.

Big thanks for the tutorial, even though I didn’t follow it, it pointed me to the right direction.

You can manually assign an IP on the 10.0.2 (default network) to your system, then set the DNS servers manually to reflect that on your operation network (outside of your virtual box). this will NAT for IP traffic and keep your system from being exploited from the outside since private space isn’t routable (normally) unless defined at your layer 3 device while providing accurate DNS. This allows resolution for all network systems outside as well as inside your Virtual Box as long as they are domain joined. if you do not join the other VB systems then you could add them into your DNS server or provide a hosts file for them locally.