I would like to ask you a question because I don't have the environment right now and I can't try it myself.
When resolving names in the hosts
file, in what order is it resolved if I specify the same host for multiple IP addresses?
For example,
1.1.1.1 A
1.1.2.1 A
So what's the order of resolution?
hosts
Defined in RFC 6724.One Linux gai.conf
(5) has the following description: (RFC 3484 is an older version replaced by RFC 6724)
A call to getaddrinfo(3) may return multiple answers.According to RFC 3484, these answers should be sorted in such an order that the most likely successful answers are at the top of the list.The RFC has an algorithm for sorting.
In general, the order is as follows:
First, IPv6>IPv4.Among them, the rankings are as follows:
It seems that the order of description of hosts
will be the same, but it depends on the implementation.
GNU libc(getaddrinfo
(3)) can be customized in the /etc/gai.conf
file.For Windows, the netsh
command sets the prefixpolicies
, but I don't know because I've never used it.
Helpful pages:
dnsstubq.c
(the command-line tool source code that displays the name resolution results exactly in getaddrinfo
(3) use case in Windows)
The results of the name resolution are as described above, but it is up to the implementation to determine the priority and multiple use of it.In general, getaddrinfo
(3) (or equivalent API) only uses one in the order returned, but I think there is also an implementation that randomizes it.The latter example is DNS-only because it is about DNS MX RR, but the Postfix smtp_randomize_addresses=yes setting (see postconf
(5)).
If you simply answer in the order of hosts, it will be in ascending order (from the top).
For example, ping or tracert when hosts are:
hosts
127.0.0.1 A
127.0.1.1 A
127.0.0.2 A
1.1.1.1 A
1.1.2.1 A
1.1.1.2 A
ping-a A-n 1
C:\WINDOWS\system32>ping-a A-n 1
Ping A [1.1.1.1]32 bytes of data:
Response from 1.1.1.1: Bytes = 32 hours = 4ms TTL = 55
Ping Statistics for 1.1.1.1:
Number of packets: Transmit = 1, Receive = 1, Loss = 0 (0% loss),
Approximate round-trip time in milliseconds:
Minimum = 4 ms, Maximum = 4 ms, Average = 4 ms
C:\WINDOWS\system32>
tracert A
C:\WINDOWS\system32>tracert A
Tracing route to A [1.1.1.1]
Up to 30 hops through:
1 <1 ms <1 ms <1 ms hoge 0
22 ms<1 ms<1 ms hoge1
: omission
1015 ms 14 ms 17 ms hoge 9
114 ms 2 ms 2 ms A [1.1.1.1]
US>Tracing.
C:\WINDOWS\system32>
hosts
127.0.0.1 A
127.0.1.1 A
127.0.0.2 A
1.1.1.1 A
1.1.2.1 A
1.1.1.2 A
ping-a A-n 1
C:\WINDOWS\system32>ping-a A-n 1
Ping A [1.1.1.1]32 bytes of data:
Response from 1.1.1.1: Bytes = 32 hours = 4ms TTL = 55
Ping Statistics for 1.1.1.1:
Number of packets: Transmit = 1, Receive = 1, Loss = 0 (0% loss),
Approximate round-trip time in milliseconds:
Minimum = 4 ms, Maximum = 4 ms, Average = 4 ms
C:\WINDOWS\system32>
US>tracert A
C:\WINDOWS\system32>tracert A
Tracing route to A [1.1.1.1]
Up to 30 hops through:
1 <1 ms <1 ms <1 ms hoge 0
22 ms<1 ms<1 ms hoge1
: omission
1015 ms 14 ms 17 ms hoge 9
114 ms 2 ms 2 ms A [1.1.1.1]
US>Tracing.
C:\WINDOWS\system32>
If you try to pattern each line in this hosts file while commenting out, you can see that the behavior changes depending on the order of hosts and addresses from 1st to 4th octets.
Note that nslookup only resolves names to DNS servers and does not reference anything else (e.g., hosts).
Another solution is Windows Server 2016, so I would like to enable Windows DNS functionality and create a locally completed DNS environment.
We believe that "Domain Name: IP Address=1:n" can be achieved in the definition of A record.
© 2024 OneMinuteCode. All rights reserved.