如何使用 vagrant 定义网络设置 [英] How can I define network settings with vagrant

查看:27
本文介绍了如何使用 vagrant 定义网络设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 vagrant 中运行 Ubuntu,这是 Vagrantfile:

I am running Ubuntu inside vagrant, here is the Vagrantfile:

# 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|
  config.vm.box = "ubuntu/trusty64"
  config.vm.network :private_network, ip: "192.168.99.4", :netmask => "255.255.255.0", auto_config: false
end

所以我希望有 192.168.99.4 作为 IP,但我总是有:

So I expect to have 192.168.99.4 as IP but I always have:

eth0      Link encap:Ethernet  HWaddr 08:00:27:88:ba:8f  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe88:ba8f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth1      Link encap:Ethernet  HWaddr 08:00:27:9c:a9:9f 
          inet6 addr: fe80::a00:27ff:fe9c:a99f/64 Scope:Link 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

知道我做错了什么吗?

在这个问题上取得了一些进展,几乎是这样的:如何在Vagrant下切换网络适配器的顺序虚拟盒子?

Got some progress on the question, and it is almost like this one : How to switch order of network adapters in Vagrant under VirtualBox?

这是关于不使用10.0.2.15",据我了解它是在virutalbox的NAT"设置中进行更改,无法由Vagrant管理

It is about not using "10.0.2.15" and from what I understand it is to be changed in the "NAT" setup of virutalbox, can not be managed by Vagrant

推荐答案

来自 流浪书

作为 VirtualBox 的第一个网络接口的 NAT 要求,

Vagrant 需要第一个连接到虚拟机的网络设备机器成为 NAT 设备.NAT 设备用于端口转发,这就是 Vagrant 如何通过 SSH 访问虚拟机器.

Vagrant requires the first network device attached to the virtual machine to be a NAT device. The NAT device is used for port forwarding, which is how Vagrant gets SSH access to the virtual machine.

因此,任何仅主机或桥接网络都将添加为额外的网络设备并暴露给虚拟机作为eth1"、eth2"等.eth0"或en0"通常总是NAT设备.

Therefore, any host-only or bridged networks will be added as additional network devices and exposed to the virtual machine as "eth1," "eth2," and so on. "eth0" or "en0" is generally always the NAT device.

目前无法覆盖此要求,但是重要的是要了解它已经到位.

It isn’t currently possible to override this requirement, but it is important to understand that it is in place.

所以你报告的是这个 NAT.

so what you're reporting is this NAT.

当您在 Vagrantfile 中设置 auto_config: false 时,也未定义 eth1,因此您基本上是在告诉 vagrant 您将自己完成所有设置.所以你应该把这个参数设置为真(或删除)或自己设置 etc/network/interfaces

Also eth1 is not defined as you set auto_config: false in your Vagrantfile so you're basically telling vagrant you'll do all the setup yourself. so you should turn this paramter to true (or remove) or set the etc/network/interfaces yourself

您可以查看vagrant public network 看看是否可以使用从您的配置中删除 eth0 网关的代码段示例

You can look at vagrant public network and look if you can use the snippet example to remove the eth0 gateway from your config

  config.vm.network "public_network", ip: "192.168.0.17"

  # default router
  config.vm.provision "shell",
    run: "always",
    inline: "route add default gw 192.168.0.1"

  # default router ipv6
  config.vm.provision "shell",
    run: "always",
    inline: "route -A inet6 add default gw fc00::1 eth1"

  # delete default gw on eth0
  config.vm.provision "shell",
    run: "always",
    inline: "eval `route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"

这篇关于如何使用 vagrant 定义网络设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆