如何从 Mac OSX 连接到 Vagrant 中的服务器?

How to connect to a server in Vagrant from Mac OSX?

我正在尝试 运行 在我的 Mac OSX 运行ning Yosemite 上使用 Vagrant 对我们的构建服务器 (Jenkins) 进行本地设置.我环顾四周,发现服务必须监听所有接口(0.0.0.0 或 ::::)的要求。

我有 Vagrant,还有 运行ning 和 Jenkins 运行ning。以下是 sudo netstat -tulpn:

的输出
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      965/sshd        
tcp        0      0 0.0.0.0:57349           0.0.0.0:*               LISTEN      653/rpc.statd   
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      591/rpcbind     
tcp6       0      0 :::22                   :::*                    LISTEN      965/sshd        
tcp6       0      0 :::57689                :::*                    LISTEN      2205/java       
tcp6       0      0 :::35586                :::*                    LISTEN      2205/java       
tcp6       0      0 :::33991                :::*                    LISTEN      653/rpc.statd   
tcp6       0      0 :::111                  :::*                    LISTEN      591/rpcbind     
tcp6       0      0 :::8080                 :::*                    LISTEN      2205/java       
udp        0      0 0.0.0.0:111             0.0.0.0:*                           591/rpcbind     
udp        0      0 0.0.0.0:60038           0.0.0.0:*                           653/rpc.statd   
udp        0      0 0.0.0.0:6057            0.0.0.0:*                           496/dhclient    
udp        0      0 0.0.0.0:765             0.0.0.0:*                           591/rpcbind     
udp        0      0 127.0.0.1:829           0.0.0.0:*                           653/rpc.statd   
udp        0      0 0.0.0.0:68              0.0.0.0:*                           496/dhclient    
udp6       0      0 :::111                  :::*                                591/rpcbind     
udp6       0      0 :::57046                :::*                                653/rpc.statd   
udp6       0      0 :::5353                 :::*                                2205/java       
udp6       0      0 :::765                  :::*                                591/rpcbind     
udp6       0      0 :::52531                :::*                                496/dhclient    
udp6       0      0 :::33848                :::*                                2205/java

Jenkins 正在侦听端口 8080,并且正在侦听 ::::(所有 IPv6)。我可以 curl localhost:8080 并在虚拟机内部得到响应,但是从我的主机我得到以下信息:

* Rebuilt URL to: localhost:8080/
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.43.0
> Accept: */*
> 
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server 

我也试过让 Java 使用 IPv4 绑定地址,以便监听地址是 0.0.0.0 但是我得到了相同的结果。

以下是我的 /etc/hosts 文件的输出:

127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

到目前为止,根据我所读的内容,一切都已按预期设置,但我一定是做错了什么。你们能帮我弄到这个 运行ning 吗?

如果您需要更多信息,请告诉我,我会post在这里。

干杯, 阿列克谢蓝.

您需要 forward port 从来宾到主机,因此将其添加到您的 Vagrantfile

config.vm.network "forwarded_port", guest: 8080, host: 8080

确保通过 vagrant reload 重新加载您的 VM 以生效,您将能够从您的主机访问