I have a windows 7 host handling a VirtualBox debian guest with nat. This guest is running some web services reachable from the outside through port redirection.

I need to provide the external access to these services through urls, but I can't rely on a proper dns resolution, so I have to propagate the host ip to the guest environment. How could I do that with the minimal impact on the host?

Some clarifications: "the outside", "external access" refers to web browsers hosted at the same level as the host, ie. they can't only rely on the host address.

A drawing is worth a thousand words:

+--------------+  External                         
| Windows Host |  network   +---------------------+
| +----+       |            |      Web client     |
| |    |      NAT           |  +----------------+ |
| |    ------------------------> Page generated | |
| |    |       |            |  |by VM with URLs | |
| | VM |       |            |  |to host         | |
| |    |       |            |  +--------|-------+ |
| |    |      FWD           |           |         |
| |    <--------------------------------+         |
| |    |       |            +---------------------+
| +----+       |                                   
  • I added some clarifications
    – Dfaure
    Jun 28 '17 at 8:52

One way to achieve this is the trick behind sites like whatsmyip.com. all you need is a web server in your network that sends the IP-address from where it receives a request back to the client.

Remember, since you are using NAT the server sees the address of the NAT-router (in this case the VM-host) as originating address.

To accomplish this you could create a php-script (i.e.whatsmyip.php) in the web-root of your web server, like

//Gets the IP address
$ip = getenv("REMOTE_ADDR") ; 
Echo "$ip; 

and access it from your guest like http://webserver/whatsmyip.php

(source: Thoughtco.com )

Now you only need to evaluate that answer in your service


Taking account guntbert's answer, this involves:

  • Use a third party web server in the "external" network to host a remote address responder as described in the given answer.
  • During the VM initialization phase (and/or periodically according to the network context), browse it to fetch the external address of the windows host
  • Use the retrieved address to forge URLs while building pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.