/etc/hosts
100%

DNS · Lesson 4

/etc/hosts

Learn how local hosts-file mappings participate in Linux name resolution and how to test them safely.

/etc/hosts provides static address-to-name entries to the local system name-service stack. It is useful for loopback names, bootstrap dependencies, and narrowly scoped tests, but it does not publish records to other hosts or update DNS.

Reading the File

A line begins with an IPv4 or IPv6 address followed by one or more names:

127.0.0.1       localhost
192.0.2.25      app-test.example.net app-test
2001:db8::25    app-test-v6.example.net app-test-v6

Comments begin with #. The first name is conventionally treated as canonical by some tools, while later names are aliases, but application behavior and resolver APIs vary. Avoid duplicate or conflicting entries for the same name.

What appears first on a normal /etc/hosts mapping line?

Resolver Order

The Name Service Switch configuration, commonly /etc/nsswitch.conf, determines how system resolver functions combine files, DNS, multicast systems, and other sources. A common line is:

hosts: files dns

Do not assume files always come first without inspecting policy. Applications can also use their own DNS libraries, caches, proxies, or encrypted resolvers and may not follow the system path.

What determines whether /etc/hosts is consulted before DNS by the system resolver?

Testing Through the System Resolver

Use getent to exercise the configured system name-service path:

$ getent ahosts app-test.example.net

dig queries DNS directly and normally does not report /etc/hosts mappings. This difference is useful: getent succeeding while dig does not can indicate a local source or resolver policy difference.

Which tool is better for checking whether normal system resolution sees a hosts-file entry?

Editing Safely

Preserve required localhost and host-identity entries, validate the intended address, and make a recoverable change with privileged editor tooling. Avoid overriding a real public domain as a casual test; it can redirect credentials or application traffic unexpectedly. Use a dedicated test name and remove the entry after the experiment.

After editing, test the exact application because it may retain a cache or use a different resolver. Document persistent overrides so they do not silently outlive their purpose.

Why use a dedicated test name instead of overriding a public service name?

Resolver Server Configuration

/etc/resolv.conf traditionally lists DNS resolver settings, but it is often generated by NetworkManager, systemd-resolved, DHCP, or another manager. Inspect symlinks and file comments, then change the owning configuration source rather than editing generated output that will be overwritten.

What should you do before editing /etc/resolv.conf?

Lesson complete

You finished /etc/hosts

You can now use /etc/hosts as a controlled local resolver input.

  • Write address-first mappings with deliberate names and aliases.

  • Inspect Name Service Switch ordering instead of assuming it.

  • Test system resolution with getent and DNS separately with dig.

  • Use dedicated temporary names and verify the real application.

  • Change resolver servers through the configuration owner.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to DNS