12

I know that you shouldn't have duplicate entries in your Windows NT hosts file, windows\system32\drivers\etc\hosts, but what is the expected behavior if you have them?

You can have multiple entries with the same hostname but different addresses in the Windows NT hosts file (windows\system32\drivers\etc\hosts). When you access that host on the network, the Windows DNS system will resolve it to one of those entries. How can I tell which entry will be used?

E.g.:

127.0.0.1    mydomain.com
192.168.1.1  mydomain.com

Edit: This particular example seems to force special behavior. The loopback seems to override any entry, regardless of order.

Or:

192.168.1.2  mydomain.com
192.168.1.1  mydomain.com

I am seeing the first entry in the list take precedence.

But, is the behavior I am seeing guaranteed, is it version specific, or is it generally unreliable?

The reason I ask is because I am writing some scripts to handle hosts, and I am trying to figure out if I can reliably select one of them to remove/comment out, and expect there to be no impact to the user's system.

4
  • Just noticed that 127.0.0.1 seems to force special behavior. I edited the question to ask about the different scenarios... Jul 19, 2011 at 8:02
  • Who says that one cannot have multiple entries with the same domain name and different IP addresses? Certainly not Microsoft. It's a perfectly legitimate thing, according to Microsoft.
    – JdeBP
    Jul 19, 2011 at 11:45
  • @JdeBP: I know this is bordering on a second question, but if a host is multi-homed, what does that mean, and how is that useful to me? If I ping it, I'll only get one address. Same if I try to terminal-serve to it. BTW, your link is quite useful to me, as it backs up the behavior I described in my question. You should write an answer :) Jul 19, 2011 at 16:58
  • Tried to edit the question to make it less inaccurate, based on JdeBP's comment. Mar 13, 2012 at 4:47

4 Answers 4

5

Only the first will be read. Once the name has been resolved, no further reading of the file takes place.

2
  • One: You contradict the question. Two: The hosts file is read in its entirety when the DNS Client service is enabled, and multiple entries with the same domain name are quite legitimate.
    – JdeBP
    Jul 19, 2011 at 11:44
  • @Synetech: Can you clarify which part is incorrect? Is it the part JdeBP already mentioned on the OP (that you can have multiple entries with the same domain) or is there something else I've missed? :) Mar 13, 2012 at 4:40
5

Multiple entries with the same domain name is allowed and useful in some scenarios.

As I described in my question, it seems that in "normal" cases, where those addresses are reachable by the same adapter, the first address seems to be the one that is taken. If one of those entries is loopback (127.0.0.1), it might take precedence though.

Sometimes it makes sense to have multiple entries with the same domain name, even when none of the addresses are to the loopback adapter, if the computer is multihomed or multiaddressable. (Thanks JdeBP for the info in the comments on my OP).

a single DNS host name can correspond to more than one IP address if each of the addresses is mapped and used in separate lines. For example, you can add lines for the following multihomed or multiaddressable DNS host computer:

10.0.0.1  host-a.example.microsoft.com
10.0.0.2  host-a.example.microsoft.com
10.0.0.3  host-a.example.microsoft.com

I believe that adapter preference/priority might play a role here (which you can set up in network settings), though I cannot test it out. Loopback will most likely always come first in adapter preference.

1
  • I can't test out adapter preference/priority at this point, so please comment if you know this to be correct or incorrect. Dec 7, 2011 at 22:41
1

I have tried 5 minutes ago and it runs without dns server on the following way into the windows hosts file:

ip1,ip2,ipx name_to_resolve

Sorry my english...

1
0

> The loopback seems to override any entry, regardless of order.

Did you make sure to close and re-run the client (browser?) after making the change? If you don’t, then it will likely still be using the old values and not see the change in order until it is restarted. Further, if you have the DNS Client service running, the old values are cached, so you will not see the change.

I did some tests and it seems that the first entry is always the one that is used.

Make sure to restart the client to ensure that it is re-reading the HOSTS file, and if you have the DNS Client service running also run ipconfig /flushdns

2
  • Can't remember if I did or not, as I was working with this between 3 to 8 months ago. But good point and thanks for checking into this. Do you have multiple adapters, and can you check if the adapter preference settings I mentioned in my self-answer have any impact? Mar 13, 2012 at 4:38
  • 1
    Actually, multiple NIC order was discussed recently.
    – Synetech
    Mar 13, 2012 at 4:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .