brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: the hosts line in nsswitch.conf decides who answers first

Linux
~/tips/tip-dns-nsswitch-hosts-order

grep ^hosts /etc/nsswitch.conf

Shows the order in which Linux asks /etc/hosts, DNS and other sources for a name.

If a name resolves in one tool and not in another, the answer is often not DNS at all. On Linux, a program that uses the system resolver library first looks at /etc/nsswitch.conf.

Look at the line for hosts:

grep ^hosts /etc/nsswitch.conf

A common result is hosts: files dns. The sources are asked in the order written, until one has an answer, so here /etc/hosts comes first and DNS second. A hit ends the search and a miss falls through to the next source, which is why an entry in /etc/hosts beats whatever the DNS server says.

Each source can carry an action in brackets, such as [NOTFOUND=return]. It means: if this source answered and did not know the name, stop here and skip the sources after it. You may see it on desktop systems in an entry like mdns4_minimal [NOTFOUND=return] dns.

To see what your programs really get, use getent hosts example.com. Caveat: dig and nslookup ask a DNS server directly and skip this file, so they can disagree with ping or a browser while nothing is broken. Edit the line with care, because a wrong order can make every lookup slow or fail.

Further reading: nsswitch.conf(5) manual

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link