Access virtual machine from browser under virtual box
I am running on 20.04.1. I have a virtual machine installed using Oracle VirtualBox 6.12. The VM is also Ubuntu 20.04. The virtual machine networking type is bridged. When I try to access the virtual machine from Google Chrome or from Firefox on the host, I get a report of connection refused on the virtual machine, I can use ping on the VM to access both localhost and the IP address. nmap on the VM can acesss localhost successfully but not the IP address of the VM. Neither ping nor nmap can access localhost or the IP address of the VM when used from the host. Both machines are on the same subnet. I have tried with ufw both enabled and disabled on the VM. This has worked before for me using the same host but a different VM. In addition, I cannot use ssh on port 22 from the host to the VM with the same problem.
I tried making a new virtual machine which changed the problem slightly. In this case, I was still unable to connect to a web server on the VM through a browser on my host but I was able to ping the VM from the host. nmap on the host still stated that it thought that the VM was down.
Any ideas on how I can troubleshoot this?
12 Answers
I think you need to enable port-forwarding on the VM. (with the NAT type in the Attached to option)
In Virtualbox, choose the Settings of the context menu of the VM (or Machine -> Settings while the VM is running), go to Network, then expand the Advanced option and choose Port Forwarding. In the dialogbox that shows up, click the + sign to add a new TCP/IP rule (if not exists).
In both the Host Port and Guest Port fields, enter a port number (e.g. 8080), then click OK.
Now, you should be able to access the servers (e.g. HTTP on 8080) on the guest (if you have configured an HTTP server at that port).
It turns out that the problem had nothing to do with the operating system. The web server was coming up on 127.0.0.1 which was not accessible on the network. Changing it to come up on 0.0.0.0 fixed the problem.