Skip to content

/commands/dig/dig-mx-record-lookup

dig example.com MX: look up mail server (MX) records

The commands on this page have not been executed by our verification runner yet.updated
tool
dig / nslookup
versions
BIND 9.18, BIND 9.20
os
linux, macos, windows

dig example.com MX asks DNS for the mail exchanger records of a domain: the hosts that accept email for it, each with a preference number. Add +short to print only PREFERENCE HOST pairs. Lower numbers are tried first, so 10 mx1.example.com. is preferred over 20 mx2.example.com..

bash
dig example.com MX                  # full output with header and sections
dig example.com MX +short           # e.g. "10 mx1.example.com."
dig +noall +answer example.com MX   # answer lines with TTL and class
dig @8.8.8.8 example.com MX +short  # ask a specific resolver

Reading the answer

An answer line has five fields: the owner name, the TTL in seconds, the class (IN), the type (MX) and the data. For MX the data is the preference (0 to 65535, RFC 1035) followed by the host name with a trailing dot. The trailing dot means the name is absolute; when you write the zone file yourself, forgetting it appends the zone name (mx1.example.com.example.com.).

The host names are not addresses. To check that mail can actually be delivered, resolve each target too:

bash
dig mx1.example.com A +short
dig mx1.example.com AAAA +short

An MX that points to a name without A or AAAA records cannot receive mail even though the MX lookup itself succeeds.

Variants

CommandUse
dig example.com MX +shortPreference and host only, one record per line.
dig -t MX example.comSame query with the type given as an option.
dig example.com MX +noall +answerAnswer section only, with the TTL.
dig @ns1.example.net example.com MX +norecurseAsk the authoritative server directly after a change.
dig example.com MX +short | sort -nSort by preference to see the order senders try.
dig _dmarc.example.com TXT +shortRelated mail record (DMARC policy), a TXT lookup.

Empty answers and errors

  • status: NOERROR with no answer lines means the domain exists but has no MX records; senders then fall back to the domain's own address.
  • status: NXDOMAIN means the name does not exist at all; check the spelling and the delegation (see the DNS RCODE table).
  • A timeout or connection refused points at the resolver, not at the domain: see connection timed out; no servers could be reached.

+short hides the status line, so in scripts an empty result can mean either NOERROR without data or NXDOMAIN. Run the full form when you need to tell them apart.

FAQ

Which MX record is used first?

The one with the lowest preference number. Senders try lower values first and move to higher ones when a server does not answer; records with the same value are tried in random order to spread the load.

What if a domain has no MX record?

RFC 5321 says the sender then treats the domain's A or AAAA address as an implicit MX with preference 0. A domain that wants to receive no mail at all can publish a null MX (RFC 7505), a single record with preference 0 and a dot as the host.

Can an MX record point to a CNAME?

It must not. RFC 2181 section 10.3 says the MX target has to be a name with its own A or AAAA records, not an alias. Some mail servers follow the CNAME anyway, others refuse, so delivery becomes unpredictable.

Sources