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.

Merlyn Morgan-Graham
  • 1,752
  • 4
  • 20
  • 26

4 Answers4

5

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

Abraxas
  • 1,265
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.

Merlyn Morgan-Graham
  • 1,752
  • 4
  • 20
  • 26
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...

Darckclone
  • 11
  • 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

Synetech
  • 69,547