可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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/
回答2:
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.
回答3:
To enable this on OSX I had to do the following:
- Shut your virtual machine down.
- Go to
VirtualBox Preferences -> Network -> Host-only Networks ->
click the "+" icon. Click OK.
- 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.
- 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
回答4:
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.
回答5:
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
回答6:
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>
.
回答7:
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.
回答8:
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.
回答9:
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.
回答10:
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
回答11:
Not being able to re-direct requests to localhost in the VM to the host's localhost is now baked in to Windows (https://tools.ietf.org/html/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.)
2) Setup IIS to perform Application Request Routing and then rewrite requests for localhost:port to the hostIP:port
https://docs.microsoft.com/en-us/iis/extensions/configuring-application-request-routing-arr/creating-a-forward-proxy-using-application-request-routing
回答12:
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?
回答13:
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
回答14:
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.
回答15:
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>
回答16:
In Virtual Box
- Set your network to Bridge networking
- 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.
回答17:
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.
回答18:
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").
回答19:
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
回答20:
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/
回答21:
Get the IP for the machine running Apache (inet addr: in ifconfig
) and enter that in the virtual box browser address bar.
回答22:
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.