【问题标题】:Vagrant: can not ping guest machine from the hostVagrant:无法从主机 ping 客户机
【发布时间】:2014-10-15 12:13:17
【问题描述】:

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

连接到 NAT 的客户机(根据 VirtualBox 设置)

我发现guest机器上只有一个接口(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 阵风机器?

这是我的流浪文件(我删除了一些我不使用的注释行):

# -*- 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 或端口转发的私有网络。docs.vagrantup.com/v2/networking/index.html
  • @GianArb 我添加了我的 Vagrantfile。为了快速修复,我取消了 config.vm.network "public_network" 行的注释,并且在我能够 ping 虚拟机之后(我需要它来测试我的应用程序)那么,你建议设置专用网络吗?
  • 可以在此处找到具有更多有用答案的类似问题:stackoverflow.com/questions/23497855/…
  • 我最近遇到了类似的问题。主机重启为我解决了这个问题。

标签: virtual-machine vagrant virtualbox


【解决方案1】:

默认情况下(即在不涉及任何网络配置时),Vagrant 会在 VirtualBox 中配置虚拟机以将其第一个接口(eth0)附加到“NAT”(不要将其与“ NAT 网络”,这是 VirtualBox 中的不同选项)。

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

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

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

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

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

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

【讨论】:

  • 如果它不能以任何方式解决问题,为什么它应该是公认的答案?
【解决方案2】:

我不确定为什么您会在来宾上看到 IP 地址,但鉴于该 vagrant 文件,您根本没有配置任何 virtualbox 网络。您需要取消注释 config.vm.network "private network" 行,然后配置 IP 地址。

virtualbox 的默认设置是将主机放在 10.0.0.2 并将整个 10.0.0.0/8 范围公开为本地地址,因此我认为 10.xxx 范围内的任何内容都适用于客户端虚拟机。

【讨论】:

    【解决方案3】:

    我的情况完全一样,这里有一个对我有用的解决方案。

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

    vagrant halt
    

    然后去你的 vagrant box,打开 VagrantFile。 在其中取消注释(或手动添加)以下行

     config.vm.network "public_network"
    

    这将确保下次您“流浪”您的虚拟机时,它将确保您的虚拟机将从与您的主机相同的子网获得 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/访问您的主机,请确保您的防火墙已关闭(系统>首选项>安全)。不过,防火墙不会影响从主机到虚拟机的 ping。

    【讨论】:

    • @Datadimension 请尽量避免无用的 cmets。如果您发现答案没有帮助或不正确,请投反对票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    相关资源
    最近更新 更多