Business tech · Beginner · 14 min
Check a domain's MX, SPF, DKIM and DMARC records with dig
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · dig 9.20 (bind9-dnsutils)
You will use dig to read your domain's MX, SPF, DKIM and DMARC records, spot the mistakes that send mail to spam, and keep a small script that prints a pass or fix line for each check.
On this page
Four kinds of DNS record decide whether your email arrives and whether anyone else can send mail in your name. MX says which servers accept mail for the domain. SPF lists the servers allowed to send it. DKIM publishes the public key that checks each message's signature. DMARC tells receiving servers what to do when those checks fail, and where to send reports.
Mail providers give you these values when you set up email, and they are easy to get subtly wrong: a second SPF record added by a newsletter tool, a DKIM key pasted into the wrong name, a DMARC policy left at none for years. None of these show up as an error in your DNS dashboard. They show up later, as invoices landing in a client's spam folder.
This tutorial is the audit, not the setup. You query each record with dig, read the answer line by line, and finish with a short script that prints PASS or FIX for each check. To publish or change the records themselves, follow set up SPF, DKIM and DMARC.
The examples come from Ember Studio, a seven-person design agency with staff in Lagos and Nairobi. Its main domain, emberstudio.test, is set up well. Its older domain, ember-agency.test, still has the mistakes you will learn to spot. Your answers will show your own domain and provider.
Step 1 · Install dig and name your domain
dig sends one DNS question and prints the answer exactly as the server gave it, which makes it better for auditing than a website that tidies the result. On Ubuntu it comes in the bind9-dnsutils package. macOS already includes it. On Windows, use it inside WSL with the same Ubuntu command.
sudo apt update
sudo apt install -y bind9-dnsutils
mkdir -p ~/email-dns-demo
cd ~/email-dns-demoStore your domain in a shell variable so every command below can be pasted as it is. Replace example.com with your own domain, without www.:
DOMAIN=example.comCheck it worked
dig -v
echo "$DOMAIN"The first line shows a version such as DiG 9.20.24-1ubuntu0.5-Ubuntu (macOS ships an older 9.x, which works the same for everything here). The second line is your domain.
Step 2 · Read the MX records
Ask for the mail exchangers:
dig +short MX "$DOMAIN"For Ember Studio the answer is:
1 mx1.mailhost.test.
5 mx2.mailhost.test.Each line is a preference number and a host name. Sending servers try the lowest number first and fall back to higher ones, so 1 is the main server and 5 the backup. Equal numbers share the load. The trailing dot means the name is complete.
To see how long resolvers may cache the answer, ask for the full answer section:
dig MX "$DOMAIN" +noall +answerThe second column is the TTL in seconds. After you change a record, old answers can stay in caches for up to that long.
Look for three things. Every host name should be the one your mail provider's setup page lists. The target must be a name, never an IP address. And that name must have its own A or AAAA record rather than being a CNAME, which RFC 2181 forbids for MX targets. Check a target with dig +short A <name>.
Check it worked
You can name your provider from the MX host names, and they match the values in its setup guide. If the answer is empty, your domain accepts no mail at all.
Step 3 · Check the SPF record
SPF lives in a TXT record at the domain itself, alongside other TXT records such as site-verification tokens:
dig +short TXT "$DOMAIN""v=spf1 include:_spf.mailhost.test ~all"
"site-verification=ember-4f7a21"Only the line starting v=spf1 is SPF. Read it left to right:
include:_spf.mailhost.testhands the decision to the provider's own SPF record. Eachinclude,a,mx,ptr,existsandredirectcosts a DNS lookup, and the total, including the lookups inside every include, must stay at 10 or under. Past 10, the whole record fails.~allat the end covers every other server:~allmeans "probably not us, treat with suspicion",-allmeans "not us, reject". Either is fine once DMARC is in place.?all(no opinion) and+all(anyone may send) protect nothing.
There must be exactly one v=spf1 record. Two records make SPF fail for every message, even if each looks correct on its own.
To count the nested lookups, follow each include:
dig +short TXT "$DOMAIN" | grep -o 'include:[^ "]*' | cut -d: -f2 | while read -r inc; do echo "== $inc"; dig +short TXT "$inc"; doneEmber Studio's include points at v=spf1 ip4:192.0.2.0/24 ~all, which has no further lookups, so its total is 1. Large providers often nest two or three levels deep, so repeat the command on any include you find inside an include.
Check it worked
You have exactly one v=spf1 line, it ends in ~all or -all, and you have written down a lookup total of 10 or less.
Step 4 · Find your DKIM selector and read the key
A DKIM key is published at <selector>._domainkey.<domain>, and nothing in DNS lists the selectors in use. The reliable way to find yours is to read the signature on a message your domain actually sent.
Open an email sent from your domain, find the option that shows the original message or its source (in Gmail it is "Show original", which also has a download link), and save it as message.eml in ~/email-dns-demo. Then pull out the domain (d=) and selector (s=) from each signature:
grep -iA6 '^DKIM-Signature:' message.eml | grep -oE '(^|[[:space:];])[ds]=[^;]+' | tr -d ' \t;'d=emberstudio.test
s=mh1A message can carry more than one signature, for example one from your mail provider and one from a newsletter service. Use the selector whose d= is your own domain. Google Workspace uses google unless you chose another prefix. Store it, replacing mh1 with yours:
SELECTOR=mh1Now fetch the key:
dig +short TXT "$SELECTOR._domainkey.$DOMAIN"The answer starts with "v=DKIM1; k=rsa; " and continues with a long p= value split across two or three quoted strings. That split is normal: a single TXT string holds at most 255 characters, and receivers join the pieces without spaces. Problems to look for: an empty answer (the key is missing or under a different name), p= with nothing after it (the key has been revoked), or stray spaces or quotes inside the p= value from a bad paste.
Check it worked
You have a selector from a real message, and the dig answer for it contains v=DKIM1 and a long, unbroken p= value.
Step 5 · Read the DMARC record
DMARC sits at a fixed name, _dmarc in front of your domain:
dig +short TXT "_dmarc.$DOMAIN""v=DMARC1; p=quarantine; rua=mailto:dmarc@emberstudio.test; adkim=r; aspf=r"The tags that matter most:
p=is the policy for mail that fails both SPF and DKIM alignment.noneonly collects reports,quarantineasks receivers to treat it as spam, andrejectasks them to refuse it.rua=is where receivers send daily aggregate reports. Without it you are enforcing a policy blind. If the address is on another domain, that domain must publish a record allowing it.sp=sets a different policy for subdomains. If it is missing, subdomains followp=.adkim=andaspf=set alignment:r(relaxed, the default) letsmail.emberstudio.testcount foremberstudio.test, whiles(strict) demands an exact match.
Check it worked
You get exactly one record starting v=DMARC1, and you can say in one sentence what your policy is and where its reports go.
Step 6 · Spot the usual mistakes
Here is what the same commands return for Ember Studio's older domain, ember-agency.test:
$ dig +short TXT ember-agency.test
"v=spf1 include:_spf.mailhost.test ~all"
"v=spf1 a mx include:_spf.mailhost.test include:bulk.mailhost.test ?all"
$ dig +short TXT mh1._domainkey.ember-agency.test
$ dig +short TXT _dmarc.ember-agency.test
"v=DMARC1; p=none"Four faults are visible in six lines:
- Two SPF records. Someone added a second record for a newsletter tool instead of editing the first. Both now fail. The fix is one merged record:
v=spf1 mx include:_spf.mailhost.test include:bulk.mailhost.test ~all. The old record'sais dropped becauseember-agency.testhas no A record of its own, so it spent a lookup and matched nothing. Keepaonly when the server at the bare domain's address really sends mail. ?allin the second record. Even after the merge, end with~allor-all.- No DKIM key at the selector the mail carries. Signatures cannot be checked, so DMARC can only rely on SPF.
- DMARC stuck at
p=nonewith norua=. Nothing is enforced and nobody gets reports. Add a reporting address first, read a few weeks of reports, then move toquarantine.
Other faults worth a glance: an MX pointing at an IP address or a CNAME, an SPF record over the 10-lookup limit, and a DMARC record published at the domain itself instead of at _dmarc.
Check it worked
Go back to your own answers from the earlier steps and mark each of the faults above as present or absent. You should have a written list, even if every line says "absent".
Step 7 · Rule out a stale cache
If you have just changed a record and dig still shows the old value, you may be reading a cached answer. Ask your domain's own name server, which always has the current version:
dig +short NS "$DOMAIN"
NS1=$(dig +short NS "$DOMAIN" | awk 'NR==1')
dig @"$NS1" +short TXT "_dmarc.$DOMAIN"If the name server shows the new value and your normal resolver does not, wait for the TTL you saw in the MX step to pass. You can also compare with a public resolver, for example dig @1.1.1.1 +short TXT "_dmarc.$DOMAIN". Some ISP and mobile-network resolvers ignore low TTLs and keep answers for longer, so a phone on mobile data can see the old record after your laptop sees the new one. Querying the name server directly avoids the wait, and each dig query uses well under a kilobyte of data, which matters on prepaid bundles.
Check it worked
The name server's answer matches the record you see in your DNS dashboard.
Step 8 · Keep a pass or fix checklist script
Put the checks into one script so you can rerun the audit after any change, or on a client's domain. It needs only dig and standard shell tools, and works on Ubuntu and macOS:
cat > check-email-dns.sh <<'SH'
#!/usr/bin/env bash
# Usage: ./check-email-dns.sh <domain> [dkim-selector]
set -u
domain="${1:?usage: check-email-dns.sh <domain> [dkim-selector]}"
selector="${2:-}"
problems=0
pass() { printf 'PASS %s\n' "$*"; }
fix() { printf 'FIX %s\n' "$*"; problems=$((problems + 1)); }
txt() { dig +short TXT "$1" | sed 's/" "//g; s/"//g'; }
mx=$(dig +short MX "$domain")
if [ -n "$mx" ]; then
pass "MX: $(printf '%s\n' "$mx" | wc -l | tr -d ' ') record(s)"
else
fix "MX: none found, so the domain receives no mail"
fi
spf=$(txt "$domain" | grep -i '^v=spf1' || true)
spf_count=$(printf '%s' "$spf" | grep -c . || true)
if [ "$spf_count" -eq 0 ]; then
fix "SPF: no v=spf1 record"
elif [ "$spf_count" -gt 1 ]; then
fix "SPF: $spf_count records found; merge them into one"
else
pass "SPF: one record"
case "$spf" in
*" -all"|*" ~all") pass "SPF: ends in ${spf##* }" ;;
*) fix "SPF: does not end in -all or ~all" ;;
esac
lookups=$(printf '%s\n' "$spf" | tr ' ' '\n' | grep -ciE '^[+?~-]?(include:|a$|a[:/]|mx$|mx[:/]|ptr|exists:)|^redirect=' || true)
if [ "$lookups" -le 10 ]; then
pass "SPF: $lookups top-level lookup(s); add the lookups inside each include"
else
fix "SPF: $lookups lookups, over the limit of 10"
fi
fi
if [ -z "$selector" ]; then
printf 'SKIP DKIM: pass the selector as the second argument\n'
else
dkim=$(txt "$selector._domainkey.$domain")
if printf '%s' "$dkim" | grep -q 'p=[A-Za-z0-9+/]'; then
pass "DKIM: key found at $selector._domainkey.$domain"
elif [ -n "$dkim" ]; then
fix "DKIM: record at $selector._domainkey.$domain has an empty p= (revoked)"
else
fix "DKIM: no key at $selector._domainkey.$domain"
fi
fi
dmarc=$(txt "_dmarc.$domain" | grep -i '^v=DMARC1' || true)
if [ -z "$dmarc" ]; then
fix "DMARC: no record at _dmarc.$domain"
else
tags=$(printf '%s' "$dmarc" | tr ';' '\n' | sed 's/^ *//')
policy=$(printf '%s\n' "$tags" | grep -i '^p=' | cut -d= -f2-)
rua=$(printf '%s\n' "$tags" | grep -i '^rua=' | cut -d= -f2-)
case "$policy" in
quarantine|reject) pass "DMARC: policy is p=$policy" ;;
*) fix "DMARC: policy is p=${policy:-missing}, which only monitors" ;;
esac
if [ -n "$rua" ]; then pass "DMARC: reports go to $rua"; else fix "DMARC: no rua= address, so you get no reports"; fi
fi
echo "$problems item(s) to fix"
[ "$problems" -eq 0 ]
SH
chmod +x check-email-dns.sh
./check-email-dns.sh "$DOMAIN" "$SELECTOR"For emberstudio.test every line passes:
PASS MX: 2 record(s)
PASS SPF: one record
PASS SPF: ends in ~all
PASS SPF: 1 top-level lookup(s); add the lookups inside each include
PASS DKIM: key found at mh1._domainkey.emberstudio.test
PASS DMARC: policy is p=quarantine
PASS DMARC: reports go to mailto:dmarc@emberstudio.test
0 item(s) to fixFor ember-agency.test with selector mh1 it prints:
PASS MX: 1 record(s)
FIX SPF: 2 records found; merge them into one
FIX DKIM: no key at mh1._domainkey.ember-agency.test
FIX DMARC: policy is p=none, which only monitors
FIX DMARC: no rua= address, so you get no reports
4 item(s) to fixThe script exits with a non-zero status when anything needs fixing, so you can run it from a scheduled job and be alerted. It counts SPF lookups only at the top level, so keep following includes by hand as in the SPF step. A PASS means the record is shaped correctly, not that the values are the ones your provider gave you: compare those with its setup page yourself.
Check it worked
The script prints one line per check for your domain and a final count. Every FIX line matches a fault you found by hand in the earlier steps.
Step 9 · Clean up
Keep check-email-dns.sh somewhere you will find it again if you want to rerun the audit, then remove the demo folder, which also holds the saved message with its headers:
cd ~
rm -rf ~/email-dns-demoCheck it worked
ls ~/email-dns-demo reports that the folder does not exist.
Next steps
- Fix what the audit found: set up SPF, DKIM and DMARC covers publishing each record and moving DMARC from
nonetoreject. - Brush up on the record types themselves: DNS records explained: A, CNAME, MX and TXT.
- Starting from scratch on a new domain: set up business email on your own domain.
Related tutorials
- How to Set Up SPF, DKIM and DMARC So Your Email Is Not Marked as SpamYou will publish the three DNS records that prove your email is really from your domain, SPF, DKIM and DMARC, check each with dig, stay under SPF's 10-lookup limit, use 2048-bit DKIM keys, and move DMARC safely from p=none to quarantine to reject using its reports.
- DNS Records Explained: A, AAAA, CNAME, MX and TXT, with dig ExamplesYou will learn what the common DNS record types do and read each one with dig, using a small practice DNS server you start yourself.
- Set up business email on your own domainYou will move a small team from personal Gmail addresses to working addresses on its own domain, with shared hello@ and accounts@ addresses, aliases, and a written list of every DNS record you added.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials