My Vagrant boxes use public networking so they can advertise themselves over zeroconf/Bonjour. The Vagrantfile
explicitly sets the bridged network interface:
config.vm.network :public_network, :bridge => 'en2: USB Ethernet'
Most of the time everything just works, but if I'm connected via a different network and the specified interface doesn't exist, vagrant up
will prompt me to pick from the available network interfaces:
[default] Specific bridge 'en2: USB Ethernet' not found. You may be asked to specify
which network to bridge to.
[default] Available bridged network interfaces:
1) en0: Wi-Fi (AirPort)
2) p2p0
What interface should the network bridge to?
Is there a way to tell Vagrant to choose from a list of preferred network interfaces? What I want is a graceful fallback if the primary network isn't available.
Default Network Interface If more than one network interface is available on the host machine, Vagrant will ask you to choose which interface the virtual machine should bridge to. A default interface can be specified by adding a :bridge clause to the network definition.
Configure port forwarding This allows you to access a port on your own machine, but actually have all the network traffic forwarded to a specific port on the guest machine. To set up a forwarded port so you can access Apache on your guest, add the config. vm.network parameter to your Vagrantfile.
To configure private or host-only networking in Vagrant with static IP address, open the Vagrantfile, find the following line and uncomment it. Here, 192.168. 121.60 is the IP address of the VM. Replace this with your own IP.
Here's a solution I came up with that seems to be working well so far:
In Vagrantfile
, add the following to the top of the file:
pref_interface = ['en2: USB Ethernet', 'en0: Wi-Fi (AirPort)']
vm_interfaces = %x( VBoxManage list bridgedifs | grep ^Name ).gsub(/Name:\s+/, '').split("\n")
pref_interface = pref_interface.map {|n| n if vm_interfaces.include?(n)}.compact
$network_interface = pref_interface[0]
Then, inside Vagrant.configure
, use $network_interface
to specify the bridge:
config.vm.network :public_network, :bridge => $network_interface
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With