DNS
DNS stands for Domain name resolutions and is a system that enables the use of domains (names) to refer to nodes in a network instead of using IP addresses.
It works by establishing a record list, which maps names (domains) to IPs
In Linux, systems normally have several “levels” of DNS resolution:
DNS Configuration
Local Resolution
/etc/hosts
In all linux systems, the file /etc/hosts is consulted for local IP -> Name mapping.
This file takes priority over any external DNS. In case of any record appearing in multiple servers in the chain, the one here will have priority (by default, see below)
127.0.0.1 localhost
192.168.1.10 dev-serverThe file /etc/nsswitch.conf can be used to configure the order of resolution:
hosts: files dnsIn case of the config above, /etc/hosts will have priority over any external DNS server
Centralized Resolution
In real systems, depending on local resolution configured in each server does not scale well and becomes very difficult to maintain. A better option is to manage records in a central server, and configure all nodes in the network to consult it for any DNS queries.
Once set, if a hostname cannot be resolved locally, the system forwards the request to the DNS server.
This is traditionally controlled with the file /etc/resolv.conf which contains nameservers and search domains (gets automatically added to any queries):
nameserver 8.8.8.8
nameserver 1.1.1.1
search home.localIn modern systems is now more common use systemd-resolved to manage name resolution instead of resolv.conf.
On such systems, /etc/resolv.conf is often a symlink to a dynamically managed file (like /run/systemd/resolve/stub-resolv.conf). Manual edits to /etc/resolv.conf may be ignored or overwritten.
DNS settings can typically be configured via NetworkManager, netplan, or systemd-networkd, depending on the system setup.
DNS Records
DNS records define how domain names map to IP addresses, services, and other information. Here are the most commonly used types:
A record
- Maps a domain to an IPv4 address
- Example
example.com. IN A 93.184.216.34AAAA record
- Maps a domain to an IPv6 address
- Example
example.com. IN AAAA 2606:2800:220:1:248:1893:25c8:1946CNAME
- Creates an alias from one domain to another.
- Example:
www.example.com. IN CNAME example.com.When resolving www.example.com, the DNS server looks up example.com instead. Useful for pointing multiple subdomains to the same canonical source.
Domains
When a request such as apps.google.com is made, the DNS server will forward the query if it does not have a record for it:
- A root DNS server directs the request to a DNS server responsible for the .com domain.
- The
.comDNS server forwards the request to Google’s DNS server. - Google’s DNS server returns the IP address for the service.
Tools for DNS in Linux
Two essential tools for DNS troubleshooting and inspection on Unix-like systems are dig, nslookup. hosts. They help query DNS servers and display detailed information about how domain names are resolved.
All tools do not consider entries in etc/hosts
dig: Domain Information Groper
dig is a flexible and powerful command-line tool to perform DNS lookups. It’s the preferred tool for advanced diagnostics.
Basic Usage
dig example.comThis returns:
- A record (IPv4 address) by default
- Query time
- Authoritative/non-authoritative status
Query a Specific Record Type
dig example.com A # IPv4 address
dig example.com AAAA # IPv6 address
dig example.com MX # Mail server
dig example.com TXT # Text records
dig example.com CNAME # AliasUse a Specific DNS Server
dig @1.1.1.1 example.comShort Answer Only (for scripting or readability)
dig +short example.comnslookup: Name Server Lookup
nslookup is an older utility, still widely available and useful for quick checks.
Basic Usage
nslookup example.comReturns the default DNS server and the A record of the domain.
Query a Specific Record Type
nslookup -query=MX example.comInteractive Mode
You can launch nslookup in interactive mode:
nslookup
> set type=TXT
> example.com
> exitUse a Specific DNS Server
nslookup example.com 8.8.8.8host
The host command is a lightweight and user-friendly utility for performing DNS lookups. It’s simpler than dig and often faster for quick queries.
Basic Usage
host example.comThis returns the default A (IPv4) record(s) for the domain.
Query Specific Record Types
host -t MX example.com # Mail exchange records
host -t AAAA example.com # IPv6 address
host -t TXT example.com # Text records
host -t NS example.com # Name serversYou can also use -a for a full query (equivalent to ANY):
host -a example.comQuery a Specific DNS Server
host example.com 1.1.1.1