Wednesday, March 6, 2013

How to configure Virtual Box with internet and host access


I have had such a hard time locating the information I needed to get a VirtualBox VM to use both NAT and Host networks so that the VM could see the internet and the host could see the VM with a known IP address that I decided to write this post.

The configuration in this post is as follows:
·         Host O/S is Windows 7 (64 bit)
·         VirtualBox is version 4.2.6 r82870
·         Guest O/S is Oracle’s Enterprise Linux 5 (64 bit)
·         Guest has a web server installed and running.

To make this work requires the following steps:

Setup the Host Network Adapter in VirtualBox

1.       Start VirtualBox
2.       Select the File menu and Preferences
3.       On the left side, select Network

4.       In the network dialog, select the  to add a new host only network



5.       Follow the prompts for locating the driver for the new network. On my machine, the file is located at “C:\Program Files\Oracle\VirtualBox\drivers\network\netadp”
6.       Double click on the new adapter; “VirtualBox Host-Only Ethernet Adapter #3” in this example.

7.       Change the settings as shown here



8.       Select OK to close the Host-Only Network Details
9.    Select OK to close the VirtualBox – Settings dialog.

Set up the VirtualBox VM

1.       With the VM shutdown, select the Settings for the VM and open the Network dialog and set Adapter 1 as shown here and refresh the MAC Address. No Port Forwarding is required.



2.       Set Adapter 2 as seen here and refresh the MAC Address as well



3.       Select OK to close the settings dialog.
4.       Start the VirtualBox VM

Set the Network in the VirtualBox VM

1.       Log in as root
2.       Select System -> Administration -> Network



3.       It should look similar to the following



4.       Select the eth0 device and Edit. Verify that it looks like the following. If not then change it.
eth0 is set for DHCP



There are no static routes defined



and the hardware matches the VirtualBox Adapter 1 hardware and MAC address.



5.       Select OK to save any changes
6.       Select the eth1 device and Edit. Verify that it looks like the following. If not then change it.
Set the IP Address and Subnet mask



There are no static routes defined



and the hardware matches the VirtualBox Adapter 2 hardware and MAC address.



7.       Select OK to save the changes
8.       Add your new IP Address and host name to the hosts file



9.       Save and exit the network configuration
10.   Open a terminal and restart the network



11.   Restart the VirtualBox VM

Update the Windows Hosts File

1.       Edit the hosts file (c:\windows\system32\drivers\etc\hosts). Note that Windows hides the etc folder so you will need to either show all hidden folders or type the path in.
2.       To the hosts file add
10.88.12.4  dev.example.com dev
3.       Save and close the host file.

Verify Virtualbox VM can reach the internet and itself

1.       When the VirtualBox VM is restarted, log in as a non-root user
2.       Start the web browser
3.       Verify internet connection by selecting a known site such as www.google.com
4.       Verify the local web server by http://dev (or whatever your VirtualBox VM hostname is)

Verify the Windows Host can connect to the VirtualBox VM

1.       Open a web browser
2.       Verify internet connection by selecting a known site such as www.google.com
3.       Verify the local web server by http://dev (or whatever your VirtualBox VM hostname is)


In this configuration, your host can access the VirtualBox VM and the VirtualBox VM can access the internet but the VirtualBox VM is not visible to others.