Virtualbox "port forward" from Guest to Host
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.
Related videos on Youtube
Carmen
Updated on July 08, 2022Comments
-
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 almost 11 yearsI had the same problem. Turned out the guest OS had an active firewall that was blocking port 80.
-
sparecycle over 10 yearsAs 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 about 10 years1. 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 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 over 8 yearsIt 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 about 12 yearsI 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 about 12 yearsAre 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 over 11 yearsboth 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 about 11 yearsIt took some time, but it worked like charm! :) Thanks buddy! :)
-
Matt Frear almost 11 yearsWorked for me - thanks!
-
Mark Roberts about 10 yearsGetting 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 about 10 yearsIf 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 almost 9 yearsIn case of CentOS, we may have to disable firewall or edit specific rules in the iptable
-
V-R over 7 yearsWorked 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 over 7 yearsIf guest is a Windows machine, you may have to disable or setup windows firewall too
-
alexbt over 7 yearsWorked 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 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 about 7 yearsI'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 almost 7 yearsYou 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 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 almost 4 yearsYou should also allow the port 9200 in the firewall like by using, sudo ufw allow 9200