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:36

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楼-- · 2020-01-23 10:36

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.

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

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

查看更多
放荡不羁爱自由
5楼-- · 2020-01-23 10:39

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

查看更多
smile是对你的礼貌
6楼-- · 2020-01-23 10:39

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?

查看更多
爷的心禁止访问
7楼-- · 2020-01-23 10:40

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

查看更多
登录 后发表回答