macos - Vagrant VirtualBox 无法正确进行端口转发

标签 macos vagrant ip virtualbox portforwarding

这就是我的问题。我使用 Vagrant 在我的 Mac 上安装了虚拟机。

  • Vagrant :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. 在 guest 计算机中运行 ifconfig 不会显示该设备的 IP 地址,仅显示硬件地址。

  3. 当我尝试根据错误消息的建议增加 Vagrant 超时(增加到 3600s = 1h)时,lsof 显示端口不断转发,并因消息而中断 (CLOSED_WAIT),然后一遍又一遍地重新转发到另一个端口,因此必须使用 ctl-C 手动停止 vagrant up

    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)]
    

    等等

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

最佳答案

我明白了!

问题是我的虚拟机尝试连接到 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

虚拟机启动得很好。我花了几天时间研究这个问题!

关于macos - Vagrant VirtualBox 无法正确进行端口转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44636394/

相关文章:

java - 使用Java获取外部IP地址

iphone - 从 NSString 中读取指定位置的 UTF8 字符

macos - 如何在Mac OS X 10.6下使用脚本自动执行新系统配置?

json - Vagrant (Saltstack) 如何使用 salt 状态安装package.json?

php - 基于网络更改PHP PDO MySQL连接的IP

ubuntu - linux Vagrant ubuntu/xenial 不同机器的不同登录用户

php - 如何在 PHP 中通过 IP 地址获取时区

linux - 从 NFS 安装文件夹开始更改 bash 脚本中的网络 ip

objective-c - 线程1 :EXC_BAd_InSTRUCTION(code =EXC_1386_INVOP,子代码)

xcode - 无法在安装了命令行工具的 Mavericks 上使用 xcodebuild