Virtual Machine / Host File Settings to Test Web Pages Served on Host Machine

Many developers install virtual machines on their local development machines in order to test their web pages in different versions of IE. On your local machine, you may run a local server and test pages on localhost (http://localhost). If you’ve installed VirtualBox and a VM like Win 7 with IE 8, then you’ll probably want to be able to open IE 8 in that VM and go to localhost to see your pages. The default Network Adapter settings in VirtualBox is NAT as shown below.

ie8-network-adapter

While you’ll be able to access pages on the internet like www.google.com, you won’t be able to go to localhost to see pages on your host machine. One way to solve this is by editing your host file in your VM to point to the IP address of your host. On your host machine, open a command prompt and enter ipconfig to get your host machine’s IP address.

ipconfig-host

In this case, my host IP address is 10.100.10.75. Now, edit your host file in your VM and add an entry like

10.100.10.75 localhost

hosts-file-ip-address

Now, in your VM, if you go to localhost, you’ll get the ip address of your host machine’s which is set up to serve your local web pages. While this works, your host machine’s IP address may change often, e.g. if it’s a laptop which you take home every day. To prevent having to change your hosts file every time your host machine’s IP address changes, you can set localhost to point to your VM’s default gateway since your host machine acts as a router for your VM. In your VM, run ipconfig to get the default gateway IP address.

ipconfig

In this case, it is 10.0.2.2. Now, update your hosts file to use this value instead, e.g.

10.0.2.2 localhost

and you should be able to see your host machine’s pages without ever having to update your VM’s hosts file whenever your host machine’s IP address changes.