1

Let's say I have inside of my hosts file an entry for foobar, how do I find out if that file is used or if a remote DNS is answering the resolution for foobar?

Do any of the basic utilities give back that information?

dig +trace isn't showing me what I want to know

dig +trace localhost

; <<>> DiG 9.10.3-P4-Ubuntu <<>> +trace localhost
;; global options: +cmd
;; Received 28 bytes from 127.0.0.53#53(127.0.0.53) in 0 ms

dig +trace google.com

; <<>> DiG 9.10.3-P4-Ubuntu <<>> +trace google.com
;; global options: +cmd
;; Received 28 bytes from 127.0.0.53#53(127.0.0.53) in 0 ms

I would expect it to tell me that the lookup is resolving to hosts for localhost and my dns server (whichever one) for google.com. If I make a call internally using getaddrinfo(3) is it hitting the hosts file or the upstream dns server when resolving localhost.

  • @Christopher not a dupe, I clarified. – Evan Carroll May 16 '17 at 18:49
  • 1
    @RuiFRibeiro Neither of those are even close to what I'm looking for.. I want to know whether or not getaddrinfo is hitting the hosts file or the upstream dns server when resolving localhost. – Evan Carroll May 16 '17 at 18:55
1

dig is a DNS troubleshooting tool: you either specify explicitely the DNS server you want to query or, if you do not, it will use what is specified in your resolv.conf. As written in its documentation:

Unless it is told to query a specific name server, dig will try each of the servers listed in /etc/resolv.conf. If no usable server addresses are found, dig will send the query to the local host.

For any other application on the system, needing to do a DNS resolution, this will be under the control of /etc/nsswitch.conf that lists the source of information to query to resolve hostname, like:

hosts: files myhostname mdns4_minimal [NOTFOUND=return] dns mdns4

which for example shows that the glibc will first have a look in /etc/hosts (the files token) and only later on do a DNS query (the dns token), again using the content of resolv.conf

If you want to be 100% sure for a given application, you will need to run it inside a strace call.

  • 1
    To run a test query using the system APIs, use getent hosts or getent ahosts. To see if the result it provides is coming from a given service, you could use getent -s files ahosts - try each service in the order specified in nsswitch.conf. – sourcejedi May 29 '17 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.