1. Introduction

Hostname resolution in Linux is the process of translating a domain name (e.g., example.com) into its corresponding IP address.

This process is controlled by:

  • /etc/nsswitch.conf → Defines lookup order
  • /etc/hosts → Local static hostname mappings
  • /etc/resolv.conf → DNS resolver configuration

This document explains how hostname resolution works on the current server configuration.


2. Current Configuration

2.1 /etc/nsswitch.conf

hosts: files dns

This line defines the order in which hostname resolution occurs.

  • files → Check /etc/hosts
  • dns → Query DNS server

2.2 /etc/hosts

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
12.34.56.78     example.com 

This file contains static hostname-to-IP mappings.


2.3 /etc/resolv.conf

nameserver 127.0.0.53
options edns0 trust-ad
search .

Key points:

  • 127.0.0.53 → systemd-resolved stub resolver
  • DNS queries are handled by systemd-resolved
  • Upstream DNS servers can be checked using:
resolvectl status

3. Hostname Resolution Flow

When an application tries to resolve:

example.com

The system follows the order defined in:

hosts: files dns

Step 1: Check /etc/hosts

  • System checks local file first.
  • If hostname is found → return IP immediately.
  • No DNS query is performed.

In this case:

12.34.56.78 example.com

So resolution stops here.

Step 2: Query DNS (If Not Found in Hosts)

If the hostname does not exist in /etc/hosts:

  • Query is sent to 127.0.0.53
  • systemd-resolved forwards request to configured upstream DNS servers
  • DNS server responds with IP

4. Priority Order

OrderSourceDescription
1/etc/hostsLocal static mapping
2DNSRemote DNS server

Important:

  • /etc/hosts always overrides DNS.
  • If an entry exists locally, DNS will not be queried.

5. Practical Example

Running:

ping example.com

Resolution process:

  1. Check /etc/hosts
  2. Entry found
  3. IP 12.34.56.78 returned
  4. DNS is NOT used

6. Conclusion

Based on the current configuration:

  • Hostname resolution follows:
    /etc/hosts → DNS
  • Local entries have higher priority than DNS.
  • systemd-resolved handles DNS forwarding.
  • This setup ensures predictable and controlled hostname resolution.

This configuration is standard and recommended for environments requiring local overrides and centralized DNS management.

Leave a Reply