854

I have a local test/development server (HTTP, of course), listening to port 8000.

I'm working on Linux, so to test the page on Internet Explorer 6, 7, 8, etc. I run a virtual machine using VirtualBox; I also need to see how it look on Firefox in a windows environment (fonts for instance are different).

In my real machine, I open the website simply using the URL http://localhost:8000, how do I address this localhost from the virtual machine?

Right now my workaround is to use the IP address. Any better ideas?

2
  • 55
    I can't believe this is still so tedious. I have been using VB for years and although it has gotten a hair easier, it's still the main hurdle, and the networking options are still hard to understand, and it still does not work out of the box for what the vast majority of users want to use it for. Probably hundreds of thousands use VB to test-run sites (or apps) on for different platforms on localhost. It is mind boggling to think how much time and annoyance can be spared by improving this particular aspect of the software.
    – Rolf
    Commented Nov 14, 2016 at 12:08
  • 1
    See also the same question on superuser.com: superuser.com/questions/310697/… Commented Apr 22, 2019 at 17:35

22 Answers 22

1000

Googling turned this up: http://data.agaric.com/localhost-from-virtualbox-xp-install-ubuntu

It suggests using IP: http://10.0.2.2, and it worked for me.

So, I edited the hosts file, C:\windows\system32\drivers\etc\hosts, and added this entry:

10.0.2.2   outer

If you're testing on IE8, remember to put http:// in the address bar. Just putting the ip directly will not work.

For example:

http://10.0.2.2:3000/
16
  • 7
    I did notice in ipconfig (Windows in VirtualBox) that the default router for my VirtualBox is that IP address... so that makes sense to me.
    – benc
    Commented Aug 12, 2009 at 16:00
  • 50
    In order to be able to save your edits to the hosts file, launch Notepad by right clicking it and choosing "Run As Administrator" Commented Jun 13, 2012 at 21:53
  • 26
    I had to switch my VirtualBox Network Attached to setting from Bridged Adapter to NAT for this to work for me. Commented Feb 4, 2013 at 15:27
  • 2
    The IE11/Win10 VM I downloaded from developer.microsoft.com/en-us/microsoft-edge/tools/vms seems already to be configured to work at 10.0.2.2. Maybe that's true for the other available VMs, too. I suggest checking this, before mucking around with whatever new security headaches get in the way of editing pseudo-/etc/hosts Commented Aug 15, 2016 at 22:54
  • 5
    I need to access on localhost not 10.0.2.2 due to some shitty auth that wants localhost as the address :(
    – nanobar
    Commented Jan 23, 2017 at 9:32
430

macOS

I'm running Virtual Box on macOS (previously OS X), using Virtual Box to test IE on Windows, etc.

Go to IE in Virtual Box and access localhost via http://10.0.2.2 for localhost, or http://10.0.2.2:3000 for localhost:3000.

I kept Network settings as NAT, no need for bridge as suggested above in my case. There is no need to edit any config files.

5
  • 39
    I actually had to add 'http://' in front of the IP address for it to work (IE8 / WinXP box).
    – wrtsprt
    Commented Jan 14, 2014 at 8:40
  • 8
    But is there any way to map 10.0.2.2 to localhost:8888 ? I have localhost:8888 as the home url in my database. Going to 10.0.2.2 in VB loads the HTML page, but all the links break because they're pointing to localhost:8888 Commented Sep 25, 2014 at 13:41
  • @MichaelGiovanniPumo to my best knowledge you cannot map the url directly to a specific port. you have to state the port explicitly Commented Sep 25, 2014 at 16:05
  • 3
    @JonathanLin I solved it by simply mapping 10.0.2.2 to localhost on the Win7 hosts file and that worked with port :8888 anyway, so it's now working. Thanks. Commented Sep 26, 2014 at 9:07
  • Marvelous is working, macOS Sierra version 10.12.5 and virtualbox v 5.1.22
    – Tabares
    Commented Jun 9, 2017 at 19:23
88

To enable this on OSX I had to do the following:

  1. Shut your virtual machine down.
  2. Go to VirtualBox Preferences -> Network -> Host-only Networks -> click the "+" icon. Click OK.
  3. Select your box and click the "Settings" icon -> Network -> Adapter 2 -> On the "Attached to:" dropdown, select "Host-only Adapter" and your network (vboxnet0) should show up below by default. Click OK.
  4. Once you start your box up again, you should be able to access localhost at http://10.0.2.2/

You can refer to it by localhost and access other localhosted sites by adding their references to the hosts file (C:\windows\system32\drivers\etc\hosts) like the following:

10.0.2.2    localhost
10.0.2.2    subdomain.localhost
7
  • 5
    This solution worked for me with a little modification: After making the new Host-only Adapter (vboxnet0), edit the adapter. In the window that pops up, go to the Adapter tab and copy the IPv4 Address (e.g. 192.168.56.1). Then once you start the VM, open your browser and go to http://192.168.56.1:3000
    – Robert
    Commented Jul 21, 2016 at 1:36
  • Works both on xp and windows 7 Commented Sep 28, 2016 at 6:15
  • 1
    Unfortunately, I'm not able to get my subdomains to resolve, I went through the steps on the accepted answer here to check hosts file is working on Win10 as expected: serverfault.com/questions/452268/… Any ideas?
    – danjah
    Commented Nov 22, 2016 at 2:00
  • 10
    I was unable to find the "Host-only Networks" option. Commented Oct 15, 2018 at 15:23
  • 1
    For anyone else that couldn't find the "Host-only Networks" option, hit File > Host Network Manager... virtualbox.org/manual/ch06.html#network_hostonly
    – Tom Sykes
    Commented Jul 22, 2021 at 16:03
63

Not being able to re-direct requests to localhost in the VM to the host's localhost is now baked in to Windows (https://www.rfc-editor.org/rfc/rfc6761#section-6.3), including the VM's available at https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/

For security reassons Microsoft now prevents host file entries for overriding the address of localhost to anything other than the loopback address ::1. So adding a line the VM's host file such as

10.0.2.2 localhost

will be ignored.

There are two ways (that I know of) to override this:

  1. use NETSH to portproxy to the host

    netsh interface portproxy add v4tov4 listenaddress=127.0.0.1 listenport=8000 connectaddress=10.0.2.2 connectport=8000

(where 10.0.2.2 is the default gateway on the VM and 8000 is the port you want to resolve to on the host.)

  1. Setup IIS to perform Application Request Routing and then rewrite requests for localhost:port to the hostIP:port

https://learn.microsoft.com/en-us/iis/extensions/configuring-application-request-routing-arr/creating-a-forward-proxy-using-application-request-routing

6
  • 11
    I used NETSH approach. worked like a charm. This is IMO the best answer.
    – Liang Zhou
    Commented Oct 31, 2019 at 4:57
  • 3
    I used NETSH as well, worked like magic. This is my best answer as well. It was the only one that worked
    – Van_Paitin
    Commented Nov 27, 2019 at 16:33
  • 1
    netsh was what helped me, I tried everything I found on the net, but no success. This will allow to access <protocol>://localhost:<port>, instead of accessing directly by IP. This should be the accepted answer for Windows 10. Commented Jan 21, 2020 at 10:57
  • 2
    This should be the accepted answer, as is relevant and has the currently working one Commented Mar 18, 2020 at 3:19
  • 1
    Works on Windows 10. You can even omit the listenaddress and it will still work. You can specify protocol=tcp if you want. Commented Oct 22, 2020 at 13:27
60

You most likely have your virtual machine’s networking set to NAT. If you set your networking to Bridged you should be able to access your host machine by its hostname. For example, if your hostname is “jsmith-Precision-7510” and you want to open http://localhost:3000/, you will be able to view that page at http://jsmith-Precision-7510:3000/.

To find your hostname, open a terminal/console and then run the command hostname. Your hostname will be outputted on the next line.

See the VirtualBox documentation for instructions on how to set your networking setttings to Bridged.

3
  • hmm, for some reason the gui (on my machine) doesn't have this option!
    – hasen
    Commented Aug 11, 2009 at 18:14
  • what version are you using? I found that on Mac, some of the base manual docs are completely different.
    – benc
    Commented Aug 12, 2009 at 15:59
  • Love this solution. Note that on some older Windows you won't be able to use whatever.local unless you install Bonjour. Commented May 14, 2013 at 14:49
48

I had to go into virtualbox and change my network settings to 'NAT'. After that, I was able to hit localhost running on my host machine from my emulator on virtualbox through http://10.0.2.2:3000

1
  • 1
    Recently (2019) I downloaded Microsoft's Windows 7 IE10 guest VM (my host is MacOS Sierra) and had a lot of problems with the networking setup. So I finally just trashed it and downloaded the Microsoft Windows 7 IE11 guest VM. Then I set it to 'NAT' networking and could hit the host's localhost via http://10.0.2.2 exactly as described here by @Pytth. Commented Apr 22, 2019 at 17:31
42

If you have adapter attached to NAT, nowadays it's better and more elegant solution to set port forwarding to local ports.

Settings > Network > Adapter > Advanced > Port forwarding

Just insert new rule and set Host port and Guest port to 80 (for http) or 22 (for ssh), and so on.

Then you can access that machine by entering just http://localhost

You may also want to switch NAT to transparent mode.

3
  • 8
    @Boris What you describe in here is how to get from the host to the guest server (port). But the question was about the other way round. Commented Mar 5, 2016 at 12:42
  • Because on being on a BT Home Hub, this was the only option that work for me Commented Mar 22, 2020 at 19:06
  • Very easy solution! thanks Commented Jul 28, 2021 at 1:29
37

I found that 10.0.2.2:<port> works, but only if Promiscuous Mode is set correctly. After installing my VM, I went to Settings > Network > Adapter 1.

NAT is set by default, and the Promiscuous Mode dropdown is disabled. I switched from NAT to Bridged Adapter, which enabled the Promiscuous Mode dropdown, and then changed the value from "Deny" to "Allow VMs". I then switched back to NAT, which disabled Promiscuous Mode again, but retained the new value.

After only this change, I was able to launch my VM and see my host machines localhost:<port> on my VM at 10.0.2.2:<port>.

3
  • 2
    I'm running Windows 7 as my host machine and Windows 10 as guest, finally this solution worked for me in VirtualBox 4.3.30 r 10.16.10. Thanks!
    – Micer
    Commented Aug 19, 2015 at 8:25
  • I just installed Window 10 on virtual box and this did the trick!
    – captainill
    Commented May 3, 2016 at 23:48
  • This worked for me as well in combination with spsaucier's instructions further up. Followed his instructions, then I enabled Promiscuous Mode - Enable All
    – DyeA
    Commented Jan 18, 2017 at 18:38
26

MacOS

If you want to set up a windows environment with Virtualbox on a mac, just use the default NAT settings on the adapter, and in your windows VM, go to hosts file and add the following:

10.0.2.2       localhost
10.0.2.2       127.0.0.1

Differently from the answers above, it's important to include both lines, otherwise it won't work.

6
  • 7
    This works great with the modern.ie images for debugging ie/edge on Mac, which is how I came to this question. There is one exception: Windows 10. Windows 10 disallows modification of the localhost domain via the hosts file (madness) The workaround is to enable IIS and set up a reverse proxy through it, accomplishing the same in many more steps: developer.microsoft.com/en-us/microsoft-edge/platform/issues/… Commented Jun 28, 2017 at 16:46
  • @MaxPRafferty Were you able to get this to work? I've been through everything else on this thread to no avail. I've updated my host file as this answer suggests and followed the steps in that link. It was a little ambiguous as to where to declare 10.0.2.2 in step 7. Where does it go in the rule? I am now relentlessly direct to the IIS page on my Windows VM Localhost Commented Dec 20, 2017 at 21:23
  • @DavidWeber yes I was - I actually filed that bug, so I still have that image available. 10.0.2.2 needs to go into the "action url", listed as "rewrite url" in the edit screen: main rewrite page: user-images.githubusercontent.com/1894398/… , user-images.githubusercontent.com/1894398/… edit rule page: user-images.githubusercontent.com/1894398/… Commented Dec 21, 2017 at 20:14
  • @MaxPRafferty Thanks for the help and links. I still cannot get this to work. Such a simple thing and I am not sure where the problem is now. I've followed this to the 't' and still nothing even when attempting each of the other network type suggestions. Looks like I may as well buy a PC at this point. Commented Dec 21, 2017 at 22:11
  • 3
    I don't think this works anymore now that newer Windows versions resolver localhost with their DNS. I don't think 127.0.0.1 can be redirected like this either (only host names). Commented Jan 29, 2018 at 17:41
25

You don't need to change hosts file or any Virtual Box configuration. Keep settings in NAT. Go to your Windows instance and run "cmd" or open cmd.exe. Execute command "ipconfig" and get the Default Gateway IP Address. Browse http://10.0.2.2:8080 on Windows IE you will see is the same than your Mac Safari http://localhost:8080/ or http://127.0.0.1:8080

2
  • 1
    When a local development server has development dependencies linked to localhost. (e.g. webpack-dev-server)
    – kube
    Commented Oct 17, 2018 at 15:21
  • what is 10.2.2.3 or .4 ? I can ping .2 as well as those - VM is Windows Server running under Windows Server
    – ycomp
    Commented Feb 3, 2019 at 15:56
22

You need to edit your hosts file on your Windows Virtual machine the same way you do for your local host machine:

C:\WINDOWS\system32\drivers\etc\hosts

And link your virtual hosts to 10.0.2.2, If you are just using localhost then replace

127.0.0.1 localhost with 10.0.2.2 localhost

For example:

10.0.2.2 localhost
10.0.2.2 local.site1.com
10.0.2.2 local.site2.com

This tells your virtual machine to point to your local machine for those domain names.

1
  • Thx, this helped in my case (running local websites in docker with nginx) - accessing the IP direclty (10.0.2.2) just gives nginx error.
    – ggzone
    Commented Sep 6, 2018 at 9:14
10

A combination of a few things eventually got things working on my end. Running a flask server on macosx.

In my windows VM I edited the hosts file:

  • Run notepad as administrator
  • open C:\windows\system32\drivers\etc\hosts
  • add this entry: 10.0.2.2 outer

Shutdown VM and on my Mac in VirtualBox:

  • Go to VirtualBox > preferences > Network > Host-only Networks > + to add a network vboxnet1
  • Go to My_VM > settings > Network > Adapter 1.
  • Select Enable Network Adapter and set Attached to: to Bridged Adapter.
  • Then set Advanced > Promiscuous Mode: to Allow VMs.
  • Click OK
  • Go to My_VM > settings > Network > Adapter 1.
  • Set Attached to: back to NAT.

Then I went to Adapter 2

  • Set Attached to: to Host-only Adapter and select the previous added network vboxnet1.

I started my server on my mac, running on 127.0.0.1:5000 and this was now accessible on my vm at http://10.0.2.2:5000

Man what a nightmare to test on IE on mac. How is there not a simpler way?

3
  • 1
    I wish I could upvote this a thousand times. This was the only thing that worked after finding more simpler methods such as simply editing the hosts file and using default NAT settings - none of those posts resolved my issue. I'm running MacOS Sierra with a VM of IE11 on Win81 from developer.microsoft.com/en-us/microsoft-edge/tools/vms in case that combo helps anyone else here. Commented Feb 6, 2018 at 18:30
  • FWIW, I tried rolling back all of those settings, and then removing VirtualBox and re-installing, re-importing the VMs from Microsoft. I started my rails server with rails s -b 127.0.0.1 and accessed it from all VMs at http://10.0.2.2:3000 and it worked. I wonder if the setting change from above somehow lingered around even after rolling all of it back. Commented Feb 6, 2018 at 21:38
  • 1
    This wasn't quite the same for me, especially the steps for Adapter 2, I had to use Nat Adapter setting to get it to be vboxnet1, but it ended up working. Thanks for the clues!
    – jfunk
    Commented Feb 4, 2020 at 21:08
4

I solved by adding a port forwarding in Virtualbox settings under network. Host IP set 127.0.0.1 port : 8080 Guest ip : Give any IP for the website ( say 10.0.2.5) port : 8080 Now from guest machine access http://10.0.2.5:8080 using IE

3

In virtual Box as said upper, you can add this line hosts file

10.0.2.2   outer

but to save it, if you don't have administrators right in your VM just move hosts file to desktop, then edit it to add the line 10.0....outer, save the file, and move to its original place.

2

check if you can hit your parent machine with: ipconfig (get your ip address)

ping <ip> or telnet <ip> <port>

If you cannot get to the port, try adding a new inbound rule in your parent firewall allowing local ports.

I was then able to access http://<ip>:<port>

2

In Virtual Box

  1. Set your network to Bridge networking
  2. Go to Advanced set Promiscuous Mode: Allow All

Now the tricky bit is your localhost, if you are running from Node.js set the IP address to 0.0.0.0, then lookup your own IP address, for example cmd:ipconfig --> 10.0.1.3

Type that address in with the port number. And it will work.

1

General steps:

  • A common network, (add host-only or bridge NIC)
  • configure preferred service to listen on appropriate interface (interface connected to shared NIC)
  • Use IP:Port to reach targeted service, use an IP that belong to shared network.
0

On Windows with a virtual Windows 7 the only thing that worked for me was using NAT and port-forwarding (couldn't get bridged connection running). I found a tutorial here: http://www.howtogeek.com/122641/how-to-forward-ports-to-a-virtual-machine-and-use-it-as-a-server/ (scroll down to the part with "Forwarding Ports to a Virtual Machine").

With this changes I could reach the xampp website with "http://192.168.xx.x:8888/mywebsite" in internet explorer 10 on my virtual machine.

I found the IP in XAMPP Control Panel > Netstat ("System").

0

If you use Virtual Box you can connect Mac OSX (and I think Linux) to your virtual windows machine using one line of code --> I suggest stopping the box before running this in terminal.

VBoxManage modifyvm "YOUR VM NAME" --natdnshostresolver1 on

I will note that this is from the Dinghy docs, which I am running, but its a virtual box command so it shouldn't actually care what you use as long as its Virtual Box

0

I need to run on localhost, not some weird IP.

1) From your Mac terminal, do iconfig -a to find your local IP address. It's probably the last one.

en7: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV> ether 38:c9:86:32:0e:69 inet6 fe80::ea:393e:a54f:635%en7 prefixlen 64 secured scopeid 0xe inet 10.1.5.60 netmask 0xfffffe00 broadcast 10.1.5.255 nd6 options=201<PERFORMNUD,DAD> media: autoselect (1000baseT <full-duplex,flow-control>) status: active

e.g. 10.1.5.60

2) boot up your windows image. start > type cmd to get a terminal

3) notepad c:\windows\system32\drivers\etc\hosts

4) add the following line 10.1.5.60 localhost

5) open IE, and the following url should hit the server running on your mac: http://localhost:3000/

1
  • Actually, this seems to break localhost urls from a mac browser. Commented Oct 15, 2018 at 17:58
-2

Get the IP for the machine running Apache (inet addr: in ifconfig) and enter that in the virtual box browser address bar.

-3

Actually, user477494's answer is in principle correct.

I've applied the same logic in other environments (OS X host - virtual Windows XP) and that does the trick. I did have to cycle the host LAMP stack to get the IP address and Apache port to resolve, but once I'd figured that out, I was laughing.

3
  • can you give any advice on how to set up / find out the IPs on a OSX machine?
    – cukabeka
    Commented May 30, 2012 at 11:05
  • ? not sure which address your after - the computer's address can be found via system prefs / network -> IP Address. That is basically the address that the macine will answer to externally. Internally localhost:80 should do for osx's apache server - while MAMP will uses localhost:8888 by default.
    – orionrush
    Commented Jun 5, 2012 at 1:35
  • It's not apparent what answer this refers to (a link would've been wise), and it seems more like a comment.
    – isherwood
    Commented Mar 28, 2019 at 19:34

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