Automatic Windows 'hosts' Update in a DHCP Environment

64 views
Skip to first unread message

Mark McFate

unread,
May 7, 2014, 12:05:26 PM5/7/14
to vagra...@googlegroups.com

I am by no means a systems admin, nor a networking expert, so what I’m trying to do may be way off the mark, but let me try to describe my situation…

My employer recently purchased a Vagrant VMWare provider license which I’m successfully using with VMWare Workstation 10 on a Windows 7 host using Puppet to provision my VMs.  My base VM configuration uses an Ubuntu Precise64 box with DHCP and port forwarding.  The relevant parts of my Vagrantfile are shown below.

   ## Configure VMWare Workstation as the provider of choice.

  ## System parameters like available memory and number of CPU cores go here.

     config.vm.provider "vmware_workstation" do |v|

       v.vmx["memsize"] = "1024"

       v.vmx["numvcpus"] = "2"

     end

 

  ## Give our guest a name.

     config.vm.host_name = "base.dg.dev"

 

  ## Every Vagrant virtual environment requires a box to build off of.

     config.vm.box = "precise64_vmware"

     config.vm.box_url = "http://files.vagrantup.com/precise64_vmware.box"

 

  ## Configure a public network adapter per DHCP.

     config.vm.network "public_network", :bridge => 'en1: Prompt Me'

 

Ultimately I would like to be able to address and remotely debug web apps running on this guest VM (and other, similar guests) using the configured vm.host_name, in this case “base.dg.dev”.  Given my limited knowledge of networking, the only way I know of doing this currently is to edit my windows “hosts” file (C:\Windows\System32\drivers\etc\hosts) and insert (or update) a line like this:

    192.168.1.58     base.dg.dev

In the example above the address of 192.168.1.58 is the dynamic value assigned to base.dg.dev VM by my router. 

While this solution seems to work nicely, I find myself making frequent changes to the “hosts” file as my VMs move from one network environment to another (I frequently work from home).  So my question… Is there a way to have Vagrant modify my Windows “hosts” file automatically, or some other technique that I’ve completely overlooked?

Thanks for any assistance or advice you can provide.  Take care.

Alvaro Miranda Aguilera

unread,
May 7, 2014, 7:25:34 PM5/7/14
to vagra...@googlegroups.com
you are using dhcp, so the ip will be different each time the dhcp give you a new one, or when you are in a different dhcp server.

the vagrant way to do this, is forward a port, say 8080 to port 80 in the guest, so then you can connect as http://localhost:8080

the 2nd vagrant way to do this, is assign a fixed IP, instead of dhcp, you can tell the VM what IP you want to use, so you can use then that ip

a non-vagrant way to do it, is ask the network guy at the company to give you always the same IP based on your mac, however, due the nature of vagrant, you may end destroying that vm, creating a new one, and that mac could change, so is not very feasible.

if at this point only you connect, i suggest going with port forward.


Alvaro




--
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark McFate

unread,
May 8, 2014, 2:35:19 PM5/8/14
to vagra...@googlegroups.com
Thanks Alvaro.  I already have port forwarding working, and setting a static IP is not an attractive option for me becasue I routinely move from one DHCP network to another, and they have very different IP address ranges.  At the root of my issue is Drupal.  I'm building Drupal sites into this VM, and others, and there are characteristics of Drupal that make referencing a site by name much better than referencing it by address.

I'll keep using my Windows "hosts" file and manually editing it for now, but  also keep looking for an automated solution.

I wonder if there is a way in Windows to turn my "hosts" file into a link (shortcut?) and point that to a file that I can modify on-the-fly?  I might experiment with that just a bit.

Thanks.   

Mark McFate

unread,
May 8, 2014, 2:43:56 PM5/8/14
to vagra...@googlegroups.com
I just checked to see if putting a shortcut (link) in /System32/Windows/drivers/etc/hosts would work...as expected, it does not.  I'll keep hunting for a solution to this.

-Mark

Alvaro Miranda Aguilera

unread,
May 8, 2014, 5:42:51 PM5/8/14
to vagra...@googlegroups.com
what I do is create a shortcut to notepad, then edit and add the path to the file at the end of the line, and execute that right click as administrator

Alvaro Miranda Aguilera

unread,
May 8, 2014, 5:43:55 PM5/8/14
to vagra...@googlegroups.com

On Fri, May 9, 2014 at 6:43 AM, Mark McFate <summitt...@gmail.com> wrote:
/System32/Windows/drivers/etc/hosts

notepad c:\windows\System32\Windows\drivers\etc\hosts

run  as administrator

Mark McFate

unread,
May 8, 2014, 11:01:05 PM5/8/14
to vagra...@googlegroups.com
Yes, I'm using the very same shortcut.  If I find any way to automate this I will post it here. Thanks.


--
You received this message because you are subscribed to a topic in the Google Groups "Vagrant" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vagrant-up/qLkJ1kEdxdY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vagrant-up+...@googlegroups.com.

Dan Barua

unread,
May 9, 2014, 4:17:02 AM5/9/14
to vagra...@googlegroups.com
Notepad++ has a "Save as Administrator" plugin for that :)
Reply all
Reply to author
Forward
0 new messages