vagrant port forwarding doesn't work: Connection reset by peer

Viewed 5911

ssh port forwarding (which is enabled by default) works fine, but my custom port forwardings don't.

I took the config below from the official docs, but it doesn't work :(

host machine runs Ubuntu 17.04

Vagrantfile is:

Vagrant.configure("2") do |config|
    config.vm.box = "ubuntu/xenial64"
    # though, I've also tried ubunty/zesty64 and centos/7 - with same results
    config.vm.provision :shell, path: "vagrant/provision.sh"
    config.vm.network "forwarded_port", guest: 3000, host: 3001
end

http server is node.js, listening port 3000 at guest machine

when I run vagrant up I see that there are 2 port forwardings:

==> default: Forwarding ports...
    default: 3000 (guest) => 3001 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)

I successfully connect to guest via ssh on port 2222, thus the forwarding 22 (guest) => 2222 (host) works fine.

Accessing port 3000 from the guest machine (via ssh) also works fine:
(the following is a quote from ssh console when connected to guest machine)

ubuntu@ubuntu-xenial:~$ curl -v http://localhost:3000/
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Date: Fri, 14 Jul 2017 12:34:29 GMT
< Connection: keep-alive
< Content-Length: 12
< 
Hello World
* Connection #0 to host localhost left intact

But requesting port 3001 directly from host machine fails:
(the following is a quote from a regular console (not ssh) on local host machine)

$ curl -v http://127.0.0.1:3001/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 3001 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:3001
> User-Agent: curl/7.52.1
> Accept: */*
> 
* Recv failure: Connection reset by peer
* Curl_http_done: called premature == 1
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer

I also found out this has nothing to do with firewall, 'cause it's disabled (on both machines):

ubuntu@ubuntu-xenial:~$ sudo ufw status
Status: inactive

I also noticed that all other ports on both machines return the same:

ubuntu@ubuntu-xenial:~$ curl http://127.0.0.1:3003/
curl: (7) Failed to connect to 127.0.0.1 port 3003: Connection refused

— which simply means that those ports are closed on respective machine, and that is completely OK.

And only the port 3001 on host machine returns Connection reset by peer:

$ curl http://127.0.0.1:3001/
curl: (56) Recv failure: Connection reset by peer

— that means that something's wrong in network settings. But what exactly?

please, help me to set up port forwardings correctly!

UPDATE 1

Maybe this will be important: when upping vagrant, the following is printed in console:

==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 3000 (guest) => 3001 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)

Maybe the reason is that Vagrant for some reason forwards ports for the wrong adapter? Maybe it should forward for hostonly, and it forwards for nat? I don't understand it, it's just an assumption.

2 Answers

switching from 127.0.0.1:3000 to 0.0.0.0:3000 indeed exposes the guest running services to the host network, however, i feel like the better way should exist, anyway, worth mentioning, I noticed that running netstat -nltp on guest OS will show the 'foreign addresses' which are the ones that will be exposed to the host OS.

Related