Setting a VM's mac address in Vagrant

2019-03-09 21:06发布

问题:

The documentation lists that the mac address of a VM can be set in the Vagrantfile, however everything I add seems to end up being a syntax error. Anyone successfully done this?

回答1:

I used this:

config.vm.network :bridged , :mac => "080027XXXXXX"

and got what I wanted.

The docs are unclear on what the syntax for the options hash were, and there seemed to be no example on what this should look like. So, here it is! Bridged with a mac address (edited of course). This brings up eth1 with the mac specified, which makes my DHCP server happy, and gives it a proper fqdn on my network.



回答2:

This is an old question, but I had the same issue just now. Vagrant documentation v2 still seems incomplete. In the end I used this line in the Vagrantfile with vagrant 1.2.7:

config.vm.network "public_network", :bridge => 'enp4s0', :mac => "5CA1AB1E0001"

This:

  • sets the host interface named 'enp4s0' as the bridge interface,
  • which as 'eth0' on the guest is then assigned an ip address by the same DHCP the host uses
  • Also sets 5C:A1:AB:1E:00:01 as the guest's mac address


回答3:

hmm, the network config didn't help in my case. After defining the MAC Address directly in the Vagrantfile via config.vm.base_mac = "MyEth0MacAddressWithoutSlashes" my machine started =)



回答4:

On Vagrant version 2.0.1, I write in the Vagrantfile for a private_network (provider = VirtualBox ; version 5.2.0) :

config.vm.network "private_network", ip: "X.X.X.X", mac: "080027xxxxxx"


回答5:

The information provided below is outdated. As per documentation to allow IP to be assigned via DHCP simply use:

config.vm.network "public_network"

This way you don't need to deal with mac address, it will be generated on its own. If you need custom mac address attached to the network device then:

config.vm.network "public_network", :mac=> "080027xxxxxx"