【问题标题】:Vagrant VirtualBox doesn't properly port-forwardVagrant VirtualBox 无法正确进行端口转发
【发布时间】:2017-11-22 00:20:17
【问题描述】:

所以这是我的问题。我用 Vagrant 在我的 Mac 上安装了一个虚拟机。

  • 流浪者:1.9.1
  • 虚拟框:4.3.38
  • OSX:10.11.6
  • 机器:geerlingguy/ubuntu1604

当我运行 vagrant up 时,我得到了这个输出

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'geerlingguy/ubuntu1604'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'geerlingguy/ubuntu1604' is up to date...
==> default: Setting the name of the VM: <name>
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

但是,运行vagrant status 表明该框已启动并正在运行。当我尝试将vagrant ssh 放入框中时,它会挂起一段时间然后退出

ssh_exchange_identification: Connection closed by remote host

最后,在 GUI 模式下启动机器可以让我成功登录。现在,玩了一段时间后,我注意到了以下几点。

  1. 运行lsof -i 2222 表明端口实际上并未按应有的方式转发。

    VirtualBo 38361 <user>   29u  IPv4 <device>      0t0  TCP localhost:2222 (LISTEN)
    
  2. 在来宾机器上运行ifconfig 显示没有IP 地址,只有硬件地址。

  3. 当我尝试根据错误消息的建议增加 Vagrant 超时(至 3600s = 1h)时,lsof 显示端口不断被转发,消息中断(CLOSED_WAIT),然后重新转发到另一个端口,超过一遍又一遍,以至于vagrant up 不得不用 ctl-C 手动停止。

    VirtualBo 38361 <user>   29u  IPv4 <device>      0t0  TCP localhost:rockwell-csp2 (LISTEN)
    VirtualBo 38361 <user>   30u  IPv4 <device>      0t0  TCP localhost:rockwell-csp2->localhost:61746 (CLOSE_WAIT)
    VirtualBo 38361 <user>   31u  IPv4 <device>      0t0  TCP localhost:rockwell-csp2->localhost:61750 (CLOSE_WAIT)
    VirtualBo 38361 <user>   32u  IPv4 <device>      0t0  TCP localhost:rockwell-csp2->localhost:61753 (CLOSE_WAIT)
    VirtualBo 38361 <user>   30u  IPv4 <device>      0t0  TCP localhost:rockwell-csp2->localhost:61746 (CLOSE_WAIT)
    VirtualBo 38361 <user>   18u  IPv4 <device>      0t0  TCP localhost:rockwell-csp2->localhost:61748 (CLOSE_WAIT)
    VirtualBo 38361 <user>   29u  IPv4 <device>      0t0  TCP localhost:rockwell-csp2 (LISTEN)
    VirtualBo 38361 <user>   30u  IPv4 <device>      0t0  TCP localhost:rockwell-csp2->localhost:61746 (CLOSE_WAIT)
    VirtualBo 38361 <user>   31u  IPv4 <device>      0t0  TCP localhost:rockwell-csp2->localhost:61750 (CLOSE_WAIT)
    VirtualBo 38361 <user>   32u  IPv4 <device>      0t0  TCP localhost:rockwell-csp2->localhost:61753 (CLOSE_WAIT)]
    

    等等

非常感谢您的建议,因为这几天让我发疯了。

【问题讨论】:

  • 盒子没有 IP,因为 vagrant 无法正确设置它 - 你能检查.ssh 文件夹是否有正确的权限,authorized_keys 是否正确

标签: macos vagrant ip virtualbox portforwarding


【解决方案1】:

我想通了!

问题是我的虚拟机试图连接到 VirtualBox 中的 NAT 适配器(适配器 1),但默认情况下适配器 1 已断开连接。阅读此 GitHub 问题线程 (https://github.com/mitchellh/vagrant/issues/7648) 后,我添加了

config.vm.provider 'virtualbox' do |vb|
  vb.customize ['modifyvm', :id, '--cableconnected1', 'on']
end

VM 启动得很好。我在这个问题上工作了 DAYS!

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 2014-07-30
    • 2018-10-14
    相关资源
    最近更新 更多