How to automatically select bridged network interfaces in Vagrant?

前端 未结 4 645
说谎
说谎 2021-02-01 15:39

What should I add in Vagrant file to prevent asking (after vagrant up command)

Which interface should the network bridge to?

4条回答
  •  抹茶落季
    2021-02-01 16:27

    For Linux, I use the fact that this file is a Ruby script to detect the default route and use the name of that interface. This allows our whole team to use the Vagrantfile as is, without having to hard code interface names or constantly update a list. I would imagine that you could do something similar on Windows or Mac (guessing Mac might actually be the same, since it is *nix).

    As a note, if no default network interface is detected, you will still be prompted to select one.

    # Grab the name of the default interface
    $default_network_interface = `ip route | awk '/^default/ {printf "%s", $5; exit 0}'`
    ...
    Vagrant.configure("2") do |config|
        # Specify the interface when creating the public network
        config.vm.network "public_network", bridge: "#$default_network_interface"
        ...
    end
    

提交回复
热议问题