1

The nslookup, dig and ping return different values for me. I want foo.bar.name to always resolve statically to localhost on my machine.

I used the command

host foo.bar.name

and edited /etc/hosts to include the line

1.1.1.1 foo.bar.name foo

(where 1.1.1.1 ip address of my host machine)

When I run either ping or curl, it behaves the way I want.

But when I run dig, or nslookup for that matter, the foo.bar.name address resolves completely differently.

Did I miss something?

What configurations do I need to modify to make sure that on that particular host DNS resolution happens the way I've stated.

2

ping, curl, and all other apps don't really use DNS directly – they use the OS-provided "gethostbyname" function, which then calls multiple providers. One of them is "dns" (which speaks to DNS servers), another is "files" (that is /etc/hosts); there can be additional providers which speak other protocols. They're all configured via /etc/nsswitch.conf.

Meanwhile dig, host, and nslookup are fundamentally DNS clients – they bypass the OS "gethostbyname" functions and instead directly craft & send DNS packets. (They were specifically written this way.) As a result they also skip /etc/hosts and any other name resolution mechanisms, and there's nothing you can configure to "fix" this.

(The OS itself is not going to examine every UDP packet to check if it's DNS, and it's also not going to inject a fake DNS reply from /etc/hosts just because.)

The only way to make dig &c. honor /etc/hosts is to set up an actual DNS server for the app to speak to, which would provide the desired static replies. For example, dnsmasq will act as DNS cache/proxy and loads static data from /etc/hosts by default. Alternatively, Unbound has extensive "local-data" configuration, although it can't read /etc/hosts directly.

Once you've configured dnsmasq or Unbound, point /etc/resolv.conf to 127.0.0.1 (or whatever other host dnsmasq is running on).

2

What configurations do I need to modify to make sure that on that particular host DNS resolution happens the way I've stated.

You can setup lightweight DNS forwarder such as unbound where you can override any DNS records.

Using your example it would looks in unbound config like:

local-zone: "foo.bar.name" redirect
local-data: "foo.bar.name A 1.1.1.1"

If you want to override external DNS you may use:

local-data: "www.google.com.  1800  IN  A    172.16.34.12"
local-data: 'www.google.com.  7200  IN  TXT "My own TXT record for text"'
local-data-ptr: "172.16.34.12        www.google.com"

When you finish setting it up, test it with dig @127.0.0.1 foo.bar.name a
If your new resolver works Ok, then set it as a system wide by changing nameserver line in the /etc/resolf.conf to nameserver 127.0.0.1

P.S.

Some of debian base distros installs dnsmasq as a DNS cacher. If you will use unbound then turn off dnsmasq

  • dnsmasq has a similar configuration, and it even loads /etc/hosts by default (unless the distro has turned it off). – user1686 Jan 21 '17 at 10:27
0

system be deault checks in /etc/hosts and then uses name server. and what you did is perfectly fine. it's just that some programs are designed to talk to name servers directly, ie host, nslookup and dig. that's why you have different results.

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.