windows - automatically add entries to hosts file

07
2014-07
  • Henrov

    I have a hosts file on my dropbox in which I create shortcuts to all kinds of ip's that I use often (server dev is added as d, stackoverflow is added as so and so on). Over time I have built quite a list. This list is purely personal. On each reinstall of my machine, with each VM I use I have to copy my hosts file from Dropbox into the machine I want to use. The other way around, when making a change in one of the hosts files, I have to take care that stuff keeps up to date somehow. I would prefer to simply have one place (machine-independant) where I can store my entries and then simply ímport/use them on my targetmachine. I have thought about using the company DNS but again, these shortcuts are strictly personal.

    Suggestions as to how I could do this? I was thinking about some import/regfile or so......

  • Answers
  • Tarek Jellali

    You can use a batch file to copy your hosts file from your Dropbox directory to C:\Windows\System32\drivers\etc this will simplify the things for you.


  • Related Question

    networking - Is the hosts file ignored in windows if DNS Client service is running?
  • Mnebuerquo

    I've seen a number of articles about how to edit the hosts file in Windows 7, but it's all about how to open notepad as administrator, not the actual behavior of the dns lookups afterward.

    I've read that the hosts file is ignored in XP SP2 if DNS Client service is running. I have tried this on my XP machine and it seems to be true. I can see how it is a security danger to have a hosts file that user programs could modify. If it could write to hosts, then any malware could spoof dns locally with minimal difficulty.

    I'm trying to use the hosts file for testing stuff on my local network without it going to the live site on the internet. At the same time I want to be able to use dns on the normal internet.

    Mostly though I just want to understand the rules on the newer windows systems.

    Thanks!


  • Related Answers
  • Fred

    XP uses the hosts file for name resolution even when DNS client is running. The contents of the hosts file are automatically loaded into the DNS resolver cache when you save the file.

    The Name Resolution Policy Table article says the same thing for Win 7:

    Here is how the name resolution process works for Windows 7 and Windows Server 2008 R2:

    1. An application uses the DnsQuery() API or the GetAddrInfo() or GetHostByName() Windows Sockets APIs to resolve a name. If the name is a flat name, the DNS Client service creates an FQDN using configured DNS suffixes.
    2. The DNS Client service checks the DNS resolver cache for the FQDN, which contains the entries in the Hosts file and the results of recent positive and negative name queries. If an entry is found, the result is used and no further processing occurs.