Interview Prep/Networking/DNS & Service Discovery

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 →

  1. Use /etc/hosts for local testing, bootstrapping, or pinning a name before DNS exists
  2. Use DNS for anything shared across hosts
  3. Order is controlled by hosts: in /etc/nsswitch.conf (files dns)
  4. Test with getent hosts myapp which 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".

All Networking questions Roadmap: Networking for DevOps

More on DNS & Service Discovery