How to automatically select bridged network interfaces in Vagrant?
Asked Answered
H

4

47

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

Which interface should the network bridge to?

Available bridged network interfaces:
1) Intel(R) 82579LM Gigabit Network Connection
2) VMware Virtual Ethernet Adapter for VMnet1
3) VMware Virtual Ethernet Adapter for VMnet8

I want to select the #1 option. Current I need to enter "1" manually. Please help!

Hadhramaut answered 21/10, 2015 at 3:13 Comment(2)
superuser.com/a/865947/74576 looks helpful.Ducat
Official Documentation reference: https://www.vagrantup.com/docs/networking/public_network#default-network-interfaceTakara
J
56

in your Vagrantfile, you should add

config.vm.network "public_network", bridge: "Intel(R) 82579LM Gigabit Network Connection"

Then it should make vagrant happy (actually its more VirtualBox that is getting happy in this case) and select the correct network adapter for the VM

Jog answered 21/10, 2015 at 7:20 Comment(1)
Works for me too. Just make sure you're passing the name of your network interface according to what your host OS exposes to VBox.Foulmouthed
E
20

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
Epiphany answered 21/11, 2019 at 13:42 Comment(2)
I find this solution more robust to change into hardware, eg changing network interfacesAsterism
Thank you for your solution. I used it in my tutorial and credited you.Harpist
U
10

You can actually provide a list of bridges and Virtualbox will cycle through them until it finds one that it can use. Put these in order of how you want them to be tried:

For example on my OSX Macbook:

config.vm.network "public_network", bridge: [
  "en0: Wi-Fi (AirPort)",    
  "en1: Wi-Fi (AirPort)",
]

So in your case:

config.vm.network "public_network", bridge: [
  "Intel(R) 82579LM Gigabit Network Connection",
  "VMware Virtual Ethernet Adapter for VMnet1",
  "VMware Virtual Ethernet Adapter for VMnet8",
]
Upstretched answered 13/7, 2018 at 19:36 Comment(0)
E
1

I simply just took out the config.vm.network :public_network from the vagrant config file.

And vagrant booted up without asking me that weird question about bridge interface to connect to the internet.

Endoskeleton answered 25/6, 2020 at 22:32 Comment(1)
but won't it get NAT instead of the bridge?Presumption

© 2022 - 2024 — McMap. All rights reserved.