无法从 Ubuntu 主机访问 Vagrant 提供的 VirtualBox VM

Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host

我正在关注 basic Vagrant tutorial,而 Vagrant 在我的 Ubuntu 上提供了一个框,没有抱怨。而且,我可以vagrant ssh进入虚拟机。但是,无法从主机访问 VM:无法 ping 它的 IP 地址。

我的收获:

这是我尝试从主机访问来宾 VM 时得到的结果:

~/tmp/myfirstvagrantproject$ ping 172.22.22.22
PING 172.22.22.22 (172.22.22.22) 56(84) bytes of data.
From ... icmp_seq=4 Destination Net Unreachable
...
171 packets transmitted, 0 received, +21 errors, 100% packet loss, time 173411ms

工件如下:

1.Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# General project settings
#################################

  # IP Address for the host only network, change it to anything you like
  # but please keep it within the IPv4 private network range
  ip_address = "172.22.22.22"

  # The project name is base for directories, hostname and alike
  project_name = "projectname"


# Vagrant configuration
#################################

  Vagrant.configure("2") do |config|


    # Define VM box to use
    config.vm.box = "precise32"
    config.vm.box_url = "http://files.vagrantup.com/precise32.box"

    # Set share folder


    # Use hostonly network with a static IP Address and enable
    # hostmanager so we can have a custom domain for the server
    # by modifying the host machines hosts file
    config.hostmanager.enabled = true
    config.hostmanager.manage_host = true
    config.vm.define project_name do |node|
      node.vm.hostname = project_name + ".local"
      node.vm.network :private_network, ip: ip_address
      node.hostmanager.aliases = [ "www." + project_name + ".local" ]
    end
    config.vm.provision :hostmanager



 end

2.ifconfig 在来宾虚拟机上:

vagrant@projectname:~$ ifconfig

eth0      Link encap:Ethernet  HWaddr 08:00:27:12:96:98  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe12:9698/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1481 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1178 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:156103 (156.1 KB)  TX bytes:132629 (132.6 KB)

eth1      Link encap:Ethernet  HWaddr 08:00:27:06:1a:26  
          inet addr:172.22.22.22  Bcast:172.22.22.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe06:1a26/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:468 (468.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
  1. 主机的ifconfig(我的主机连接的是无线网络):

    ifconfig br-34f3008c7cda Link encap:Ethernet HWaddr [已删除]
    inet addr:172.18.0.1 Bcast:0.0.0.0 Mask:255.255.0.0 向上广播多播 MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

    docker0   Link encap:Ethernet  HWaddr [removed]  
              inet addr:172.17.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
              UP BROADCAST MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    
    enp8s0    Link encap:Ethernet  HWaddr [removed]  
              UP BROADCAST MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:11151 errors:0 dropped:0 overruns:0 frame:0
              TX packets:11151 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:1759590 (1.7 MB)  TX bytes:1759590 (1.7 MB)
    
    tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
              inet addr:[removed]  P-t-P:[removed]  Mask:255.255.255.255
              inet6 addr: fe80::[removed] Scope:Link
              UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
              RX packets:76270 errors:0 dropped:0 overruns:0 frame:0
              TX packets:62204 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:100 
              RX bytes:72710602 (72.7 MB)  TX bytes:9456524 (9.4 MB)
    
    wlp2s0    Link encap:Ethernet  HWaddr [removed]  
              inet addr:192.168.1.106  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::88c0:3b8a:a25d:dcd5/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:508449 errors:0 dropped:1 overruns:0 frame:0
              TX packets:321820 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:532525406 (532.5 MB)  TX bytes:55885133 (55.8 MB)
    

4.VirtualBox -> 设置 -> 网络显示:

问题:如何配置 Vagrant/VirtualBox 以将 IP/port 暴露给主机 OS?

这是由于 a bug in VirtualBox 5.1.24 and has been fixed in 5.1.26. You can take a look at my 提供的更多详细信息。归根结底,VirtualBox 5.1.24 无法在内核路由 table 中为 vagrantfile 中定义的专用网络创建路由。

升级到 VirtualBox 5.1.26 应该可以解决您的问题。