virtual-machine - Vagrant:无法从主机 ping 客户机

标签 virtual-machine vagrant virtualbox

我有安装了 vagrant 的 Mac OS。在客户机上我有 Ubuntu 12。所以,我想做的是从主机 ping 客户机。

附加到 NAT 的客户机(根据 VirtualBox 设置)

我在 guest 机器上发现只有一个接口(interface)(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 接口(interface),但它们都没有任何 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

最佳答案

默认情况下(即不接触任何网络配置时),Vagrant 将 VirtualBox 中的虚拟机配置为将其第一个接口(interface)(eth0)附加到“ NAT ”(不要将其与“ NAT Network ”混淆,后者是 VirtualBox 中的不同选项)。

当您的 VM 接口(interface)连接到 NAT 时, guest 将获得 10.0.2.15 IP。在主机端,您只会看到没有 IP 的 vboxnet# 接口(interface)。您可以将此接口(interface)视为该访客的专用路由器。

连接到 NAT 的 VM 无法从外部世界路由。这是设计使然,这也是 vboxnet# 接口(interface)没有 IP 的原因之一。虚拟机可以访问“外部世界”,例如互联网和主机,但如果没有端口转发,则无法从外部世界访问。

如果默认行为不是您想要的,Vagrant 提供网络配置的高级抽象:

  • 专用网络 - 这将创建不应与主机的子网(或主机可能具有路由到的任何其他子网)冲突的新子网。在 VirtualBox 中,这将通过将附加接口(interface)附加到“ Host-Only ”来配置。
  • 公共(public)网络 - 您的虚拟机将从与您的主机相同的子网获取 IP。基本上,这就像在与您的主机相同的网络上创建新机器。在 VirtualBox 中,这将通过将附加接口(interface)附加到“ 桥接适配器 ”来配置。

  • 通过 provider specific configuration 配置网络,可以实现更高级的网络配置。

    每个配置都有自己的优点和缺点,这实际上取决于您计划实现的目标。
    有关其他信息,请检查 VirtualBox's docs about networking

    关于virtual-machine - Vagrant:无法从主机 ping 客户机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25433488/

    相关文章:

    qt - 为什么 Qt Creator 3.0.0 欢迎模式在 VM 中不起作用?

    linux - 在装有 64 位英特尔第三代酷睿 i5 处理器的 32 位 Windows 7 上安装 64 位 CentOS

    Vagrant 共享抛出 "No space left on device"错误

    networking - Vagrant::Errors::NetworkCollision:指定的主机网络与非主机网络冲突

    python - 将 Vagrant 和 VM 与 python-django 和 PostgreSQL 结合使用

    linux - 如何将文件放到我的 vagrant ubuntu 虚拟盒子上?

    VirtualBox Ubuntu 11.10 段错误上的 OpenGL GLUT

    python - 无法访问Azure计算实例中的Flask应用程序

    azure - 为什么在 Azure 云服务上重新配置远程设置时代码会恢复?

    licensing - 如何保护应用程序免受虚拟机的复制