Addressing localhost from a VirtualBox virtual mac

2020-01-23 10:11发布

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?

22条回答
戒情不戒烟
2楼-- · 2020-01-23 10:41

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.

查看更多
成全新的幸福
3楼-- · 2020-01-23 10:42

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/
查看更多
姐就是有狂的资本
4楼-- · 2020-01-23 10:44

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.

查看更多
霸刀☆藐视天下
5楼-- · 2020-01-23 10:44

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

查看更多
我命由我不由天
6楼-- · 2020-01-23 10:45

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.

查看更多
放我归山
7楼-- · 2020-01-23 10:47

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.
查看更多
登录 后发表回答