Vagrant Multiple VM's with Unique IP address on public_network using DHCP

host machine: Ubunutu Server 20.04

Tools:
Vagrant

box/image being used
custom ubuntu server image.

Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :


$veh= 1
$vehCount = 2

$vehList ={}


while $veh<= $vehCount do
  $vehList["veh_#{$veh}"] = "525400" + Array.new(6){[*"A".."F", *"0".."9"].sample}.join
  $veh+=1
end

Vagrant.configure("2") do |config|

  $vehList.each do |vehName, macADDR|

    $vname = vehName
    $mac = macADDR

    config.vm.define vehName do |v|

      v.vm.box = "vehserver"
     
      v.vm.network "public_network", :mac => macADDR 
    end
  end
end

Goal: What I want to be able to do is launch multiple VM’s that will each have a unique IP address that will allow them to communicate with each other, and allow other physical machines on the LAN to have access to them as well.

What’s actually happening: Instead of each VM having its own IP address, all VM’s are created with the same IP address.

What I’ve tried:

  • Pulled the VM creation out of the loop. I thought it might have to do with the way the loop was working. But if I separate them out, still the same issue
  • perhaps it was that they were using the same Vagrantfile. So I created a second directory and ran all the same settings on both Vagrantfiles and again. All VM’s had the same IP address.
  • I’m running a custom Box image, so I thought it was the image. So I pulled ubuntu/trusty64 down from the vagrant repo, and ran tested it against a single or multiple file build. and again all IP addresses were the same
  • the first solution I found had to do with making sure the MAC addresses were different. I was able to do that with no issues. As you can see from the above Vagrantfile, I’m generating a unique MAC for each VM. However, this changed nothing.
  • make sure VB is using a bridged adapter and not a host-only adapter. I’ve verified that the adapter I’m using, “eno1” is in fact a bridged adapter. I used vboxmanage list bridgedifs to list all bridge adapters.
  • manage default router. so at the bottom of the page on this link they talk about utilizing the DHCP on a public_nework using DHCP on the public network. the command appears to be deleting the default gateway. but I’m not sure exactly what its doing. In either case I attempted the instructions, and nothing came of it.
    Both VM’s sees the default gateway for the LAN.

What does work, but doesn’t solve my issue.
the one thing that works but does not solve my issue is if I use two different box images. For instance ubuntu/trusty64 and my custom image, on two different file will create two different VM’s with different IP addresses. While it works, it doesn’t do me any good as I need the preconfigured custom box for my project.

How do I configure Vagrant to ask the DHCP server for unique IP addresses for both VM’s?

While I’m not sure if there is a more appropriate way to handle this, I have found a solution that will work for my needs.

So during the provisioning step of Vagrant, I’ve updated the shell script I’m using for configuring the VM to contact the DHCP server to acquire a new address.

I first release the current IP address with the use of dhclient. After releasing the current IP, I requirest a new IP for the Bridge adapter.

INTERFACE="$(ls --ignore=‘lo’ /sys/class/net)"

INTERARRAY=($INTERFACE)

dhclient -r ${INTERARRAY[-1=]}

dhclient ${INTERARRAY[-1]}

This ultimately allows me to communicate across all VM’s and are able to manage them as individual entities.

I would be very interested to hear how others would manage this issue.

Michael