Random Musings

Sporadic thoughts on tech, economics, business, finance and trading

When I do a ping in Linux, does the command first check /etc/hosts and then /etc/resolv.conf or vice versa?

,

When you run the ping command in Linux, the system resolves the target hostname to an IP address. The resolution process follows the order defined in the Name Service Switch (NSS) configuration file (/etc/nsswitch.conf), which specifies whether the system should check /etc/hosts or DNS (via /etc/resolv.conf) first.

Typically, the configuration in nsswitch.conf looks like this for hostname resolution:

hosts: files dns
  • files means /etc/hosts is checked first.
  • If no match is found, it proceeds to DNS as specified in /etc/resolv.conf.