Skip to content

Instantly share code, notes, and snippets.

@garrettwilkin
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garrettwilkin/e3eadbd6a78c44a044c6 to your computer and use it in GitHub Desktop.
Save garrettwilkin/e3eadbd6a78c44a044c6 to your computer and use it in GitHub Desktop.
VM, Y U NO RESPOND? Is there anyway to open a port on a vagrant VM other than using the forwarded ports? Below are my vagrant file, netstat from inside the VM, nmap scan of the VM from the host machine. My webserver is running on port 8000 in the VM but I can't access it from the HOST, even though the VM has a private IP if 192.168.13.37.

Resolved!

Turns out that the django app providing the HTTP server was listening only to the localhost interface, 127.0.0.1. In order to be reachable outside the VM, it it needed to listen to 0.0.0.0.

I fixed this by running the server like this:

python manage.py 0.0.0.0:8000

$ curl -X GET 'http://192.168.13.37:8000'
curl: (7) Failed connect to 192.168.13.37:8000; Connection refused
$ nmap -PN 192.168.13.37
Starting Nmap 6.46 ( http://nmap.org ) at 2014-07-28 16:54 EDT
Nmap scan report for local.hacktivate.org (192.168.13.37)
Host is up (0.00097s latency).
Not shown: 998 closed ports
PORT STATE SERVICE
22/tcp open ssh
111/tcp open rpcbind
Nmap done: 1 IP address (1 host up) scanned in 1.16 seconds
» ~/Code/Hacktivate
$
$ nmap -p 5432 192.168.13.37
Starting Nmap 6.46 ( http://nmap.org ) at 2014-07-29 09:25 EDT
Nmap scan report for local.hacktivate.org (192.168.13.37)
Host is up (0.00046s latency).
PORT STATE SERVICE
5432/tcp closed postgresql
Nmap done: 1 IP address (1 host up) scanned in 1.15 seconds
» ~/Code/Hacktivate
$
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "hack_local"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.13.37"
config.ssh.forward_agent = true
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
config.vm.provider "virtualbox" do |vb|
vb.name = 'hack_local'
end
end
vagrant@vagrant-ubuntu-trusty-64:~$ curl -X GET 'http://127.0.0.1:8000'
<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="robots" content="NONE,NOARCHIVE"><title>Welcome to Django</title>
<style type="text/css">
html * { padding:0; margin:0; }
body * { padding:10px 20px; }
body * * { padding:0; }
body { font:small sans-serif; }
body>div { border-bottom:1px solid #ddd; }
h1 { font-weight:normal; }
h2 { margin-bottom:.8em; }
h2 span { font-size:80%; color:#666; font-weight:normal; }
h3 { margin:1em 0 .5em 0; }
h4 { margin:0 0 .5em 0; font-weight: normal; }
table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
tbody td, tbody th { vertical-align:top; padding:2px 3px; }
thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
#summary { background: #e0ebff; }
#summary h2 { font-weight: normal; color: #666; }
#explanation { background:#eee; }
#instructions { background:#f6f6f6; }
#summary table { border:none; background:transparent; }
</style>
</head>
<body>
<div id="summary">
<h1>It worked!</h1>
<h2>Congratulations on your first Django-powered page.</h2>
</div>
<div id="instructions">
<p>
Of course, you haven't actually done any work yet.
Next, start your first app by running <code>python manage.py startapp [appname]</code>.
</p>
</div>
<div id="explanation">
<p>
You're seeing this message because you have <code>DEBUG = True</code> in your
Django settings file and you haven't configured any URLs. Get to work!
</p>
</div>
</body></html>
vagrant@vagrant-ubuntu-trusty-64:~$ sudo iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
vagrant@vagrant-ubuntu-trusty-64:~$ sudo netstat -ntlp | grep LISTEN
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 1189/postgres
tcp 0 0 127.0.0.1:5050 0.0.0.0:* LISTEN 2781/python
tcp 0 0 0.0.0.0:51945 0.0.0.0:* LISTEN 699/rpc.statd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 666/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1523/sshd
tcp6 0 0 :::36929 :::* LISTEN 699/rpc.statd
tcp6 0 0 :::111 :::* LISTEN 666/rpcbind
tcp6 0 0 :::22 :::* LISTEN 1523/sshd
vagrant@vagrant-ubuntu-trusty-64:~$
vagrant@vagrant-ubuntu-trusty-64:~$ date
Tue Jul 29 13:47:12 UTC 2014
vagrant@vagrant-ubuntu-trusty-64:~$ sudo netstat -ntlp | grep LISTEN
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 1226/postgres
tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN 1980/python
tcp 0 0 0.0.0.0:34415 0.0.0.0:* LISTEN 736/rpc.statd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 627/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1476/sshd
tcp6 0 0 :::48015 :::* LISTEN 736/rpc.statd
tcp6 0 0 :::111 :::* LISTEN 627/rpcbind
tcp6 0 0 :::22 :::* LISTEN 1476/sshd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment