179

Here is my setup:

- Host: Windows XP
- Guest: Ubuntu 10.04
- Networking: NAT

I am setting an Apache web server on the Guest, but I want to be able to do this on the Windows machine:

- go to the browser, type http://localhost:8000

Also, I tried to change my networking to bridge and I got a new IP. But when I tried to do http://:8000, it says that it could not connect.

5
  • 1
    I had the same problem. Turned out the guest OS had an active firewall that was blocking port 80. Commented Jul 2, 2013 at 15:32
  • 166
    As a web developer who uses VirtualBox as part of my daily workflow, disagree with this being marked as off topic. Please consider reopening.
    – sparecycle
    Commented Dec 5, 2013 at 22:25
  • 1
    1. Go to the VM 2. ifconfig (get local IP - should be 10.0.2.X) 3. ssh 10.0.2.2 to get to the host machine Commented Feb 13, 2014 at 21:24
  • @deeperDATA It may be in the scope of a web developer's job, but stack overflow isn't meant to encapsulate every part of the job. It's a testament to the complexity of our profession that its requirements span multiple stack exchange sites. Commented Oct 25, 2015 at 3:32
  • 1
    It took me a long time to get it working. Our problem was the ip binding of the application in the guest system, it binded to machine name, meaning 127.0.1.1 in ubuntu. We changed the binding to 0.0.0.0. Port forwarding settings: Host IP = DNS Host IP, Host Port = 8080, Guest IP = IP of eth0, Guest Port = 8080.
    – slowy
    Commented Nov 26, 2015 at 14:21

2 Answers 2

249

Network communication Host -> Guest

Connect to the Guest and find out the ip address:

ifconfig 

example of result (ip address is 10.0.2.15):

eth0      Link encap:Ethernet  HWaddr 08:00:27:AE:36:99
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0

Go to Vbox instance window -> Menu -> Network adapters:

  • adapter should be NAT
  • click on "port forwarding"
  • insert new record (+ icon)
    • for host ip enter 127.0.0.1, and for guest ip address you got from prev. step (in my case it is 10.0.2.15)
    • in your case port is 8000 - put it on both, but you can change host port if you prefer

Go to host system and try it in browser:

http://127.0.0.1:8000

or your network ip address (find out on the host machine by running: ipconfig).

Network communication Guest -> Host

In this case port forwarding is not needed, the communication goes over the LAN back to the host.

On the host machine - find out your netw ip address:

ipconfig

example of result:

IP Address. . . . . . . . . . . . : 192.168.5.1

On the guest machine you can communicate directly with the host, e.g. check it with ping:

# ping 192.168.5.1
PING 192.168.5.1 (192.168.5.1) 56(84) bytes of data.
64 bytes from 192.168.5.1: icmp_seq=1 ttl=128 time=2.30 ms
...

Firewall issues?

@Stranger suggested that in some cases it would be necessary to open used port (8000 or whichever is used) in firewall like this (example for ufw firewall, I haven't tested):

sudo ufw allow 8000 
11
  • 12
    both port will not be 8000. The host port will be 8000 or whaterver u want, but the guest port should be 80
    – Yasin
    Commented Dec 5, 2012 at 10:12
  • 6
    Getting from the VM to the host in this case should be possible by going to the VM and getting ITS IP address (10.0.2.15 as below). To get to the host machine from the VM, the IP is 10.0.2.2 (by convention). Commented Feb 13, 2014 at 21:25
  • 5
    If using NAT for the guest... If the service that is running on the host is bound to 127.0.0.1 only, then the guest cannot use the public ip of the host to connect to that service (example service: privoxy). Instead you need to use 10.0.2.2 as mentioned by Mark, or whatever 'route -n' (run on the guest) shows as the default gateway.
    – desm
    Commented Mar 6, 2014 at 20:20
  • 4
    In case of CentOS, we may have to disable firewall or edit specific rules in the iptable
    – Reddy
    Commented Jun 4, 2015 at 4:37
  • 2
    I've found that it's not necessary to specify Host or Guest IP addresses, and you can just leave the boxes blank. Although if you only want to connect from localhost, it's probably a good idea to put 127.0.0.1 as the Host.
    – mwfearnley
    Commented Mar 6, 2017 at 11:14
15

That's not possible. localhost always defaults to the loopback device on the local operating system.
As your virtual machine runs its own operating system it has its own loopback device which you cannot access from the outside.

If you want to access it e.g. in a browser, connect to it using the local IP instead:

http://192.168.180.1:8000

This is just an example of course, you can find out the actual IP by issuing an ifconfig command on a shell in the guest operating system.

4
  • I tried this as well, but didn't work for both NAT and bridge. The apache logs in the guest doesn't give any errors and the apache is up and running in the guest.
    – Carmen
    Commented Mar 2, 2012 at 18:07
  • Are you sure that you are using the correct IP? You can verify that by trying to access the address from inside the virtual machine. If that fails, you are either using a wrong IP or your apache is not set up to listen on port 8080.
    – Chris
    Commented Mar 2, 2012 at 18:09
  • Worked for me as well. Before replacing "localhost" with the host OS IP address, I always got "Server refused your key" and "Access denied" without explanation in /var/log/auth.log despite LogLevel DEBUG3 in /etc/ssh/sshd_config
    – V-R
    Commented Aug 26, 2016 at 14:57
  • @Chris does this mean that on the host, with virtual host entries like mysite.localhost will not be accessible to the Guest machine, because I must refer to the Host using an IP address only?
    – danjah
    Commented Nov 23, 2016 at 23:34

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