12

Okay so I have my actual laptop which has vmware player installed. I am running lubuntu as a virtual machine and I installed django on the virtual machine and am testing my app so I did python manage.py runserver and I can access the app by visiting 127.0.0.1:8000 from my VM, however, If I go to 127.0.0.1:8000 from the actual computer (not the VM), it says 'chrome could not connect to 127.0.0.1:8000'.. Any idea how to fix it?

SilentDev
  • 20,997
  • 28
  • 111
  • 214
  • Have a look at similar topics: http://stackoverflow.com/questions/1261975/addressing-localhost-from-a-virtualbox-virtual-machine http://stackoverflow.com/questions/1155487/access-tomcat-localhost8080-of-guest-virtualbox-vm-from-host-os – ambi Aug 31 '13 at 19:14
  • What network mode do you adopt ? Bridge ? NAT ? Host-only ? – Jeff Li Sep 08 '13 at 00:41
  • @JeffLi NAT.. when I try to change the network mode to bridge, my guest OS losses internet access. – SilentDev Jun 18 '14 at 03:00

3 Answers3

17

You can try running the server on 0.0.0.0

python manage.py runserver 0.0.0.0:8000

The IP address 0.0.0.0 means "all IP addresses on the local machine" (or all IPv4 addresses on the local machine).

Next, you will need the ip address of your VM. Visting http://<ip_address_of_vm>:8000 on other computers should access the django development server on your VM.

Note: If your VM only has an internal IP (e.g. 192.168.x.x) then only computers on the same network can visit the VM.

Derek Kwok
  • 12,768
  • 6
  • 38
  • 58
  • Hm, didn't work, so I did python manage.py runserver 0.0.0.0:8000 and from the vm, went to 0.0.0.0:8000 and it worked. I then opened up the terminal on the vm and typed ifconfig, it says inet addr:192.168.174.132 so I went to 192.168.174.132:8000 from my computer but that didn't work.. any ideas on what's wrong? – SilentDev Sep 03 '13 at 22:55
  • 1
    I was able to do this, and connect on the host to `localhost:8000`. I have Virtualbox set up to use NAT and forward port 8000 on the guest. – Dan Nov 22 '15 at 17:21
  • 1
    There is a shortcut for doing this, if you run `python manage.py runserver 0:8000` then it is accessible from host machine (of course if you setup port forwarding etc.) – mndeveci Mar 26 '18 at 12:27
15

I was able to get @Kerberos answer to work. (not enough points to comment so I'm adding it as a seperate answer).

I am running Ubuntu 12.04 LTS in a guest OS in VMWare. The host laptop is running Windows 8.

As mentioned by Kerberos, in VMWare, go to Player ==> Manage ==> Virtual Machine Settings...

On the Hardware tab, select Network Adaptor, then select the radio button for Bridged: Connect directly to the physical network. Select OK

In the VM, the network connection information should now have the same IP address of the host OS internet connection. In my case: 192.168.1.141 (yours will vary).

In the VM, start Django using python manage runserver 192.168.1.141:8000

Using this method, I am able to access the webserver running in the VM at this IP address from within the VM, from host machine, and from other systems on the same 192.168.1.xxx network.

ChrisFreeman
  • 5,831
  • 4
  • 20
  • 32
  • right, but my problem is that even thought I tried what you did, once I set the connection of the VM to 'Bridged' and hit okay, I was unable to connect to the internet from my VM. My IP address for the VM also did not change and when I tried running the server on my host OS's IP address, it gave an error saying 'that ip address can't be assigned-to' and I confirmed that the two lines which are suggested here: s/13505540/error-that-ip-address-cant-be-assigned-to-in-django are in my etc/network/interfaces – SilentDev Jun 18 '14 at 23:42
  • 2
    Okay so I just shut down my VM and restarted it and then bridged the network and it worked. After bridging, I also just ran the surver on 0.0.0.0:8000 and then accessed my VM's ip address (192.169.2.49:8000) from my host OS's Google Chrome and it worked. – SilentDev Jun 19 '14 at 04:37
  • Perfect answer! – XpressGeek Jun 24 '21 at 06:41
  • This answer makes sense and actually works. Also, is there a way to configure this so that we don't have to type the full IP in the runserver command? – Bhanu Prakash Oct 09 '22 at 20:11
2

to access virtual machine (guest) from outside the host computer, you have to set the guest network mode to bridge

Bridge-mode connects the guest to the actual network so that other machines can connect to it

Kerberos
  • 103
  • 1
  • 6
  • I went to VMWare Player, clicked 'Player -> Manage -> Virtual Machine Settings' and then changed 'Network Adapter' from 'NAT' to 'Bridged'... after doing this, my guest OS couldn't lost internet connection. I ran the django server on 0.0.0.0:8000 and could access it by going to either 0.0.0.0:8000 OR http://192.168.174.233:8000/ from the guest OS, but I couldn't access it from either of the two links from the host OS.. any idea why? – SilentDev Jun 18 '14 at 02:21