Linux online course

Linux /etc/hosts File for Local Name Resolution

Learn how to use Linux /etc/hosts to map hostnames to IP addresses, check lookup order with NSS, override DNS locally, and troubleshoot name resolution.

The Linux /etc/hosts file is a local, plain-text file used for name resolution. Name resolution is the process of finding the IP address associated with a hostname. For example, an entry can tell the system that webserver means 192.168.198.140.

A hosts-file mapping is static and applies only to the Linux machine containing the file. The machine can resolve matching names without contacting a DNS server.

What Is the /etc/hosts File?

The file is located at /etc/hosts. It contains mappings between IP addresses and hostnames. A hostname is a human-readable name for a system or network endpoint, while an IP address is the network address used to reach it.

Each active entry normally occupies one line. The IP address comes first, followed by one or more hostnames or aliases separated by spaces or other whitespace:

IP_ADDRESS HOSTNAME ALIAS

An alias is an additional hostname that resolves to the same address. Lines beginning with # are comments and are ignored. Blank lines can separate groups of entries and improve readability.

IP address — Identifies the destination system. Example: 192.168.198.140.

Primary hostname — The main human-readable name. Example: webserver.

Optional aliases — Additional names for the same address. Example: webserver.internal.

Comment — Explanatory text beginning with #. Example: # development server.

Default Loopback Entries

Most Linux systems include entries for localhost, the conventional hostname for the local machine:

127.0.0.1 localhost
::1 localhost

127.0.0.1 is the commonly used IPv4 loopback address. ::1 is the IPv6 loopback address. A loopback address refers back to the same machine rather than to another device on the network.

Do not remove existing localhost-related entries casually. Local software may depend on IPv4, IPv6, or both. A system can also contain additional standard entries, such as a local hostname or broadcast-related names.

Viewing and Editing the File

View the current file with:

cat /etc/hosts

Editing normally requires administrative privileges because /etc/hosts is owned and protected by the system:

sudo cp /etc/hosts /etc/hosts.bak
sudo nano /etc/hosts

Making a backup first gives you a simple recovery option. After saving, check the file for misspellings, duplicate names, and conflicting entries.

Creating a Custom Hostname Mapping

To map a private-network server to a memorable hostname, add the IP address first and the hostname second:

192.168.198.140 webserver

You can assign multiple names to the same address on one line:

192.168.198.140 webserver webserver.internal

Programs that use the system name-resolution mechanism can then resolve both webserver and webserver.internal to 192.168.198.140.

A complete small example might look like this:

127.0.0.1 localhost
::1 localhost

# Private development server
192.168.198.140 webserver webserver.internal

Verifying and Using a Mapping

Use getent hosts to test the system resolver:

getent hosts webserver

A successful result should contain the mapped address and hostname, similar to:

192.168.198.140  webserver

You can also test the name with ping:

ping -c 1 webserver

Successful name resolution does not prove that the target service is reachable. The host might be powered off, blocked by a firewall, unreachable because of routing, or not listening on the service port. Separate name lookup, basic network reachability, and service availability when troubleshooting.

How /etc/hosts Relates to DNS

DNS, the Domain Name System, is a distributed service that resolves names to addresses. Linux can use both local hosts entries and DNS during name resolution.

A common configuration checks the hosts file before DNS. If a hostname matches an entry in /etc/hosts, that local result can override an address returned by DNS. If there is no matching local entry, the resolver usually continues to DNS, subject to the system's resolver configuration.

Scope/etc/hosts: one machine; DNS: a network, organization, or the public Internet.

Administration effort/etc/hosts: edit each client; DNS: manage records centrally.

Best use case/etc/hosts: small tests, development, temporary overrides, isolated systems, and local aliases; DNS: shared or production infrastructure.

Scalability/etc/hosts: poor for many changing hosts; DNS: designed for larger and changing environments.

Typical lookup role/etc/hosts: local static result; DNS: fallback or centrally managed result.

Name Service Switch and Lookup Order

Linux commonly uses NSS, or Name Service Switch, to select lookup sources and their order. The main configuration file is /etc/nsswitch.conf.

Inspect the hostname lookup rule with:

grep '^hosts:' /etc/nsswitch.conf

A common result is:

hosts: files dns

Here, files means the local hosts file and dns means DNS. The local file is checked first, so a matching hosts entry takes precedence over DNS.

Local hosts file — Used first when the hosts: rule begins with files. A match supplies the local address.

DNS server — Used when no earlier source matches, or when the configured order places DNS first. A DNS match supplies the address.

Do not assume that every Linux system uses the same order. Lookup precedence is configurable, and some systems include additional sources or resolver components. Check /etc/nsswitch.conf when behavior matters.

Temporarily Overriding DNS

A hosts entry can direct one machine to a different address for a name that also exists in DNS:

192.168.198.140 webserver.example.test

With hosts: files dns, the local address is returned for that machine instead of the DNS-provided address. This is useful for development and testing, but it can also cause confusion if an old override remains in the file.

Limitations and Appropriate Uses

Hosts-file records are local. They are not automatically shared with other computers, containers, virtual machines, or users. Every client that needs the mapping must be updated individually.

Appropriate uses include:

  • Small test environments
  • Temporary DNS overrides
  • Local development
  • Isolated systems without DNS access
  • Short local aliases for a few machines

DNS is generally more scalable and manageable when many clients need the same records or when infrastructure changes frequently. Central DNS avoids maintaining separate copies of the same mapping on every client.

Safe Editing and Operational Behavior

  • Back up the file before editing: sudo cp /etc/hosts /etc/hosts.bak.
  • Keep the IP address before the hostname on every active line.
  • Use whitespace to separate the address, hostname, and aliases.
  • Do not place an accidental # before a mapping, because that comments out the entire line.
  • Avoid duplicate or conflicting hostname entries. They make the result difficult to predict and maintain.
  • Preserve required localhost entries unless you understand the consequences of changing them.

Changes normally affect new name lookups without restarting a DNS service. However, individual applications, browsers, libraries, or local resolver services may cache results. If getent hosts shows the new address but one application does not, the application may need to be restarted.

Troubleshooting /etc/hosts

The hostname does not resolve

  • Verify that you edited /etc/hosts, not another similarly named file.
  • Confirm that the IP address appears before the hostname.
  • Check spelling, whitespace, hidden characters, and accidental comment markers.
  • Test the system resolver with getent hosts hostname.

DNS returns a different address

  • Inspect the lookup order with grep '^hosts:' /etc/nsswitch.conf.
  • Ensure files appears before dns if local entries should take precedence.
  • Check whether the application uses its own DNS resolver or cached results.

The name resolves but the connection fails

  • Confirm that the mapped IP address is correct.
  • Test basic reachability separately from service availability.
  • Check routing and firewall rules.
  • Verify that the remote service is listening on the required port.

An application still uses the old address

  • Run getent hosts hostname to check system resolver behavior.
  • Consider application, browser, or service-level DNS caching.
  • Restart only the affected application when its cache is responsible.

Different machines resolve the name differently

  • Remember that /etc/hosts is local to each machine.
  • Compare the hosts file and NSS configuration on each client.
  • Use centrally managed DNS when a shared mapping is required.

Exam-Relevant Notes

  • /etc/hosts is a local plain-text source of static IP-to-hostname mappings.
  • The normal entry order is IP address, hostname, then optional aliases.
  • 127.0.0.1 and ::1 are IPv4 and IPv6 loopback addresses.
  • /etc/nsswitch.conf controls lookup-source order through the hosts: rule.
  • hosts: files dns commonly means the hosts file is checked before DNS.
  • A local match can override a DNS result, but the override affects only the machine with that entry.
  • getent hosts is useful for testing the system resolver, while ping also tests an aspect of network reachability.

For related Linux command-line and filesystem fundamentals, see Linux, showing the full path of shell commands, and determining file types.