Look Up MX Records with dig(1)

Recall that an MX record is a Mail eXchanger record. MX records list the hosts that should receive email for a host or a domain. A handy way to look up MX records with dig(1) is to use its +short command-line argument and pipe the result through sort(1):

% dig +short mx example.gov | sort -n
5 amx.example.gov.
5 bmx.example.gov.
5 cmx.example.gov.
100 backup1.example.gov.
100 backup2.example.gov.

Here, a +short argument limits output to just brief answers, the cost and hostnames found as MX records. The sort(1) uses -n to sort numerically, lowest through highest costs.

This example reveals a handy property of MX records. When multiple hosts share the same cost, the rule is to select randomly from among them. That is, in this example, all three of the cost 5 hosts are tried first before any of the cost 100 hosts, but the order in which the cost 5 hosts are tried is random.

Note that we discuss MX records, generally, in Set Up MX Records on page 332, covering their management and associated pitfalls. Here, we have limited our discussion to using dig(1) to look up MX records.

Get sendmail, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.