Skip to main content
This lesson introduces DNS basics on Linux for absolute beginners. You’ll learn how name resolution works locally (with /etc/hosts), how to point systems at a central DNS server (/etc/resolv.conf), how lookup order is decided (/etc/nsswitch.conf), and how to test DNS using tools like ping, nslookup, and dig. The examples use two hosts on the same network:
  • Host A: 192.168.1.10
  • Host B: 192.168.1.11 (runs database services)
The goal: be able to reach the database by a memorable name (for example, db) instead of remembering an IP address.

Local name resolution with /etc/hosts

On small networks you can map names to IPs in /etc/hosts. By default, Linux resolves names from the local hosts file first (depending on nsswitch.conf—see below). Example: host A can ping host B by IP but cannot ping it by name until you map the name:
Add a mapping on host A:
Now the short name resolves locally:
Important: /etc/hosts is local-only and is considered authoritative by the resolver on that machine. Host A will use the mapping you added even if the remote system’s own hostname (output of hostname) is different. You can map any domain locally. For example, map www.google.com to the same IP for testing:
You can maintain multiple entries in /etc/hosts for web, db, NFS, etc.:
Translating hostnames to IPs this way is called name resolution. For a few machines this is fine, but it doesn’t scale: any IP change must be applied to every hosts file.

Centralized DNS with a nameserver

To manage names centrally, run a DNS server and point clients at it. Suppose your DNS server is at 192.168.1.100; configure each host to use it by writing /etc/resolv.conf:
With this configuration:
  • If a name is not found in /etc/hosts, the system queries the configured DNS server(s).
  • If a service IP changes, update the DNS server and clients will resolve the new IP without editing /etc/hosts.
Editing /etc/resolv.conf directly may be temporary: many systems use DHCP, NetworkManager, or systemd-resolved which can overwrite that file. To make persistent changes, configure your network manager or DHCP client, or update the system resolver configuration according to your distribution. See the resolv.conf man page for details: https://man7.org/linux/man-pages/man5/resolv.conf.5.html
You can still keep local /etc/hosts entries for temporary or lab-only names. Example:

Lookup order — /etc/nsswitch.conf

If the same name exists in both /etc/hosts and DNS with different IPs, the system consults /etc/nsswitch.conf to decide which source to check first. A common setting:
  • files = /etc/hosts
  • dns = DNS servers listed in /etc/resolv.conf
With files dns, /etc/hosts is consulted first; only if the name is not found locally will DNS be queried. You can change the order in /etc/nsswitch.conf if you want DNS to take precedence.

DNS hierarchy and how resolution works

Domain names are hierarchical (labels separated by dots). The rightmost label is the top-level domain (TLD) like .com, .org, .net, etc. Example: www.google.com resolves through the DNS hierarchy:
  • Root (implicit trailing dot)
  • .com (TLD)
  • google (second-level domain)
  • www (subdomain)
Subdomains let organizations split services (for example, maps.google.com, drive.google.com, mail.google.com).
A diagram showing the DNS hierarchy with a root (.), a top-level domain (.com), the second-level domain "google", and subdomains: mail, drive, www, maps, and apps. The nodes are displayed as blue boxes on a dark background.
When your organization’s DNS server needs to resolve a name it’s not authoritative for, it can perform a recursive query up the DNS tree:
  1. Query root servers → get .com nameservers.
  2. Query the .com nameservers → get google.com nameservers.
  3. Query google.com authoritative nameservers → get the IP for apps.google.com.
  4. The organization’s DNS caches the answer for the TTL.
An infographic titled "Domain Names" showing the DNS resolution path for apps.google.com, with the hierarchical labels (.com → google → apps) and DNS stages (Org DNS, Root DNS, .com DNS, Google DNS) resolving to IP 216.58.221.78.
Many organizations run their own internal DNS (for example, mycompany.com) and create subdomains for services (web.mycompany.com, db.mycompany.com, etc.). To use short names like web instead of web.mycompany.com, add search domains in /etc/resolv.conf. The resolver appends each search domain and tries to resolve the short name. Example:
With that configuration, ping web will try:
  • web.mycompany.com
  • web.prod.mycompany.com
If you provide a fully qualified domain name (FQDN) ending with a trailing dot (for example, web.mycompany.com.), the resolver treats it as absolute and will not append search domains. The resolver behavior can also be influenced by the ndots option in /etc/resolv.conf.

Common DNS record types

DNS supports different record types. Use the table below as a quick reference:
A presentation slide titled "Record Types" showing a table of DNS records (A, AAAA, CNAME) with example hostnames like "web-server" and "food.web-server" mapped to an IPv4 address, an IPv6 address, and alias names.
Notes:
  • A and AAAA records map names directly to IP addresses.
  • CNAME records create aliases to another name; the resolver must then resolve that canonical name to an A/AAAA record.

Testing DNS: ping, nslookup, dig

  • ping uses the system resolver stack (consults /etc/hosts, follows nsswitch.conf, and queries DNS servers in /etc/resolv.conf).
  • nslookup queries DNS servers directly and ignores /etc/hosts. It’s useful to ask specific DNS servers for answers or to verify what DNS returns.
  • dig provides detailed DNS query results and is commonly used for troubleshooting and viewing record data.
Example nslookup:
Example dig:
If a name resolves locally via /etc/hosts but nslookup/dig returns nothing, the name is local-only and not present in DNS.

Practical tips and next steps

  • Use /etc/hosts for quick, local mappings or lab testing.
  • Use a central DNS server for scalable name management in production.
  • Make persistent resolver changes using your system’s network configuration tools (NetworkManager, systemd-resolved, DHCP client configuration, etc.).
  • Practice with dig and nslookup to inspect authoritative vs. cached DNS answers.
  • Later in this series you’ll deploy a DNS server (CoreDNS) and create records so hosts can resolve names via DNS.
Further reading and references: You can practice these commands and configurations in a lab environment on real or virtual machines. Thank you for reading.

Watch Video