How to connect from Windows 7 to localhost on Ubuntu VirtualBox?

10

7

My host OS is Windows 7

I'm running a (Django development) server on Ubuntu 10.04 in VirtualBox.

I want to connect to that server (through my browser) on my Windows host. How can I do that?

mpen

Posted 2010-08-04T22:52:41.790

Reputation: 9 109

Answers

12

It's also possible to set network adapter mode to 'bridged', then a VM will be seen as a usual machine with self IP address.

eigenein

Posted 2010-08-04T22:52:41.790

Reputation: 262

I set it to "Bridged Adapter". http://localhost:8000/ works on Ubuntu/Guest/VirtualBox but not from my Windows/Host machine; says "Firefox can't establish a connection to the server at localhost:8000." – mpen – 2010-08-05T01:43:25.380

Hah! Got it. Had to explicitly pass in my own IP address when running the server: ./manage.py runserver <MY_IP>:8000. Then it works on both machines. – mpen – 2010-08-05T02:17:04.940

3Try 0.0.0.0:8000 to not pass an exact IP. – eigenein – 2010-08-05T12:18:05.640

@eigenein: I like that. Then I can use localhost on ubuntu, but I still have to use my IP from windows. – mpen – 2010-08-09T02:22:54.737

@Mark It's logical because your virtual Ubuntu is a different OS on a different machine, so Ubuntu is not localhost for Windows. – eigenein – 2010-08-09T15:03:50.897

But it's "Bridged". Thought that meant they "shared" that kinda thing. Not that it's a big deal. – mpen – 2010-08-14T22:45:41.053

6

This did the job for me:

From memory, if you have set up VirtualBox with NAT networking, I think you can access services running on the host from the guest OS. From the guest (WinXP), the host appears as 10.0.2.2. So if you point your WinXP web browser to that address, it should connect to your Ubuntu Apache server. (To the host, this appears to be a connection from localhost, I believe.)

http://ubuntuforums.org/showthread.php?t=682519

tm_lv

Posted 2010-08-04T22:52:41.790

Reputation: 161

This solution also worked for me on Windows 7. I had to use 10.0.2.2 in the hosts file. – Eric Majerus – 2019-08-09T13:13:58.217

3

For that you have to forward the port on your Virtual Machine.

Go to the command prompt and switch to your virtual box folder, there you can issue the following commands:

VBoxManage.exe setextradata "name of vm" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/HostPort" 8888
VBoxManage.exe setextradata "name of vm" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/GuestPort" 80
VBoxManage.exe setextradata "name of vm" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/http/Protocol" TCP

Switch "name of vm" with the name of your virtual machine, as well as the GuestPort with the port your web server is using, as it might use port 8080, or something different. After that you should be able to access the server via http://localhost:8888/ if I'm not mistaken.

Edit: P.S.: as far as I remember, you need to set your network adapter on "NAT" for that

Edit 2: The VirtualBox Manual which can be found here includes a section about port forwarding, done slightly different to the way described here. Read it in chapter 6.3.1. (For version 3.2.6)

private_meta

Posted 2010-08-04T22:52:41.790

Reputation: 2 204

After running those commands, I can no longer boot up Ubuntu. – mpen – 2010-08-05T01:45:48.887

This command VBoxManage modifyvm "VM name" --natpf1 "djangoserver,tcp,,8000,,8000" doesn't seem to work either... when I try accessing it from both localhost:8000 on my guest machine, and the IP address given by ifconfig. – mpen – 2010-08-05T01:56:54.450

Oops... still had it on Bridged. Switched back to NAT and now it's just timing out... wonder what that means? – mpen – 2010-08-05T02:00:03.743

Found this note in the Django docs "Note that the default IP address, 127.0.0.1, is not accessible from other machines on your network. To make your development server viewable to other machines on the network, use its own IP address or 0.0.0.0." -- tried this too, still "taking too long to respond" – mpen – 2010-08-05T02:07:37.750

2Port forwarding in this case should work in a way that accessing "localhost:8888" or "127.0.0.1:8888" on the host system translates to accessing "localhost:80" or "127.0.0.1:80" on the guest machine, to my knowledge. Maybe you can try forward SSH or something similar already running to see if port forwarding works correctly. Also, have made sure that the server works on your guest machine (by using guest machines firefox and opening the page?)

P.S. well it seems you got your answer already anyway :) – private_meta – 2010-08-05T09:49:12.950

1

My host and guest OS is Windows 7 and the only way to connect them is configure the network adapter on "NAT" mode and then use the IP 10.0.2.2 like @tm_lv wrote https://superuser.com/a/287323/314495

I tried to set the network adapter to "Brigde" mode. I disabled both firewalls and used the right IP but didn't work. I don't know why.

Kikers25

Posted 2010-08-04T22:52:41.790

Reputation: 11