Skip to content

DNS: from names to addresses with dig

AdvancedLesson 46 of 669 min

See how a name becomes an address: ask DNS with dig for A, MX and reverse records, find out why /etc/hosts can win, and give a server a test name of your own.

Computers connect to addresses, people type names. DNS (the Domain Name System) turns one into the other: your machine asks a DNS server "what's the address of shop.example.com?" and gets a number back. The machines here (your laptop, web, db and the router) are pretend ones inside your browser, so nothing you type reaches a real network.

dig

dig asks the DNS server and shows the whole answer. +short gives just the result:

dig example.com
dig +short example.com

In the full answer, the ANSWER SECTION has the record: the name, how many seconds it may be remembered (the TTL), and the address. SERVER says which DNS server answered.

Record types

TypeHoldsAsk with
Aan IPv4 addressdig +short example.com
AAAAan IPv6 addressdig +short example.com AAAA
CNAME"this name is another name"dig +short www.example.com
MXthe mail server for a domaindig +short example.com MX
PTRthe name for an address (reverse)dig +short -x 10.0.0.20

dig @10.0.0.1 example.com asks one particular server, which helps when two servers disagree.

Where your machine looks

Programs don't ask DNS first. They read /etc/hosts, a plain list of names and addresses, then ask the DNS server named in /etc/resolv.conf:

cat /etc/hosts
cat /etc/resolv.conf

getent hosts NAME shows what programs actually get. dig skips /etc/hosts, which is why dig web finds nothing here while ping web works: web is only in /etc/hosts. On the DNS server it's web.lan.

Testing with /etc/hosts

A line in /etc/hosts beats DNS, which is handy for trying a new server before the real DNS changes. It's a system file, so it needs root. sudo echo … >> /etc/hosts fails, because the >> is done by your own shell, not by sudo. tee -a (append) run with sudo does the writing instead:

echo "10.0.0.10 staging" | sudo tee -a /etc/hosts
getent hosts staging

Your turn

Answer three questions with dig: (1) the address of shop.example.com, (2) the mail server for example.com, (3) the name of the machine at 10.0.0.20. Then (4) make the name api point to web (10.0.0.10) on your laptop, so that curl http://api/ gets the shop’s page.

Next