When do you use /etc/hosts and when DNS?
BeginnerScenarioDNS & Service Discovery
Answer
Situation read → Hosts file is local, static and overrides DNS for that machine only.
What I'd do →
- Use
/etc/hostsfor local testing, bootstrapping, or pinning a name before DNS exists - Use DNS for anything shared across hosts
- Order is controlled by
hosts:in/etc/nsswitch.conf(files dns) - Test with
getent hosts myappwhich follows nsswitch rather than only querying DNS
Why → dig ignores the hosts file, so it can disagree with what applications see; getent shows the application's view.
Interview tip → Mention that stale hosts entries are a classic cause of "works on my machine".
More on DNS & Service Discovery
A customer types www.example.com in a browser. Walk through how DNS resolves it.BeginnerWhat is the difference between an A record and a CNAME, and when do you use each?BeginnerWhy can't you put a CNAME at the zone apex, and what do you use instead?BeginnerYou are moving a site to a new server. What do you do with DNS to make the cutover smooth?BeginnerHow do you find which nameservers are authoritative for a domain and query them directly?BeginnerWhat DNS records do you set up for company email?Beginner