How to access server running in VirtualBox from the host?

I am running a web app on the VM.

I can see the app at localhost:8888 via a browser from within the VM.

I cannot see the app from the host at the same URL. That is the goal.

I tried VBoxManage modifyvm "VM name" --natdnshostresolver1 on but it did not have an impact.

My configuration is:

  • Host: Windows 7
  • Guest: Ubuntu 16.04.1 LTS
  • VirtualBox: 5.1.4r110228

Guest Configuration

(Guest) ifconfig when on NAT

kirkland@GC:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:d0:fc:0e  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::4c67:70b5:c37b:8fa8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7431 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3587 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:7444830 (7.4 MB)  TX bytes:731870 (731.8 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:1566 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1566 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:655936 (655.9 KB)  TX bytes:655936 (655.9 KB)

(Guest) /etc/hosts

127.0.0.1   localhost
127.0.1.1   GC

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Solution 1:

The localhost:8888 URL is only a valid URL inside your VM. Outside that VM you will have to use ip.ad.dr.ess:8888 or hostname:8888. In the latter case, a dependency on hostname resolving to ip.ad.dr.ess on your network is introduced.

By default, when you create a VM in VirtualBox, network adapter 1 is set to NAT. This will give the VM an IP address of 10.0.2.15/24 and a default route of 10.0.2.2 (which should be your host). Long story short, windows will not route to an address it is providing NAT for, without jumping through more hoops first.

Shutting down the VM and changing the VM network adapter1 setting to "bridged" will allow your host (and any host on your local network) to communicate with that VM without any special routes added or hoops to jump through.

Restart the VM. Verify your connectivity to localhost:8888. Also verify your connectivity to hostname:8888 (assuming hostname was not added to the loopback/localhost address in the /etc/hosts file) and ip-address:8888. EDIT: Based on the /etc/hosts details posted, you are binding your VM and therefore the webapp to the loopback network. This configuration is not usable outside the VM at all.

If your web server and application are not dependent on the IP it was first configured/installed on, and you have a DHCP server on your LAN, the only other piece of information you should need to connect to the VM is the IP address that was assigned to it via DHCP.

To find the IP address of the VM: Open a terminal/console window from the Ubuntu desktop (ctrl-alt-T), we use hostname, ifconfig, and grep /etc/hosts commands to put the pieces together and find your address. From the open terminal, type hostname then hit enter. This is probably gc. In that same terminal, typing ifconfig eth0 | awk '/Bcast/{print $2}' will show the address. You should have a line in /etc/hosts with the address followed by the hostname. grep [hostname] /etc/hosts or grep [address] /etc/hosts should find the line you need. If you come up with anything other than your current address followed by the hostname, you need to modify the /etc/hosts file to add the correct information.

Hosts on your LAN should now be able to connect to the IP of your VM on port 8888. For those hosts/clients to access the webapp via hostname:8888 will depend on client hosts files, local DNS, or even mDNS services possibly provided on your DHCP server.

Solution 2:

To accomplish this I think you'd be far better off using bridged mode. If that isn't possible or desirable, you'll need a static route on your host.

To add a static route, do this: Look in the network properties of your host. You should find an adapter called VirtualBox Host-Only Network. Find its IP address. It's probably 192.168.56.1, which I think is the default, but could be something else. Call that a.b.c.d. Next, get the IP address of your guest. Call that w.x.y.z. (The first 3 octets should be the same in both addresses.) When you have that, open a DOS prompt on your host, and type this command:

route add w.x.y.z mask 255.255.255.0 a.b.c.d

If that works, you can run the same command with the -p switch to make it permanent (otherwise it goes away next time you reboot the host).