How can I define network settings with vagrant

2019-03-21 18:14发布

问题:

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

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

Any clue what I do wrong ?

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

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

回答1:

From the vagrant book

NAT Requirement As the First Network Interface With VirtualBox,

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.

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.

so what you're reporting is this NAT.

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

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; }'`"


回答2:

To change the default NAT you should set it in Vagrantfile.

For instance:

 config.vm.define "bs" do |bvtserver|
  bvtserver.vm.hostname = "bvt-server"
  bvtserver.vm.network "private_network", ip: "192.168.50.3",
      virtualbox__intnet: "gcptest-network"
  bvtserver.vm.provider :virtualbox do |vbox|
    vbox.customize ["modifyvm", :id, "--natnet1", "10.3/16"]
  end
end

For credits and more informations: Li Chao blog



回答3:

I can bet you're looking at eth0 (that's another interface). Look at eth1 and you'll get what you're searching for. Moreover, from outside vagrant, try to ping it with

ping 192.168.99.4


标签: vagrant