Until now I used VirtualBox for two things – running Windows apps that didn’t run on macOS and checking production issues that happened only in specific environments – but everytime I was accessing an existing, Internet-facing domain.
Yesterday I had to fix one of such occurs-only-in-this-browser issues – so I ran VirtualBox, typed the address to my app into some legacy IE –
http://localhost:9292 – and… it didn’t work. I got “The page cannot be displayed” error.
That was pretty obvious, the
localhost I put in the URL was not the host machine’s localhost that I meant. So, how to reach ports of the host machine from within the guest system?
In the VirtualBox image’s network settings I found a thing called “port forwarding”, but that was futile - it is a setting that appears if your network is configured as NAT. While it’s not obvious at first sight, it’s about forwarding ports in the other direction, from the virtual guest machine to your host computer - so a setup like the below won’t help.
The solution for accessing the host system from the guest is to add another network interface adapter, called: Host-only adapter. To perform that operation you need to first shut down the guest system. Adding it as an extra adapter next to the existing NAT one will result in a setup where you can access the Internet and the host as if your virtualized environment is your regular host.
But how to access the host now? Obviously the
localhost name won’t work. You need to get the new network interface’s default gateway IP address. If you are on Windows then run the shell console and type
ipconfig /all to find out the host-only interface’s gateway IP address. In my case it was
10.0.2.2 and it seems to be the same for two adapters I setup in VirtualBox.
Finally – to reach my application in virtualized Windows I have to use
http://10.0.2.2:9292. (Curious why this particular IP address? Check the docs.)
That’s all and now you can access anything from your virtualized environments.
As our insightful reader suggested in a comment, it’s not the Host-only adapter that gives the IP of host in
ipconfig output but the NAT setting itself. So with default setup of the VirtualBox machine (when NAT is your network adapter) you can reach your host machine by host of the NAT adapter interface. Which in most situations is indeed the
10.0.2.2 IP address.