Using a Virtual Windows Machine to browse a local website: is it possible?

7,015

Solution 1

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.

Solution 2

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!

Solution 3

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

Share:
7,015

Related videos on Youtube

Aron Rotteveel
Author by

Aron Rotteveel

Words describing me in third person.

Updated on September 17, 2022

Comments

  • Aron Rotteveel
    Aron Rotteveel almost 2 years

    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?
  • Aron Rotteveel
    Aron Rotteveel about 15 years
    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.
  • Kara Marfia
    Kara Marfia about 15 years
    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.