无法从Ubuntu主机访问Vagrant配置的VirtualBox VM

时间:2017-07-26 10:42:14

标签: ubuntu networking vagrant virtual-machine virtualbox

我跟随basic Vagrant tutorial和Vagrant在我的Ubuntu上设置了一个没有抱怨的方框。而且,我可以vagrant ssh进入VM。但是,无法从主机访问VM:无法ping通其IP地址。

我取得的成就:

  • 已安装VirtualBox(5.1.24 r117012(Qt5.5.1))和Vagrant(1.9.7) 到我的Ubuntu(16.04.2 LTS)机器上。
  • 为VM配置了vagrant up,Vagrant完成了这项工作并且没有抱怨。
  • vagrant ssh有效。
  • 我在来宾虚拟机上手动安装了lighttpd,我可以在来宾虚拟机中成功ping/curl地址:localhost

这是我尝试从主机访问来宾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
来宾VM上的

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链接封装:以太网HWaddr [已删除]
                  inet addr:172.18.0.1 Bcast:0.0.0.0 Mask:255.255.0.0               UP BROADCAST MULTICAST MTU:1500公制:1               RX数据包:0错误:0丢弃:0超限:0帧:0               TX数据包:0错误:0丢弃:0溢出:0载波:0               碰撞:0 txqueuelen:0               RX字节:0(0.0 B)TX字节: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)
    
  2. 4. VirtualBox - >设置 - >网络显示:

    • 适配器1.附加到:NAT
    • 适配器2.附加到:仅限主机的适配器vboxnet0,已连接电缆 - 是。

    问题:如何配置Vagrant / VirtualBox以向主机操作系统公开IP /端口?

1 个答案:

答案 0 :(得分:2)

这是由a bug in VirtualBox 5.1.24引起的,并已在5.1.26中修复。您可以查看我的answer to a similar question了解更多详情。它归结为VirtualBox 5.1.24无法在您的vagrantfile中定义的专用网络的内核路由表中创建一个路径。

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