Querying DNS records with dig
- Version
- unversioned
- Review
- Draft
Summary#
dig sends a DNS query and displays the server's reply. Use @server to query a particular resolver or authoritative server and compare its result with the default resolver.
Syntax#
dig example.net. A
dig @"$resolver" example.net. MX
dig @"$resolver" -p 15353 example.net. AAAA
Use A for IPv4 addresses, AAAA for IPv6, MX for mail exchangers and TXT for text records. dig defaults to A. Without @server it normally uses the servers in /etc/resolv.conf. Set resolver to the server's address and use -p when it listens on a port other than 53.
Read status: in the header, then check the answer count and the records under ANSWER SECTION. An answer row contains the owner name, TTL in seconds, class, type and record data. NXDOMAIN reports a missing name. SERVFAIL means the server could not complete the query. NOERROR with zero answers can mean the name exists without a record of the requested type, as in the example.
Example#
Run this in a fresh disposable Linux container with BIND's dig and named installed and port 15353 free. The script starts an authoritative server on loopback, loads a local zone and compares three replies. It downloads nothing.
(
set -eu
export LC_ALL=C
work=$(mktemp -d /tmp/devlookup-dig.XXXXXXXX)
pid=
cleanup() {
if [ -n "$pid" ]; then
kill "$pid" 2>/dev/null || :
wait "$pid" 2>/dev/null || :
fi
rm -rf -- "$work"
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
cat > "$work/named.conf" <<EOF
options {
directory "$work";
listen-on port 15353 { 127.0.0.1; };
listen-on-v6 { none; };
recursion no;
dnssec-validation no;
pid-file "named.pid";
};
controls {};
zone "workshop.test" {
type primary;
file "workshop.zone";
};
EOF
cat > "$work/workshop.zone" <<'ZONE'
$TTL 300
@ IN SOA ns hostmaster (1 3600 600 86400 300)
@ IN NS ns
ns IN A 192.0.2.53
api IN A 192.0.2.21
ZONE
named -c "$work/named.conf" -g -n 1 > "$work/log" 2>&1 &
pid=$!
query() {
dig -r @127.0.0.1 -p 15353 "$1" "$2" \
+norecurse +noall +comments +answer +time=1 +tries=1
}
attempts=0
until query api.workshop.test. A > "$work/reply" &&
grep -Fq '192.0.2.21' "$work/reply"
do
attempts=$((attempts + 1))
if [ "$attempts" -ge 10 ] || ! kill -0 "$pid" 2>/dev/null; then
cat "$work/log" >&2
exit 1
fi
sleep 0.1
done
show_reply() {
status=0
query "$1" "$2" > "$work/reply" || status=$?
printf '%s %s (dig exit %s)\n' "$1" "$2" "$status"
[ "$status" -eq 0 ] || exit "$status"
awk '
/^;; ->>HEADER<<-/ { sub(/,$/, "", $6); print $5, $6 }
/^;; flags:/ {
for (i = 1; i < NF; i++)
if ($i == "ANSWER:") print "answers:", $(i + 1) + 0
}
$4 == "A" { print $1, $2, $3, $4, $5 }
' "$work/reply"
}
show_reply api.workshop.test. A
show_reply api.workshop.test. AAAA
show_reply missing.workshop.test. A
)
Expected output#
The example prints the DNS status, answer count and A record without the changing query IDs and timings.
api.workshop.test. A (dig exit 0)
status: NOERROR
answers: 1
api.workshop.test. 300 IN A 192.0.2.21
api.workshop.test. AAAA (dig exit 0)
status: NOERROR
answers: 0
missing.workshop.test. A (dig exit 0)
status: NXDOMAIN
answers: 0
Common mistakes#
NXDOMAIN means that the queried name does not exist. Shell exit status 0 can accompany it because the server returned a DNS reply. Check the DNS status and answer before treating the lookup as successful. Use full output during diagnosis because +short hides the header. For a script that needs consistent output options, -r skips the user's .digrc.
Version applicability#
This entry uses ISC BIND 9. The example uses the type primary zone syntax introduced in BIND 9.12.0. Both dig and named were verified at 9.20.24.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0