Vagrant:无法从主机ping通客户机

19
我有一台安装了Vagrant的Mac OS。在虚拟机上,我安装了Ubuntu 12。因此,我想做的是从主机ping虚拟机。
虚拟机连接了NAT(根据VirtualBox设置)
我只在虚拟机上找到了一个接口(除了lo)。
eth0      Link encap:Ethernet  HWaddr 08:00:27:88:0c:a6  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe88:ca6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:581 errors:0 dropped:0 overruns:0 frame:0
          TX packets:410 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:61376 (61.3 KB)  TX bytes:51599 (51.5 KB)

事实是主机上的10.0.2.*网络中没有IP地址。主机有几个vboxnet接口,但它们都没有任何IP地址。
vboxnet0: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
          ether 0a:00:27:00:00:00 
vboxnet1: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
          ether 0a:00:27:00:00:01 
vboxnet2: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
          ether 0a:00:27:00:00:02 
vboxnet3: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
          ether 0a:00:27:00:00:03 

您有没有想过为什么VirtualBox没有将IP地址分配给主机?我该怎么做才能ping通虚拟机?

这是我的vagrant文件(我删除了一些不需要的注释行):

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

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.

# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "hashicorp/precise64"

# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
# config.vm.box_url = "http://domain.com/path/to/above.box"

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 3306, host: 8086
config.vm.network "forwarded_port", guest: 27017, host: 27017

config.vm.synced_folder "/Users/KoulSlou/Documents/Cloudware12.10", "/vagrant", owner:     "www-data", group: "www-data"
config.vm.synced_folder "/Users/KoulSlou/Documents/Cloudware/public","/cloudware", owner: "www-data", group: "www-data"

# Create a private network, which allows host-only access to the machine
# using a specific IP.
#config.vm.network "private_network", ip: "192.168.1.2"

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"

# If true, then any SSH connections made will enable agent forwarding.
# Default value: false
# config.ssh.forward_agent = true
...

end

你能分享一下你的Vagrantfile吗?通过良好的网络配置,你可以选择一个带有静态IP或端口转发的私有网络。http://docs.vagrantup.com/v2/networking/index.html - GianArb
@GianArb,我添加了我的Vagrantfile。为了快速修复,我取消注释了配置行“config.vm.network”public_network”,之后我就能够ping虚拟机(我需要它来测试我的应用程序)。那么,你建议设置私人网络吗? - Tamara
类似的问题和更有帮助的答案可以在这里找到:https://dev59.com/X2Ag5IYBdhLWcg3whrQV - efeder
我最近也遇到了类似的问题。重新启动主机解决了这个问题。 - Matthijs P
3个回答

30

默认情况下(即没有触及任何网络配置),Vagrant 在 VirtualBox 中配置虚拟机将其第一个接口(eth0)连接到 "NAT"(不要与 VirtualBox 中的 "NAT Network" 混淆,后者是不同的选项)。

当您的 VM 接口连接到 NAT 时,它将获得 10.0.2.15 IP。在主机端,您只会看到 vboxnet# 接口而无 IP。您可以把这个接口看作是该客户机的私有路由器。

连接到 NAT 的 VM 无法从外部世界进行路由。这是设计上的限制,这也是为什么 vboxnet# 接口没有 IP 的原因之一。VM 可以访问“外部世界”,如互联网和主机机器,但不能在没有端口转发的情况下从外部世界访问。

如果默认行为不是您所需的,请使用 Vagrant 提供的高级网络配置:

  1. 私有网络 - 这将创建新的子网,不应与主机子网(或主机可能有路由到的任何其他子网)冲突。在 VirtualBox 中,这将通过将附加接口连接到“Host-Only”来配置。
  2. 公共网络 - 您的 VM 将从与主机相同的子网中获取 IP。基本上,这就像在与主机相同的网络上创建新计算机。在 VirtualBox 中,这将通过将附加接口连接到“Bridged Adapter”来配置。

通过使用 提供商特定的配置 来配置网络,还可以实现更高级的网络配置。

每个配置都有其优缺点,这取决于您计划实现什么。有关更多信息,请查看 VirtualBox 关于网络的文档


5
如果这个答案没有以任何方式解决问题,为什么它应该被接受呢? - Computer's Guy

-1

我曾经遇到过同样的情况,这里是对我有效的解决方案。

如果您的虚拟机已经启动,请使用以下命令停止它:

vagrant halt

然后进入您的vagrant box,打开VagrantFile。 取消注释(或手动添加)其中的以下行。
 config.vm.network "public_network"

这将确保下一次你执行 "vagrant up" 命令启动虚拟机时,它会确保你的虚拟机从与主机相同的子网获取 IP(有关更多详细信息,请参见 @m1keil 的答案)。

然后再次启动你的虚拟机:

vagrant up

在我的情况下,它问我应该使用哪个接口来作为网络桥接,这里是我的一部分摘录:
==> default: Available bridged network interfaces:
1) en0: Ethernet
2) en1: Wi-Fi (AirPort)
==> default: When choosing an interface, it is usually the one that is
==> default: being used to connect to the internet.
    default: Which interface should the network bridge to? 1
==> default: Preparing network interfaces based on configuration...

请注意,它会告诉您可用的接口是什么。 输入您想要的接口对应的数字,例如我输入了1(代表en0:以太网)。

最后,连接到您的vagrant box。 例如,在我的情况下:

vagrant ssh

然后从主机上,如果您键入ifconfig,仍然无法看到虚拟机的IP地址,但是,如果您从虚拟机中键入ifconfig,它将提供一个可从主机ping /访问的IP地址。

注意:如果要从客户机ping /访问主机,请确保防火墙已关闭(System>Preferences>Security)。但是防火墙不影响从主机到虚拟机的ping。


@Datadimension请尽量避免无用的评论。如果您发现答案不够有帮助或者是错误的,请进行点踩。 - Zolnoor

-1
我不确定为什么您在客户机上看到IP地址,但是您在vagrant文件中没有配置任何virtualbox网络。 您需要取消注释config.vm.network "private network"行,然后配置IP地址。
virtualbox的默认设置是将主机放在10.0.0.2,并公开整个10.0.0.0/8范围作为本地地址,因此我认为10.x.x.x范围内的任何内容都适用于客户端vm。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接