36

I have a windows Vista machine upon which VirtualBox is running ubuntu.

In ubuntu I am running a webserver at the address http://127.0.0.1:3000/

How do I access this address from my host machine (the windows box)?

Argh
  • 507
  • 1
  • 4
  • 6
  • 1
    Simplest option is to use port Forwarding. It can be configured under Setting->Network. Check this [link](https://nsrc.org/workshops/2014/btnog/raw-attachment/wiki/Track2Agenda/ex-virtualbox-portforward-ssh.htm) – spartan Oct 29 '15 at 09:18
  • If you really configured your guest's web server at 127.0.0.1 then you can't access it from the outside of the guest. The localhost/127.0.0.1 is a loopback adapter (in-memory) and doesn't even require the network card to be installed to work properly. If you change your web server's ip to something other than the loopback, then the answers below will be able to help. – Mladen B. Nov 20 '17 at 15:16
  • Port forwarding using NAT worked for me. – Justin Phillips Jun 22 '21 at 12:15

5 Answers5

36

You can change your guest networking mode from NAT to Bridged. Just close your Ubuntu guest, go to the Settings page in VirtualBox, select the Network category and change the Attached to listbox from NAT to Bridged Mode.

Edit:

After following the above steps restart Ubuntu and the allocated IP will show on the welcome screen after load. i.e. In the welcome message it should say something like:

IP address for eth0: 192.168.1.3

Use this address in the browser bar of the host machine followed by the port your using. If its anything other than port 80. i.e. if you set it up to use port 666 then the address would be 192.168.1.3:666

Another way to find the IP is to connect to the HTTP server built into your router and look in LAN settings. Every machine on the network should be listed by host name and IP. On my router most others I've had, you can do this by typing 192.168.1.1 in the address bar. if that fails try same address at port 81. If that fails READ THE MANUAL.

slm
  • 7,355
  • 16
  • 54
  • 72
  • But then how can you find out what the IP address is? Thx – SSH This Jan 23 '13 at 22:27
  • That will work but the option of host-only didnt expose that vm to the corporate network – Carlos Garcia Oct 29 '15 at 09:42
  • 3
    Am I the only one here who thinks that the guest's endpoint of 127.0.0.1 can only be reached from within the guest (that's the point of localhost)? There should be no way to access that kind of an endpoint from anything outside of the guest machine (because it's a loopback adapter, it doesn't even require an installed network card). – Mladen B. Nov 20 '17 at 15:13
  • if i enter this 192.1681.3 as the IP for an A record on Godaddy it does not work, obviously it expects an external IP address, if I put that, still doesnt work for some reaosn, I want to test a domain name locally before going on EC2 – PirateApp Aug 28 '19 at 03:15
  • This is a good answer and on the Linux Guest OS, you may also have to: 1 [Stop IPTables using: sudo service iptables stop] 2 [Turn off SELinux using: sudo setenforce 0] – Craig Gjerdingen Oct 22 '20 at 13:14
14

Depends how you setup networking on the Ubuntu guest OS. If you set it up to use NAT and you have a DHCP server running on your network, then just run this:

ifconfig | grep inet

on the Ubuntu box -- you'll see the IP address assigned to the Ubuntu guest.

Example:

ifconfig | grep inet
# inet addr:10.0.0.2  Bcast:10.255.255.255  Mask:255.255.255.0
# inet addr:127.0.0.1  Mask:255.0.0.0

Then just hit up http://10.0.0.2:3000.

524F4A
  • 103
  • 4
fsckin
  • 555
  • 4
  • 9
  • Well in my case the address should be http://10.0.2.15:3000/public and by default vista runs in DHCP mode..(the ubuntu machine is also configured to use NAT) however I'm not getting a response :( – Argh Apr 28 '10 at 18:13
  • Hmmm just realised that I dont have a DHCP server - I'm running vista home... – Argh Apr 28 '10 at 18:15
  • VirtualBox has it's own DHCP server that handles virtual machines - otherwise virtual machines wouldn't automatically get an IP address without setting them statically. I think your problem might be that you're not having Apache listen on all interfaces - just localhost (127.0.0.1). Make sure that in /etc/apache2/sites-available/default the Listen directive says 80, not 127.0.0.1:80 per https://help.ubuntu.com/9.04/serverguide/C/httpd.html – Cory Plastek Apr 28 '10 at 19:22
  • This is probably the cause. – fsckin Apr 30 '10 at 18:44
6

Your Windows box and the Ubuntu box (whether they are physical or virtual doesn't really matter in this instance) both need to have an IP address on the same subnet so they can talk to one another via TCP/IP. You can assign an additional address to each OS using one of the private IP subnets (see RFC 1918). Once they both have an address on the same subnet, configure the web server to listen on the one you gave Ubuntu, and then use that IP in the browser within Windows to load it up.

The IP you have configured now, 127.0.0.1, is the loopback address for the local network interface. You would never use that address to access something on a different system, only to refer back to the same system you're on. When using a virtual system, treat it just like you would a separate computer from a networking standpoint.

Justin Scott
  • 8,748
  • 1
  • 27
  • 39
4

First of all, you need to do what Answer 1 says:

"You can change your guest networking mode from NAT to Bridged. Just close your Ubuntu guest, go to the Settings page in VirtualBox, select the Network category and change the Attached to listbox from NAT to Bridged Mode."

Then, to get your ip address:

On your virtual machine, open a command prompt and do:

ip addr show

You will probably see a line that looks something like this:

inet 192.168.43.96/24 ....

There is also a line before that with

inet 127.0.0.1

But you don't want that one! You want the 192 address. That's the address of your virtual machine on your local network. It should be accessible by any device hooked up to the same local network.

Now on your host machine, edit the file /etc/hosts (Not ~/etc) The name of the file is hosts (it's not a folder name)

add a line that looks like this:

192.168.43.96 myvm.nameofmywebsite.com

Now go to your host machine browser and type in myvm.nameofmywebsite.com

Hopefully that will do it!

However, you may have a router that has security features that will get in the way of seeing your local address. What you can try to do is to use your cellphone's hotspot as your network. That works for me. Just remember when you change network on your host machine that you need to restart your virtualbox.

2

To solve your problem i sugest that you add a new network adapter and configure it as Host-only adapter.

Now you run the virtual box instance and then what fsckin sugested to see what is the ip of the new network.

This way you have a subnet with your computer and your virtual machine.

Filipe Pinheiro
  • 157
  • 1
  • 7
  • So... I did as you said grabbed my IP address. I then tried to access the webserver (which I can access within the guest as http://127.0.0.1:3000) using http://allocatedIP:3000 and I didnt get a response. I could however ping the ip from the host – Argh Apr 29 '10 at 09:58
  • You need to open the firewall on port 3000. – Filipe Pinheiro May 16 '10 at 07:07
  • Host-only won't work out of the box: http://serverfault.com/questions/205477/can-ping-between-host-and-guest-but-cant-acces-webserver-with-virtualbox – givanse Nov 24 '10 at 17:17