【问题标题】:Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host [duplicate]无法从 Ubuntu 主机访问 Vagrant 配置的 VirtualBox VM [重复]
【发布时间】:2017-07-26 10:42:13
【问题描述】:

我正在关注basic Vagrant tutorial,Vagrant 在我的 Ubuntu 上提供了一个盒子,没有抱怨。此外,我可以vagrant ssh 进入虚拟机。但是,无法从主机访问 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 有效。
  • 我已经在来宾VM上手动安装了lighttpd,我可以在来宾VM中成功ping/curl地址:localhost

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

~/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 链接封装:以太网 HWaddr [已删除]
    inet 地址:172.18.0.1 广播:0.0.0.0 掩码:255.255.0.0 上行广播多播 MTU:1500 公制:1 RX 数据包:0 错误:0 丢弃:0 超限:0 帧:0 TX 数据包:0 错误:0 丢弃:0 超限:0 运营商:0 碰撞:0 发送队列: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)
    

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

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

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

【问题讨论】:

  • 您是否提供了一些可能是pingable 的服务?你可以在vm上安装例如lighttpd并在浏览器中打开http://172.22.22.22吗?
  • 感谢@RandallFlagg。我之前在VM上安装过Jenkins,这次我只是安装了lighttpd,我可以从guest VM成功curl localhost,但是来自主机的curl 172.22.22.22失败curl: (7) Failed to connect to 172.22.22.22 port 80: Network is unreachable

标签: ubuntu networking vagrant virtual-machine virtualbox


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-07-23
    • 2016-07-03
    • 2021-04-12
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多