How to set eth0 MAC address in Vagrant with the VM

2019-09-04 05:50发布

问题:

Vagrant relies on VMware (Workstation and Fusion) to generate the MAC address of eth0 (the first and default ethernet interface) on a guest being deployed from a box.

I would like to fix this MAC address so it is static and not regenerated each time the VM is recreated so that the VMware DHCP service can assign it the same IP address each time.

回答1:

First make sure that the VMware DHCP service will assign an IP address to a specified MAC address by editing vmnetdhcp.conf which has different locations depending on OS. Place an entry at the end of the file replacing the hostname, MAC and IP with desired values:

host hostname {
    hardware ethernet 00:0A:AA:AA:AA:AA;
    fixed-address 192.168.1.1;
}

Restart the VMware DHCP service to load these changes.

In your Vagrantfile use the following settings to configure the default network interface:

Vagrant.configure("2") do |config|
    config.vm.define 'hostname' do |hostname|
        hotname.vm.box = box
        hostname.ssh.host = '192.168.1.1'
        puppet.vm.provider :vmware_fusion do |v, override|
            v.vmx['ethernet0.addressType'] = 'static'
            v.vmx['ethernet0.address'] = '00:0A:AA:AA:AA:AA'
       end
       puppet.vm.provider :vmware_workstation do |v, override|
            v.vmx['ethernet0.addressType'] = 'static'
            v.vmx['ethernet0.address'] = '00:0A:AA:AA:AA:AA'
        end
    end
end

Next time the virtual machine is brought up with vagrant up it will assigned the static MAC and recieve the same IP address from DHCP.

Other vmx keys could also be manipulated for ethernet0 using this method.