Skip to content

How to configure a static IP on the primary interface

Jonathan Senkerik edited this page Apr 29, 2018 · 1 revision

How to configure a static IP on the primary interface.

I've been getting so many requests & inquiries about this that I decided to write a little wiki page.

Method 1:

This works for all types of systems and would be the most supported way!

Set a static MAC address for the primary interface. Use the Vagrantfile option: esxi.guest_mac_address = ["00:50:56:XX:XX:XX"] Reserve a fixed IP address for that MAC address on your DHCP server. The VM will boot using DHCP, but will always have the same fixed IP. If your a bit paranoid and your vagrant VM's are not always up, you may want to set a very long reservation.

Method 2:

  1. THIS IS NOT SUPPORTED. Vagrant doesn't support you modifying the primary interface. Therefore, the provider plugins do not support modifying the primary interface!

  2. This is only an example to show how it can be done. nmcli doesn't exist on Ubuntu. I provide this example for Centos7 which uses nmcli. This should work for RHEL and Fedora. You mileage will vary.

  3. If you need additional support or you don't understand what I'm doing, see note 1 above.

This method uses a post provision script to update the network configuration, then you need to reboot the VM for the settings to take effect.

At the top of the Vagrantfile, you declare your static IP, cidr and gateway. The provision script first determines the primary interface, then it uses nmcli to create a new ifcfg-$INTERFACE file.

*** Basically, my biggest tip when doing this type of thing... Update the configuration files, then do "vagrant reload" to reboot. Vagrant will be much happier. ***

$ vi Vagrantfile
# Static settings
$mgmt_ip = '192.168.1.94'
$mgmt_cidr = '/24'
$mgmt_gw = '192.168.1.1'

#  Inline provisioning script
$script = <<SCRIPT
INTERFACE=`ip a|grep ^2: |awk -F":" '{print $2}'`
sudo rm -fr /etc/sysconfig/network-scripts/ifcfg-$INTERFACE
sudo /usr/bin/nmcli con mod $INTERFACE ip4 ${1}${2} ipv4.method manual ipv4.gateway $3
sudo /usr/bin/nmcli con mod $INTERFACE connection.autoconnect on
SCRIPT


Vagrant.configure("2") do |config|
  config.vm.box = 'generic/centos7'
  config.vm.synced_folder('.', '/vagrant', type: 'nfs', disabled: true)

  #  Do post up provision
  config.vm.provision "shell" do |s|
    s.inline = $script
    s.args   = [ $mgmt_ip , $mgmt_cidr , $mgmt_gw ]
  end

  config.vm.provider :vmware_esxi do |esxi|
    esxi.esxi_hostname = "esxi"
    esxi.esxi_username = "root"
    esxi.esxi_password = 'prompt:'
    esxi.esxi_hostport = 22
    esxi.local_allow_overwrite = 'True'
  end
end

Bring up the VM, then use "vagrant reload" to reboot the VM.

$ vagrant up --provider vmware_esxi
$ vagrant reload

Debugging

Use these Vagrantfile options to help you out...

  esxi.local_use_ip_cache = 'False'
  esxi.debug = 'true ip'