Posted on August 9th, 2008 at 2:08 am.

So here’s the situation. You’ve got a development copy of a website on your local Macbook and you’ve been making some changes to it. Everything looks great but you want to make sure you haven’t made changes that stop the site rendering the way you expect in Internet Explorer or Firefox 2. Usually you’d make use of your corporate network computers to test the site but your working remotely so don’t have that luxury. You do, however, have a couple of virtual machines running Windows XP and different versions of browsers. The question is, how do you test the changes without committing them to the live site?

The answer is remarkably simple and allows you to test any site hosted on your local machine with browsers installed on your virtual machines. In all of this I am assuming your running Mac OS X and Windows XP in the virtual machine. That being said, as long as your know your hosts IP address and the virtual machine is running Windows 95 upwards this will work. By host I mean the machine which you run VMWare or Parallels on.

What you need

  • A virtual machine application such as VMWare or Parallels
  • A website hosted on your local machine

What to do

With your virtual machine turned off, edit it’s settings to ensure it is running the Network card in “Bridged” mode. What this does is allow your virtual machine to appear on your network as if it was another computer, rather that the default NAT mode that hides the virtual machine behind the hosts IP address.

Next start up your virtual machine and whilst it’s loading, make a note of the hosts IP address. You can find your IP address in the network preferences or through the command ipconfig (Windows) or ifconfig (Linux and Mac).

With the virtual machine now loaded go to Start > Run and type notepad "c:\windows\system32\drivers\etc\hosts" This will load up the file Windows uses to alias IP addresses to host names.

The default Windows hosts file

The default Windows hosts file

The file is very well explained but in a nutshell, you put the IP address of the host at the beginning of the line, and the domain name that should point to it. So, lets assume my host IP is 10.0.1.97 and the domain I am developing is www.themattharris.com. In the hosts file I would enter a new line as 10.0.1.97 www.themattharris.com
Now lets assume i’m also developing another domain on the host called www.tokofu.com. All I need to do is add another line to the hosts fileĀ 10.0.1.97 www.tokofu.com

Windows hosts file with local domain alias entries

Windows hosts file with local domain alias entries

Save the file and test in your virtual machine. The domains you entered into the Windows hosts file will have caused your web browser to request the site from your host and not the Internet.

We can also validate this by running two commands in the Windows Command Prompt. If we run nslookup the computer will lookup the domain name using a DNS server. If we run the command ping Windows instead uses the address it knows from the hosts file. You can see this by loading a command prompt (Start > Run and type cmd ) and entering nslookup your-domain.com and ping your-domain.com

running nslookup and ping shows different address for the domain

running nslookup and ping shows different address for the domain

To restore the default behaviour you edit the “c:\windows\system32\drivers\etc\hosts” file and either delete the lines you entered, or start the line with a # (this marks the line as a comment).

Posted in Web Development. You can leave a response, or trackback from your own site.

Response Form