The primary network interface of a Vagrant (VMware) machine typically obtains a dynamic IP address from the vmnet-dhcpd
process bound to the vmnet8
network. In a multi-machine
Vagrant environment, it is preferable to reserve a static IP address for this interface to simplify connectivity with the Mac host for automation purposes.
Prerequisites
Steps
-
Quit the VMware Fusion application.
-
Open your favorite terminal emulator.
-
Generate a unique MAC address for the Vagrant machine primary network interface.
printf '00:0C:29:%02X:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256))
output:
00:0C:29:26:C5:EF
- Identify an available IP address from the
vmnet8
virtual network.
grep -A16 subnet /Library/Preferences/VMware\ Fusion/vmnet8/dhcpd.conf
output:
subnet 172.16.239.0 netmask 255.255.255.0 {
range 172.16.239.128 172.16.239.254;
option broadcast-address 172.16.239.255;
option domain-name-servers 172.16.239.2;
option domain-name localdomain;
default-lease-time 1800; # default is 30 minutes
max-lease-time 7200; # default is 2 hours
option netbios-name-servers 172.16.239.2;
option routers 172.16.239.2;
}
host vmnet8 {
hardware ethernet 00:50:56:C0:00:08;
fixed-address 172.16.239.1;
option domain-name-servers 0.0.0.0;
option domain-name "";
option routers 0.0.0.0;
}
- The
range
declaration has defined128
to254
for dynamic allocation. - Name servers and gateway use
2
. - The VMnet adapter uses
1
.
This leaves 3
to 127
for use within the /24
network. I select the 172.16.239.101
IP address for my example.
- Add the
base_mac
andbase_address
attributes to the machine definition in theVagrantfile
.
|
|
- Start the Vagrant machine.
vagrant up
- Verify the
host
declaration has been added to thedhcpd.conf
file.
grep -A7 'host 172.16.239.101' /Library/Preferences/VMware\ Fusion/vmnet8/dhcpd.conf
output:
host 172.16.239.101 {
hardware ethernet 00:0C:29:26:C5:EF;
fixed-address 172.16.239.101;
option domain-name-servers 172.16.239.2;
option domain-name localdomain;
option netbios-name-servers 172.16.239.2;
option routers 172.16.239.2;
}
- Optionally, verify inside the Vagrant machine.
vagrant ssh
Get the MAC and IP address for the eth0
network interface.
for n in link addr; do ip -br ${n} sh eth0 | awk '{ print $3 }'; done
output:
00:0c:29:26:c5:ef
172.16.239.101/24