851

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hasen
  • 161,647
  • 65
  • 194
  • 231
  • 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 Nov 14 '16 at 12:08
  • 1
    See also the same question on superuser.com: https://superuser.com/questions/310697/connect-to-the-host-machine-from-a-virtualbox-guest-os – Purplejacket Apr 22 '19 at 17:35

22 Answers22

996

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/
hasen
  • 161,647
  • 65
  • 194
  • 231
  • 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 Aug 12 '09 at 16:00
  • 49
    In order to be able to save your edits to the hosts file, launch Notepad by right clicking it and choosing "Run As Administrator" – Noah Sussman Jun 13 '12 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. – Troy Harvey Feb 04 '13 at 15:27
  • 1
    you can also set it as 10.0.2.2 localhost in the VM config to ensure absolute paths are resolved properly – Mario Peshev Apr 03 '13 at 07:35
  • 1
    Run this command in an adminstrator powershell console: `"\`n$((Get-NetIPConfiguration).Ipv4DefaultGateway.NextHop) outer" | Add-Content "C:\Windows\System32\drivers\etc\hosts"` to automate this, adding the host OS as "outer" :) – WickyNilliams Dec 08 '13 at 17:28
  • To enable this on OSX: go to VirtualBox Preferences -> Network -> Host-only Networks -> click the "+" icon. – Stephen Saucier Mar 28 '14 at 21:29
  • Remember to include the port you would on your own physical machine. – dubilla May 23 '14 at 18:44
  • This doesn't help at all if for example you're using localhost/wordpress as a wordpress test setup. Everything goes to localhost which is (still) the virtual-windows. Yes I know you could do a bunch of db changes to fix it but that can be a pain. – NoBugs Oct 24 '14 at 02:45
  • I'm able to go into System Preferences > Network, grab the IP address listed there and visit 'http://[IP Address From Network Settings]:[Port]/'. This is the issue generally in having other computers load 'localhost' within a given network. – Ryan Walton Mar 25 '16 at 18:56
  • 1
    @user1696255: Sounds to me more like Windows and IE being silly than you. It's not much to expect Windows' networking stack to monitor the timestamp on /etc/hosts—every other OS seems to manage—and IE shouldn't even have to know about that! – Michael Scheper Aug 15 '16 at 22:50
  • 2
    The IE11/Win10 VM I downloaded from https://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 – Michael Scheper Aug 15 '16 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 :( – Dominic Jan 23 '17 at 09:32
  • And if you use docker-machine on a Mac, and you need to access this IP address, it's just the same adresse (e.g. 192.168.99.100:3000 works also inside VirtualBox). Took me way too long – 23tux Mar 09 '17 at 08:46
  • 1
    10.0.2.2 Is this a IP address of Gateway in VM ? – Pratik Patil May 08 '18 at 18:18
  • yeah it works. I had to use the shared card option before. Thanks. – Sampgun Dec 03 '18 at 09:43
  • This solution still work with latest Windows 10? https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12464785/ – smartmouse Feb 26 '19 at 11:04
429

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.

Jonathan Lin
  • 19,922
  • 7
  • 69
  • 65
  • 39
    I actually had to add 'http://' in front of the IP address for it to work (IE8 / WinXP box). – wrtsprt Jan 14 '14 at 08: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 – Michael Giovanni Pumo Sep 25 '14 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 – Jonathan Lin Sep 25 '14 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. – Michael Giovanni Pumo Sep 26 '14 at 09:07
  • Marvelous is working, macOS Sierra version 10.12.5 and virtualbox v 5.1.22 – Tabares Jun 09 '17 at 19:23
86

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
Stephen Saucier
  • 1,925
  • 17
  • 20
  • 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 Jul 21 '16 at 01:36
  • Works both on xp and windows 7 – Sukeerthi Adiga Sep 28 '16 at 06: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: http://serverfault.com/questions/452268/hosts-file-ignored-how-to-troubleshoot Any ideas? – danjah Nov 22 '16 at 02:00
  • Worked on host machine MacOs High Serra and guest Windows 7 – Kapitula Alexey Jan 08 '18 at 08:07
  • 10
    I was unable to find the "Host-only Networks" option. – Jason Moore Oct 15 '18 at 15:23
  • 1
    For anyone else that couldn't find the "Host-only Networks" option, hit File > Host Network Manager... https://www.virtualbox.org/manual/ch06.html#network_hostonly – Tom Sykes Jul 22 '21 at 16:03
  • I followed TomSykes 's instructions and then Robert 's and it worked for me. I did a summary here: https://gist.github.com/n8jadams/ab8d63eeb4f9b82e332d6bd05052de0a – n8jadams Oct 30 '21 at 12:59
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.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
Nick Haddad
  • 8,767
  • 3
  • 34
  • 38
59

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

Community
  • 1
  • 1
ActualAl
  • 1,192
  • 10
  • 13
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

Pytth
  • 4,008
  • 24
  • 29
  • 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. – Purplejacket Apr 22 '19 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.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Boris Brdarić
  • 4,674
  • 2
  • 24
  • 19
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>.

bluehazetech
  • 1,161
  • 10
  • 10
  • 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 Aug 19 '15 at 08:25
  • I just installed Window 10 on virtual box and this did the trick! – captainill May 03 '16 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 Jan 18 '17 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.

Artipixel
  • 1,246
  • 14
  • 17
  • 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: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12464785/ – MaxPRafferty Jun 28 '17 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 – David Weber Dec 20 '17 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: https://user-images.githubusercontent.com/1894398/34273137-1e8056e8-e661-11e7-847d-9afdbc0b4350.png , https://user-images.githubusercontent.com/1894398/34273136-1ccfc798-e661-11e7-9649-1ea5dc321da4.png edit rule page: https://user-images.githubusercontent.com/1894398/34273207-67ff9a90-e661-11e7-83ba-027785df1d6d.png – MaxPRafferty Dec 21 '17 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. – David Weber Dec 21 '17 at 22:11
  • @DavidWeber Its a very irritating problem for no good reason. Tsk tsk Microsoft. If I've missed a step somewhere, you can retrace my steps by following https://weblogs.asp.net/owscott/creating-a-reverse-proxy-with-url-rewrite-for-iis for setting up that reverse proxy. Can you at least see your host systems' localhost on 10.0.2.2 when you view it directly in the Win 10 edge browser? If not, your problem lies in your virtualbox config, and not your windows settings, and none of these solutions will work. – MaxPRafferty Dec 21 '17 at 23:20
  • 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). – kossmoboleat Jan 29 '18 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

marlonjuc
  • 351
  • 3
  • 4
  • 1
    When a local development server has development dependencies linked to `localhost`. (e.g. webpack-dev-server) – kube Oct 17 '18 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 Feb 03 '19 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.

William
  • 337
  • 2
  • 5
  • 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 Sep 06 '18 at 09: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?

Craicerjack
  • 6,203
  • 2
  • 31
  • 39
  • 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 https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/ in case that combo helps anyone else here. – Manu Kanthan Feb 06 '18 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. – Manu Kanthan Feb 06 '18 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 Feb 04 '20 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

Praveen
  • 41
  • 2
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.

Nico
  • 3,430
  • 4
  • 20
  • 27
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>

Jon
  • 632
  • 5
  • 13
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.

Joel Chu
  • 828
  • 1
  • 9
  • 25
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.
Anna
  • 23
  • 4
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").

herrfischer
  • 1,768
  • 2
  • 22
  • 35
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

Lux.Capacitor
  • 336
  • 3
  • 12
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/

Jason Moore
  • 7,169
  • 1
  • 44
  • 45
-2

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

H.Rabiee
  • 4,747
  • 3
  • 23
  • 35
-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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
orionrush
  • 566
  • 6
  • 30
  • can you give any advice on how to set up / find out the IPs on a OSX machine? – cukabeka May 30 '12 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 Jun 05 '12 at 01:35
  • It's not apparent what answer this refers to (a link would've been wise), and it seems more like a comment. – isherwood Mar 28 '19 at 19:34