4. /etc/hosts

Before your Linux system queries a DNS server to resolve a hostname, it first looks for a mapping on the local machine. This initial check is a fundamental part of the name resolution process.

The Role of /etc/hosts

The primary file for this local lookup is /etc/hosts. This simple text file contains static mappings of hostnames to IP addresses. The structure of the etc hosts file is straightforward, with each line containing three fields: the IP address, the canonical hostname, and optional aliases for that host.

Here is a typical example of an etc host linux file:

pete@icebox:~$ cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       icebox

You will almost always find the localhost address mapped by default. This file is a standard feature across most Linux distributions, including on Debian hosts.

Editing the etc hosts linux file

You can manually edit the /etc/hosts file to create your own mappings. Let's try a fun example. Add the following line to your file:

123.45.6.7  www.google.com

After saving the file, try navigating to www.google.com in your web browser. You'll find that it doesn't work. This is because we mapped www.google.com to an incorrect IP address. Since your system checks the local /etc/hosts file first, it uses our faulty mapping and never proceeds to query a DNS server to find the correct address. To fix this, simply remove the line you added.

While older systems used /etc/hosts.deny and /etc/hosts.allow for access control, this method is largely obsolete. Modern security practices rely on configuring firewall rules for robust protection instead.

Local DNS Server Configuration

Traditionally, the /etc/resolv.conf file was used to specify the DNS name servers for lookups. However, with advancements in system management, this file is often no longer managed manually. As you can see in the example below, the file is generated automatically by another service. For managing DNS name server mappings, you should refer to the documentation for your specific distribution, as tools like systemd-resolved or resolvconf often handle this now.

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.1.1
search localdomain

Sign in to save your learning progress

Sign in

Exercises

Practice makes perfect! Here are some hands-on labs to reinforce your understanding of local hostname resolution and DNS queries:

  1. Manage Local Hostname Resolution in Linux - Practice editing the /etc/hosts file to manage local hostname resolution, a key step before DNS queries.
  2. Query DNS Records in Linux with dig and nslookup - Learn to query DNS records using essential Linux tools like dig and nslookup to understand how your machine resolves external names.

These labs will help you apply the concepts in real scenarios and build confidence with hostname resolution and DNS.

Quiz

What file is used to map hostnames to IP addresses on our machines? (Please answer in English, paying attention to case sensitivity).