Virtualbox "port forward" from Guest to Host

197,268

Solution 1

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 

Solution 2

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.

Share:
197,268

Related videos on Youtube

Carmen
Author by

Carmen

Updated on July 08, 2022

Comments

  • Carmen
    Carmen almost 2 years

    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.

    • Nicholas Shanks
      Nicholas Shanks almost 11 years
      I had the same problem. Turned out the guest OS had an active firewall that was blocking port 80.
    • sparecycle
      sparecycle over 10 years
      As a web developer who uses VirtualBox as part of my daily workflow, disagree with this being marked as off topic. Please consider reopening.
    • Mark Roberts
      Mark Roberts about 10 years
      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
    • Kevin Lawrence
      Kevin Lawrence over 8 years
      @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.
    • slowy
      slowy over 8 years
      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.
  • Carmen
    Carmen about 12 years
    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.
  • Chris
    Chris about 12 years
    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.
  • Yasin
    Yasin over 11 years
    both port will not be 8000. The host port will be 8000 or whaterver u want, but the guest port should be 80
  • Praveen Kumar Purushothaman
    Praveen Kumar Purushothaman about 11 years
    It took some time, but it worked like charm! :) Thanks buddy! :)
  • Matt Frear
    Matt Frear almost 11 years
    Worked for me - thanks!
  • Mark Roberts
    Mark Roberts about 10 years
    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).
  • desm
    desm about 10 years
    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.
  • Reddy
    Reddy almost 9 years
    In case of CentOS, we may have to disable firewall or edit specific rules in the iptable
  • V-R
    V-R over 7 years
    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
  • Gian Marco
    Gian Marco over 7 years
    If guest is a Windows machine, you may have to disable or setup windows firewall too
  • alexbt
    alexbt over 7 years
    Worked like charm! => I used the following command to get the guest IP: "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" guestproperty get default "/VirtualBox/GuestInfo/Net/0/V4/IP" (where 'default' is the vm name')
  • danjah
    danjah over 7 years
    @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?
  • mwfearnley
    mwfearnley about 7 years
    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.
  • abbas
    abbas almost 7 years
    You do not need to find the IP address. Leave the IP addresses empty in the rule and access the page using localhost:portNumber
  • Matt Royal
    Matt Royal about 5 years
    @GianMarcoGherardi took me a while to eventually figure this out, if only I head read all the comments first :) I had to configure Windows Firewall to allow the port we were using.
  • Stranger
    Stranger almost 4 years
    You should also allow the port 9200 in the firewall like by using, sudo ufw allow 9200