5

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?
4

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:

rmtshare \\name-of-host-system\hostspath=%systemroot%\System32\Drivers\Etc /grant Everyone:r

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:

copy \\name-of-host-system\hostspath\hosts %systemroot%\System32\Drivers\Etc

Import this into your registry:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run]
"CopyHostsFile"="c:\hostsfile\cm_hosts_copy.bat"

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.

1

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

1

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 or localhost 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:

  1. Download and install VirtualBox and the Extension pack https://www.virtualbox.org/wiki/Downloads
  2. Download and install a guest OS, most likely from Microsoft https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/ - as of the time of this writing, you shouldn't need to edit any of the pre-configured settings (install the VM by double-clicking the orange cube .ovf file)
  3. Install the guest additions
  4. Change permissions of and edit the hosts file, C:\Windows\System32\Drivers\etc\hosts and repeat any hacks you've done for the host OS, replacing 127.0.0.1 with 10.0.2.2, e.g. you might add a line like 10.0.2.2 local.dev.example.com
  5. fire up a web browser and hit the URL you masked in step 4 (local.dev.example.com) - it should load the page served by the host OS

If you don't hack your hosts files, you can just use 10.0.2.2 directly in the guest OS. Hope that helps!

0

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

0

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.

1
  • Thanks for the answer; this is indeed possible, but even if I'd copy the hosts file it would still not work. The VM's localhost is local only to the VM and cannot seem to access the localhost ran on my Windows7 install. – Aron Rotteveel Jun 24 '09 at 9:07
0

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

  • using #INCLUDE statements in the LMHOSTS file
  • clicking Import LMHOSTS on the WINS Address tab of the Microsoft TCP/IP Properties dialog box.

See also this MS KB Article.

0

Is there a way to make the Virtual Machine's localhost the same as the actual Windows 7 localhost?

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.

2
  • Thanks for your reply; since you state that DNS and hosts files exists exactly for this reason, could you please eleborate? Although I understand your point, this does not actually help fix this specific issue. – Aron Rotteveel Jun 24 '09 at 13:40
  • I guess it's not clear why you need to redirect localhost? Your specific issue is unclear. Using a host file or DNS server is far easier, so I'd need details about why changing localhost is the only solution that works in order to come up with additional options. – Kara Marfia Jun 24 '09 at 15:03
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.