How can I define network settings with vagrant

前端 未结 3 708
没有蜡笔的小新
没有蜡笔的小新 2021-02-10 09:02

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_V         


        
相关标签:
3条回答
  • 2021-02-10 09:40

    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; }'`"
    
    0 讨论(0)
  • 2021-02-10 09:44

    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
    
    0 讨论(0)
  • 2021-02-10 10:00

    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

    0 讨论(0)
提交回复
热议问题