During our webdevelopment process, we often need to test the layout of our applications and websites in several browsers. Since running all of these browsers stand-alone is nearly imposible and applications such as IETester do not work flawlessly and cannot run on Windows 7, it would be nice to be able to run Virtual Machines for these browsers in order to have a "native" experience.
Currently, I am trying this using a Virtual Windows XP running IE7. My own setup is a Windows 7 RC1 64bit. Most of our websites and applications are developed on locally using mappings in the Windows hosts file.
The problem:
When navigating to http://mywebsite.localhost from within a Virtual Machine, the Virtual Machine's host file is queried instead of the actual OS on top of the Virtual Machine. Also, when the exact same mapping is added, the browser (obviously) still navigates to the "virtual localhost".
- Is there a way to make the Virtual Machine query the Windows 7 hosts file?
- Is there a way to make the Virtual Machine's localhost the same as the actual Windows 7 localhost?
Please read the whole instructions below before beginning them, and check what you would have to do to undo following them. This is from memory, I use a Mac now.
I assume you use VMware, and your VM [the usual abbreviation for Virtual Machine] in NAT networking mode.
I use the term host system for the operating system installation running VMware and guest system for the system running inside VMware, as per the usual VMware parlance.
Part I: Find the IP address of the host system on the NAT network that VMware simulates (normally, network 8):
Open a cmd.exe on the host machine and enter:
ipconfig /all
Note down the IP address for the VMware NAT interface
Part II: Making the hosts file uniform
Change the hosts file, %systemroot%\System32\Drivers\Etc\hosts, on your host system so that it uses the IP address of the VMware NAT adapter instead of 127.0.0.1 for the local web sites.
Make sure the web server listens on that IP address.
Part III: Making the hosts file available
On the host system, in the command line:
Make sure there is no sensitive information in that path.
Part IV: Fetching the hosts file
Make a batch file, c:\hostsfile\vm_hosts_copy.bat on the guest system, with these contents:
Import this into your registry:
If all that is done, you should be able to work with the same hosts file on your host system and all guest systems. The hosts file on the guest systems will get updated on every reboot. You only have to change it at the usual location, %systemroot%\System32\Drivers\Etc\hosts, on your host system.
You can use that on all your Windows VMs, and analogous scripts with smbclient and /etc/rc.local on most Unix systems for multiplatform testing.
Configure your VMs to use bridged networking instead of NAT (if not already done so). Now the VM is on the same network as the host.
Configure the guest VM's hosts file to be the same as your workstation (the VM host).
ie:
[IP of VM HOST] mywebsite.localhost
The correct answer for this already exists at Superuser https://superuser.com/questions/144453/virtualbox-guest-os-accessing-local-server-on-host-os - but I'll summarize here:
Just as
127.0.0.1
orlocalhost
are the magic addresses for each computer to refer to itself,10.0.2.2
is the magic address for a guest OS to refer to its host, at least in the VirtualBox world. My process for setting up a VirtualBox VM which can hit websites served locally on the host OS is:.ovf
file)C:\Windows\System32\Drivers\etc\hosts
and repeat any hacks you've done for the host OS, replacing127.0.0.1
with10.0.2.2
, e.g. you might add a line like10.0.2.2 local.dev.example.com
local.dev.example.com
) - it should load the page served by the host OSIf you don't hack your hosts files, you can just use
10.0.2.2
directly in the guest OS. Hope that helps!Sort of sideways, but if you're going to use alot of VM's for your testing, setup a DNS server on the host with a test zone which forwards to your regular DNS servers. Then change the host to use the local DNS server and the VM's should follow.
Anders
Since you have access to the Windows 7 filesystem from within the virtual machine, it seems like you could set up the VM so that it copies the Windows 7 hosts file to the VM's host file during startup. This is a theoretical rather than a practical answer, though, as I don't have Windows 7 available to try it with.
You should somehow arrange for your VMs to "see" each other via DNS or lmhosts feature. I would also avoid using pc names with localhost in it, you can use no suffix at all or smth like mypc.local
The primary LMHOSTS file is always located in the %systemroot%\System32\Drivers\Etc directory. With Microsoft TCP/IP, you can include other LMHOSTS files from local and remote computers.
Users can import the LMHOSTS file from remote computers on the network by
See also this MS KB Article.
The above makes me think you may be hitting a conceptual speedbump. Your VM will not (and should not) think of its host as localhost. It's a different logical device on the network.
Localhost is a reserved term, of sorts, which will always point to 127.0.0.1. Attempting to subvert it seems a lot like breaking the head off a sledgehammer to make it into a walking stick (instead of just using a stick).
DNS and host files exist for exactly this reason.
I would just put squid or another proxy on the host machine and have all the guests use that as the proxy so localhost would be the localhost of the host and the host's hosts file would be honored.