Preferred fallback network interfaces with Vagrant? - vagrant

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.

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

Related

How do I create a isolated network on Vagrant?

I have started learning Vagrant.
What I want to do is create a private network with 2 guests :
ip private network: 192.168.3.0
ip guest #1 (centos8): 192.168.3.1
ip guest #2 (ubuntu20.o4): 192.168.3.2
So:
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.3.1"
end
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.3.2"
end
I thought that setting a network as "Private" no other guests on Vagrant were able to do ping to them or do ssh... Because I can do it.
What Must I do? to isolate that network from others guests.
The only solution I know is specific to the VirtualBox provider, using VirtualBox's internal network feature:
Internal Networking is similar to bridged networking in that the VM can directly communicate with the outside world. However, the outside world is limited to other VMs on the same host which connect to the same internal network.
https://www.virtualbox.org/manual/ch06.html#network_internal
In vagrant you would specify the internal network like this:
config.vm.network "private_network", ip: "192.168.100.4",
virtualbox__intnet: "isolatednet1"
Where isolatednet1 can be any name you want for the internal network. All VirtualBox VMs using the isolatednet1 network will be able to communicate with each other, but they won't be able to communicate with VMs outside the internal network.
Note that instead of a network name you can use a boolean value of true for virtualbox__intnet but in that case Vagrant will assign all VMs to the network "intnet". So if you want to achieve isolation you need to assign a unique internal network name for each group of VMs you want to isolate.

VirtualBox networking using vagrant

I need to configure a vm into using vagrant and virtualbox as provider. The issue is related to how to allow network comunication between the virtual machine and the host machine.
Concretly, I need:
Each developer is using a log viewer in his host machine in order to be able see the log messages generated into vm applications. So I need to send log messages to an network address(host) from guest.
Each developer machine has its own ip address, so I'm not able to reference to a single ip since each developer has its host ip address.
Any ideas?
This is how I've set up my VMs lately, hopefully it points you in the right direction.
My requirements were:
enable internet access on a single guest basis
enable access guest to guest
enable access host to guest
avoid IP changes when changing physical network (lan home, lan work, random wlans)
avoid IP clash with various clients VPNs
avoid showing machines on the physical network
To do this use 2 network interfaces on all guests.
Default NAT, enables internet access;
HostOnly network, enables communication host-to-guest and guest-to-guest.
Steps:
Create hostonly network with DHCP (Virtualbox comes with a default one, I customized it)
hostonly network setup
hostonly network DHCP
add network adapters to the guest
NAT(default)
hostonly-network
Start the VM, check with ip a and ip r:
ip addr
ip route (has docker installed too, ignore that)
Create Vagrantfile to provision VM configured like above (I'm using bento/ubuntu-20.04).
NAT is on by default
join the hostonly-network with "modifyvm" (avoid :private_network as it creates a new network that will clash with the one already available), add to Vagrantfile:
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--nic2", "hostonly"]
vb.customize ["modifyvm", :id, "--hostonlyadapter2", "vboxnet1"] #use proper network name here
vb.customize ["modifyvm", :id, "--cableconnected2", "on"]
end
vagrant up
vagrant ssh
ip a will show eth1 as down
try sudo ifup eth1, in my case this failed
edit /etc/network/interfaces and add this:
allow-hotplug eth1
iface eth1 inet dhcp
sudo ifup eth1 should work now and get an ip from the DHCP of the host-only network
Probably these last steps could be added to vagrant shell provisioning but I'm still new to it.
Edit: add this section to Vagrantfile for the steps above:
config.vm.provision "shell", inline: <<-SHELL
if ! ifquery eth1 > /dev/null 2>&1; then
sudo echo "allow-hotplug eth1" >> /etc/network/interfaces
sudo echo "iface eth1 inet dhcp" >> /etc/network/interfaces
sudo ifup eth1
ip -4 a show dev eth1
fi
SHELL
At this point the vagrant vm should be accessible, able to access the web, able to access the hostonly network (other guests and the host through the configured ip, 192.168.178.1 in my case), these should all work:
ping 8.8.8.8 #web
ping 192.168.178.1 #host
ping 192.168.178.3 #other guest
Final result
Hopefully this enables all relevant communication for your use case also.

How to automatically select bridged network interfaces in Vagrant?

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!
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
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
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",
]
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.

vagrant homestead - public network not working on osx

I've been trying all day to get a public network setup using Vagrant 1.6.5 with VirtualBox. I really need to be able to access all my Homestead sites on my other devices (iPad, iPhone, Android phone, etc.) on my local network. The box provisions fine but the sites all timeout.
homestead.rb
config.vm.network "public_network", :bridge => "en0: Wi-Fi (AirPort)", :ip => "192.168.1.200"
My host is OS X mavericks using DHCP (192.168.1.137).
Private networking works fine, but I can't get public networking to work with any variation of the above config.
You can just grab the host's internal IP address (NOT the VM ip) and add the port :8000. Let's say your host ip is 192.168.0.5, then from any other device on your network, you can just browse to the host at 192.168.0.5:8000 and you'll hit the VM and see the Laravel home page.
This works great for me with the default homestead.rb setting:
# Configure A Private Network IP
config.vm.network :private_network, ip: settings["ip"] ||= "192.168.10.10"
To get my homestead machine to be visible from my LAN I did the following:
Edit the Homestead/scripts/homestead.rb file and set config.vm.network :public_network, ip: "192.168.1.123" (just make sure the IP is free).
Do a vagrant reload
Make sure my old local hosts file now points to the new IP.
The result is I can ping 192.168.1.123 from another machine in my LAN.
What is the error that you are getting? Make sure that 192.168.1.200 is not being used by any other device.
Network configuration for vagrant boxes
This row works fine for me:
config.vm.network :public_network, ip: settings["ip"] ||= "192.168.10.10", bridge: "eth0"
Then it uses the ip variable from ~/.homstead/Homestead.yaml and if thats not set it uses 192.168.10.10

Vagrant keeps creating unwanted network interfaces

I'm creating a VM definition and I want to specify a single "private_network" on the primary interface of the box (eth0, it's Linux)
Despite having only one config.vm.network statement, I keep getting the desired network set on eth1, while eth0 is assigned an ip from a 10.0.2.0/24 subnet which I have never defined.
How can I prevent this and have my desired 192.168.x.y/24 set on eth0?
Best,
Edoardo
eth0 is by default used by Vagrant for NAT (VirtualBox NAT networking mode), which allow you to vagrant ssh into the box (port forwarding rules - host 2222 <=> guest 22).
That's why you have 2 NICs even though you have only 1 config.vm.network in Vagrantfile.
I don't think it can be disabled, BUT I am not 100% sure, you may want to look into vagrant source code to dig further.
https://superuser.com/questions/957631/how-to-force-vagrant-to-have-a-single-bridged-network-interface
You can try to add the adapter: 1 to the config. Although I still find some problem with it. See if it will help you starts from somewhere.
Example:
config.vm.network "public_network", bridge: "Broadcom BCM5709C", adapter: "1", ip: "192.168.x.xx"
You will still have the eth1 which is host-only network...

Resources