14

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
0

3 Answers 3

14

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.

7
  • I tried both Bridged Adapter (3 options) and Host-only Adapter (1 option). Didn't work. I'm not sure what you mean by hostname:8888. On the VM I can get to the webapp via localhost:8888 and 127.0.0.1:888. I am not able to get to the app within the VM at 10.0.2.15:8888 or 10.0.2.255:8888. I am not able to get to it at all from the host. I've added more info about my config to the original post. If there is anything else I should post that can clear things up, let me know. Thank you for your help!
    – Kirkland
    Commented Sep 2, 2016 at 18:12
  • Pictures are worth a thousand words. The hostname:8888 I referred to is the name you gave the the machine inside the Ubuntu OS. In your case "GC". Here is the rub. That name is bound to the loopback network on 127.0.1.1 and will never be seen by anything outside the VM no matter what. It does not even see/use the 10.0.2.15 address based on your /etc/hosts information. Try adding this line to /etc/hosts: 10.0.2.15 gc and see if you can access the app with 10.0.2.15:8888 and gc:8888. If you can, we are making progress. Commented Sep 3, 2016 at 18:08
  • Considering your ultimate goal is to reach the GC VM from other hosts on your network, NAT and Host-only for your VM adapter are not the correct answer, unless you like setting up port forwarding and special routes on the host OS. You really need to work with Bridged if you want to get where you plan to go. Commented Sep 3, 2016 at 18:17
  • I added 10.0.2.15 GC below 127.0.1.1 GC in my guest's hosts file. Within the guest I can see the app via localhost:8888, but 10.0.2.15:8888 gives me... ERR_CONNECTION_REFUSED.
    – Kirkland
    Commented Sep 6, 2016 at 20:10
  • 1
    This confirms my suspicion that even if you did have the correct network routing to the VM, you will not be able to use the "app" until you solve the problem of the address it is listening to. Your app is bound to the loopback network (127.x.x.x) and does not respond to the address assigned to your eth0 device. This needs to be corrected before we can take the next step of connecting from the host. Commented Sep 6, 2016 at 21:37
7

Here is a quick solution using port forwarding if you don't want VM to be exposed to external network. Just create these port forwarding profile for web and ssh as shown below in NAT settings of VM

enter image description here

1
  • Thanks, it works without any additional setting and it's secure Commented Aug 5, 2022 at 9:25
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).

12
  • What do you mean by "a static route on your host"?
    – Kirkland
    Commented Sep 1, 2016 at 22:46
  • Your host has a default gateway, which is where all traffic gets routed through if it isn't going to another host on the same subnet. But outgoing traffic cannot get to your VM guest though the default gateway, so you have to tell it how to get there. Commented Sep 1, 2016 at 23:54
  • I updated my original answer to have instructions for creating a static route. Commented Sep 2, 2016 at 0:07
  • Yes I agree that it's overkill. It would be far better to simply use a bridged connection. That makes the host and the guest equal peers as far as networking is concerned. Commented Sep 2, 2016 at 0:49
  • I tried both Bridged (3 options) and Host-only (1 option) without luck. It's working on another VM and that one uses NAT. I can't figure out whats different. I set the other one up a long time ago and I can't remember what I did to make it work. When I toggle back and forth between them in VirtualBox with all the details showing, literally the only things that change are the name of the VM and the SATA Port 0 vdi.
    – Kirkland
    Commented Sep 2, 2016 at 18:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .