134

On my Ubuntu host I have my local webserver. I installed VirtualBox and Debian as a guest.

I would like the Debian guest to be able to hit my webserver running on my Ubuntu host (for example, I just type http://localhost:8080/ in the browser under Debian).

How can this be done?

Cfinley
  • 1,435
  • 3
  • 14
  • 20
Maxim
  • 2,343
  • 2
  • 16
  • 7

7 Answers7

217

Enable ONE network adapter. Set it to NAT and start your machine. Don't need to configure anything.

On guest machine's browser type http://10.0.2.2 which will show you the same content as "localhost" in the host OS.

10.0.2.2 is the "default gateway" for NAT connections and your guest OS' IP is 10.0.2.15

Of course you can set a "HOST-ONLY" or an "Internal Network" which will allow you another advanced configurations. I recommend to stay with NAT.

Enjoy

M.

Melvyn
  • 2,697
  • 1
  • 16
  • 8
  • 1
    "Enable ONE network adapter". Where is this done? – jshen May 08 '14 at 16:39
  • 1
    Note: If you have name-based virtual hosts configured in apache on your host machine, then you can access them by simply creating a mapping in the guest's machines hosts file for `10.0.2.2 yourvirtualhostname.local (or whatever)` – User Aug 22 '14 at 01:12
  • @User are you sure that your last comment will work? I have a virtual host on my VB host set to `http://example.dev`, and mapping `10.0.2.2` to anything on my VB guest doesn't help access it at all. – Томица Кораћ Jan 06 '15 at 11:36
  • 1
    @ТомицаКораћ it does work. For example see: http://askubuntu.com/questions/36183/how-to-access-apache2-virtual-host-from-virtualbox-or-any-vm however, you probably should open a separate question to get this solved. – User Jan 06 '15 at 17:11
  • @User you're right. I think I had to restart my guest machine, but now it's working. Thanks. – Томица Кораћ Jan 06 '15 at 19:21
  • Doing that, I get an 401 http code when fetching the page. I have disabled the host firewall, but no luck... I only get to see my favicon on the browser's tab... – user2173353 Oct 14 '15 at 12:14
  • 1
    "Enable ONE network adapter" means under the network settings of your virtual machine (within VirtualBox) enable only one of the adapters. Got me a bit confused when I first read it. – Marquez Dec 16 '15 at 19:37
  • Does this only work for web servers? I can get this working with one and see a hello world page from the host, but when I write my own little Python server listening on 21333 instead of 8080 the connection always times out. – Joseph Garvin Dec 08 '18 at 21:32
  • Note: You can change the guest's network settings while it is running via Machine > Settings > Network (at least in version 6.0). – trebormf Feb 13 '19 at 16:06
  • Thanks for sharing this information. It really saved my day and lot of reading around networking when using virtualbox. – Jitendra Nov 14 '19 at 08:36
6

Also a point to make when using the ip address of 10.0.2.2 is if you're using MAMP for instance add the port number :8888 so the full address reads http://10.0.2.2:8888

soandos
  • 24,206
  • 28
  • 102
  • 134
Paul
  • 61
  • 1
  • 1
4

The easiest way of allow uninterrupted access to your VirtualBox Guest machine is to set the network device as a 'bridge' to your eth0 on to your LAN.

The Guest machine will grab a LAN IP via DHCP from your router.

Ash Palmer
  • 229
  • 1
  • 3
  • To clarify: This is a setting in the client app. It worked for me—it just took me a while to realise it, because IE was too stupid to assume I wanted an HTTP connection when I typed an IP address and port into the address bar. – Michael Scheper Nov 19 '15 at 06:43
2

What I found fixed the issue for me was making sure that the windows VirtualHost-Only adapter was set to the .1 address for the ip range so my virtual box host only adapter was 192.168.58.20 but the ip for the Windows adapter is 192.168.58.1

2

If solution by @Melvyn does not work, as an alternative you can set you network adapter to be bridged.
Go to Settings -> Network -> then change Attached to to Bridged Adapter.

enter image description here

You should be able to call parent by it's hostname or external IP address you can get it by going to Command prompt and typing ipconfig /all enter image description here

enter image description here

Update If you are debugging with Visual Studio and using IIS Express.

go to C:\Users\[uesername]\Documents\IISExpress\config\applicationhost.config

find your website in <configuration>/<system.applicationHost>/<sites>/<site> (name can be deceptive try checking location on the disk in physicalPath attribute)

then add into <bindings> section new binding

<bindings>
    <binding protocol="http" bindingInformation="*:50766:localhost" />
    <binding protocol="http" bindingInformation="*:50766:48qzr12" />
</bindings>

where bindingInformation is *:[port]:[machineName]

Hope this saves you some time.

Matas Vaitkevicius
  • 1,179
  • 5
  • 21
  • 35
0

Guests can connect to hosts, and hosts to guest, but not out of the box.

Here is a scenario. Suppose you have a host, and two guests (in my case, Windows host (HOST), and a Linux (GUEST1) and Windows (GUEST2) virtual image): Suppose further more, you run a website on one guest (in my case, http://localhost:8081, on the Windows GUEST2). I run both guests in NAT networking mode.

HOST -> GUEST2 : Use NAT port forwarding rule, on 8081. Go to networking of the virtual guest (GUEST2), and add the rule there. So, now on the host, http://localhost:8081 will reach the GUEST2 website.

GUEST1 (linux) -> HOST(W10) -> GUEST2(W10) website on port 8081

Go to your other GUEST1, in my case, Linx, run netstat -rn:

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

0.0.0.0         **10.0.2.2**        0.0.0.0         UG        0 0          0 enp0s3
10.0.2.0        0.0.0.0         255.255.255.0   U         0 0          0 enp0s3
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 enp0s3
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0

Now, your website can be reached on the gateway, http://10.0.2.2:8081. That will trigger the port NAT from HOST to GUEST2 again.

-1

Well you can get Debian to hit your webserver, but not using localhost. You'll have to use the IP address of the Ubuntu host.

Assuming the guest OS is connected to the network, to grab the IP open up a terminal and type ifconfig. Should look something like 192.168.0.2.

Jivings
  • 101
  • 13
  • Hi! Sorry for too late reply. ifconfig on my host shows 192.168.0.102 for eth0. When I enter this ip in firefox in guest it is trying to connect for a while and then connection is terminated: "The server at 192.168.0.102 is taking too long to respond"... So what should I try next? Thanks in advance. – Maxim May 24 '10 at 17:52