Accessing localhost in Mac OsX from Windows VM in Virtualbox

Oh the joy of cross-browser testing… The worst part is that it’s rather difficult to even just see your dynamic pages from localhost in your Virtualbox VMs… Here’s how I did it.


Since I’m not going to cover everything, here’s what you need to have to get started :

  • Virtualbox installed
  • Some Windows virtual machines installed (I have XP and Seven)
  • A website on local, Mamp in my case

Having your Windows installations access your localhost

First part is to give access to your host’s (Mac) localhost to your guests (Windows). This is rather easy, open up Virtualbox, go in Virtualbox>Preferences>Network and click on the little icon to add a new one. You don’t need to configure anything in here. Here’s what it’s supposed to look like

Now let’s give each of our VMs access to this new local network we created, in the list of VMs you have, select one, click on “Settings” at the top of the list and go in Network>Adapter 2. If you already have a network adapter in here, go to the 3rd one instead, but if you have done anything to your network settings, it should be empty.

Activate this 2nd adapter and set it with Host-only Adapter and the only Name attribute in the list we just created earlier.

IP stuff you’ll need to know

While you’re still enjoying the beauty of Mac Os, fire up your and run ifconfig vboxnet0(change vboxnet0 to the actual value of your local name).

Some stuff will appear in there, including inet, you’ll need this IP to setup your VMs so either keep this Terminal opened or note the IP somewhere.

Windows config

If you’re on Windows XP then it’s rather simple, start by opening your Notepad app, then in Windows’ ugly Finder-like thing, browse to C:\WINDOWS\system32\drivers\etc and drag the hosts file in your Notepad window. Here you see the current configuration.

Add something like that to the end of your hosts file, using the IP I told you to carefully note earlier and the url you’d like to access your localhost from your VM.

Voilà !

Now if you open your browser and go to the url you’ve just setup in your VM in some crappy old browser you want to test stuff on (cmon who runs IE on a VM for fun ?) and it should appear… Unless

Issues I’ve encountered

Along the path, I’ve had to face a few issues and spent countless time googling, here’s how I solved them :

Your local website isn’t exactly on localhost

If you’re using MAMP or something similar, you may have setup your local website to be on some.url.local or any kind of fancy url you like. This may not work depending on how you’ve setup your fake url.

In your httpd.conf you can write it in 2 ways : <VirtualHost some.local.url:80> which is going to fail, or <VirtualHost *:80> which will work in your VM.

If you’re using something that looks like the first piece of code, go ahead and change it to the second one. Now it should work on your VM (once you’ve rebooted your Apache server).

This is going to be an issue if you need to have access to 2 different local urls in your VMs. I don’t need this so it’s not an issue for me and I didn’t research how to fix that, you’ll have to do some more googling for that.

Saving the hosts on Windows 7 (and Vista as well I’ve heard)

Due to permissions issues, you may not be allowed to save your hosts file on some versions of Windows… This is rather annoying and it took me a while to figure out, but basically what you want to do is right click on the file, edit the permissions for all users then edit and save the file.

It gets faster after the 2nd VM

The process is tedious and takes a moment but once you’ve done it a couple times it gets really easier to do. I can’t believe it’s so difficult to make something that simple work but I guess it’s the price one has to pay to have websites that don’t break in IE7 or commit logs full of garbage.

Also if you think that you can just save static generated pages share them using a shared folder and test that, know that IE7 and 8 will actually show the file as an intranet file with different parameters, making this solution rather unreliable, avoid that whenever possible. Plus it’s much easier to actually edit the files rather than copies of the files to copy the changes back when you’re done !

Have nice testing folks !

4 thoughts on “Accessing localhost in Mac OsX from Windows VM in Virtualbox

  1. Thanks for the guide, really useful. I think it’s worth me saying that I could not activate the 2nd Network Adapter as the tab is disabled.
    And when I add vboxnet0 there is no IP address in the ifconfig vboxnet0.

    My solution is now as follows:

    I simply took my IP address down from en0 in ifconfig and added this to my hosts file in Windows and it works. No other changes in VirtualBox are necessary.


    1. Ok I couldn’t activate the 2nd adapter because my machine was paused. It must be stopped completely for this to be available. Regardless, my simpler solution worked for me.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>