Addressing localhost from a VirtualBox virtual machine [closed]
Asked Answered
S

22

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?

Suzetta answered 11/8, 2009 at 17:57 Comment(2)
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.Cockhorse
See also the same question on superuser.com: superuser.com/questions/310697/…Fluff
S
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/
Suzetta answered 11/8, 2009 at 18:27 Comment(16)
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.Milford
In order to be able to save your edits to the hosts file, launch Notepad by right clicking it and choosing "Run As Administrator"Twayblade
I had to switch my VirtualBox Network Attached to setting from Bridged Adapter to NAT for this to work for me.Blaisdell
you can also set it as 10.0.2.2 localhost in the VM config to ensure absolute paths are resolved properlyTgroup
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" :)Lunitidal
To enable this on OSX: go to VirtualBox Preferences -> Network -> Host-only Networks -> click the "+" icon.Adriene
Remember to include the port you would on your own physical machine.Sutton
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.Wolgast
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.Choragus
@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!Superdreadnought
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/hostsSuperdreadnought
I need to access on localhost not 10.0.2.2 due to some shitty auth that wants localhost as the address :(Stewardson
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 longEldreda
10.0.2.2 Is this a IP address of Gateway in VM ?Mercenary
yeah it works. I had to use the shared card option before. Thanks.Caroche
This solution still work with latest Windows 10? developer.microsoft.com/en-us/microsoft-edge/platform/issues/…Interrogation
G
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.

Godin answered 23/10, 2012 at 9:8 Comment(5)
I actually had to add 'http://' in front of the IP address for it to work (IE8 / WinXP box).Kozloski
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:8888Pless
@MichaelGiovanniPumo to my best knowledge you cannot map the url directly to a specific port. you have to state the port explicitlyGodin
@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.Pless
Marvelous is working, macOS Sierra version 10.12.5 and virtualbox v 5.1.22Lissome
A
87

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
Adriene answered 28/3, 2014 at 21:41 Comment(7)
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:3000Ardy
Works both on xp and windows 7Babble
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?Haydon
Worked on host machine MacOs High Serra and guest Windows 7Song
I was unable to find the "Host-only Networks" option.Rosinarosinante
For anyone else that couldn't find the "Host-only Networks" option, hit File > Host Network Manager... virtualbox.org/manual/ch06.html#network_hostonlyPlatform
I followed TomSykes 's instructions and then Robert 's and it worked for me. I did a summary here: gist.github.com/n8jadams/ab8d63eeb4f9b82e332d6bd05052de0aProbability
J
62

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

Jetport answered 27/9, 2019 at 10:24 Comment(6)
I used NETSH approach. worked like a charm. This is IMO the best answer.Moro
I used NETSH as well, worked like magic. This is my best answer as well. It was the only one that workedDhole
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.Stavros
This should be the accepted answer, as is relevant and has the currently working onePremillenarian
Works on Windows 10. You can even omit the listenaddress and it will still work. You can specify protocol=tcp if you want.Angus
netsh is best approach there. Thank you a lot!Inflorescence
M
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.

Mother answered 11/8, 2009 at 18:8 Comment(3)
hmm, for some reason the gui (on my machine) doesn't have this option!Suzetta
what version are you using? I found that on Mac, some of the base manual docs are completely different.Milford
Love this solution. Note that on some older Windows you won't be able to use whatever.local unless you install Bonjour.Athal
M
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

Mediocrity answered 25/8, 2016 at 22:29 Comment(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.Fluff
D
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.

Drastic answered 27/6, 2013 at 18:15 Comment(3)
@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.Goldschmidt
Because on being on a BT Home Hub, this was the only option that work for meAdmetus
Very easy solution! thanksTerret
G
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>.

Grison answered 7/8, 2014 at 14:25 Comment(3)
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!Loya
I just installed Window 10 on virtual box and this did the trick!Hellkite
This worked for me as well in combination with spsaucier's instructions further up. Followed his instructions, then I enabled Promiscuous Mode - Enable AllRuzich
P
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.

Pike answered 30/3, 2017 at 14:45 Comment(6)
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/…Adenine
@Adenine 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 LocalhostSuspiration
@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/…Adenine
@Adenine 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.Suspiration
@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 weblogs.asp.net/owscott/… 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.Adenine
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).Champignon
B
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

Brophy answered 14/8, 2018 at 22:0 Comment(2)
When a local development server has development dependencies linked to localhost. (e.g. webpack-dev-server)Divinize
what is 10.2.2.3 or .4 ? I can ping .2 as well as those - VM is Windows Server running under Windows ServerJuneberry
A
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.

Acus answered 17/4, 2013 at 17:7 Comment(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.Giffie
S
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?

Shiri answered 30/3, 2017 at 11:19 Comment(3)
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.Tarpley
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.Tarpley
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!Entryway
G
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

Giamo answered 28/4, 2015 at 21:22 Comment(0)
A
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.

Algebra answered 15/3, 2016 at 18:9 Comment(0)
P
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>

Polarimeter answered 12/7, 2013 at 19:17 Comment(0)
B
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.

Bayne answered 9/5, 2016 at 11:42 Comment(0)
M
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.
Milch answered 13/9, 2018 at 21:39 Comment(0)
D
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").

Dehorn answered 24/5, 2016 at 14:57 Comment(0)
C
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

Coze answered 30/11, 2016 at 18:36 Comment(0)
R
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/

Rosinarosinante answered 15/10, 2018 at 15:29 Comment(1)
Actually, this seems to break localhost urls from a mac browser.Rosinarosinante
S
-2

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

Swayder answered 9/10, 2013 at 14:10 Comment(0)
M
-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.

Miticide answered 22/2, 2011 at 15:42 Comment(3)
can you give any advice on how to set up / find out the IPs on a OSX machine?Scriven
? 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.Miticide
It's not apparent what answer this refers to (a link would've been wise), and it seems more like a comment.Corliss

© 2022 - 2024 — McMap. All rights reserved.