How can I access a server running in VirtualBox from the host?
I am running a web application on a VM.
I can view the application localhost:8888
through the browser inside the VM .
I can't see the app from the host using the same URL. That's the goal.
I tried it and VBoxManage modifyvm "VM name" --natdnshostresolver1 on
it didn't make a difference.
My configuration is:
- Host: Windows 7
- Guest: Ubuntu 16.04.1 LTS
- VirtualBox: 5.1.4r110228
(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
The localhost:8888
URL is just a valid URL in your VM. Outside of that VM you will have to use ip.ad.dr.ess:8888
or hostname:8888
. In the latter case, a dependency on hostname resolution on the network is ip.ad.dr.ess
introduced .
By default, when creating a VM in VirtualBox, Network Adapter 1 is set to NAT. This will give the VM an IP address 10.0.2.15/24
and default route 10.0.2.2
(should be your host). Long story short, Windows won't route to the address that gives it NAT without jumping through more loops first.
Shutting down the VM, and changing the VM Network Adapter 1 setting to "Bridged", will allow your host (and any host on your local network) to communicate with that VM without adding any special routing or bypassing hoops.
Restart the virtual machine. Verify your connection to localhost:8888
. Also verify your connection hostname:8888
(assuming the hostname is not
added to the loopback/localhost address in the /etc/hosts file) and ip-address:8888
. EDIT: According to the posted /etc/hosts details, you are
are binding the VM to the webapp and therefore the webapp to the loopback network. This configuration simply cannot be used outside the VM.
If your web server and applications don't depend on the IP of the first configuration/installation IP, and you have a DHCP server on your LAN, the only other information you need to connect to the VM is the IP address. Assigned to it via DHCP.
Find the IP address of the VM: From the Ubuntu desktop (ctrl-alt-T) open a terminal/console window, we use the hostname, ifconfig and grep /etc/hosts
commands to put the pieces together and find your address. In the terminal that opens, type hostname
and press Enter. It might be gc. In the same terminal, typing ifconfig eth0 | awk '/Bcast/{print $2}'
will display 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 provide a hostname in addition to the current address, you will need to modify the /etc/hosts file to add the correct information.
Hosts on the LAN should now be able to connect to the VM's IP on port 8888. For these hosts/clients, access to the web application hostname:8888
will depend on the client hosts file, the local DNS or even the mDNS service that may be available on the DHCP server. .