Understanding DNS MX record with Examples

DNS MX record is short for mail exchange. It is used to direct email to a mail server. The MX record indicates how email messages should be routed when we send out an email.


DNS MX record was first defined in RFC 1035.


DNS MX record must always point to a domain name.



Example of DNS MX record


% dig google.com mx +answer

; <<>> DiG 9.10.6 <<>> google.com mx +answer
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 59938
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 13, ADDITIONAL: 3

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1280
;; QUESTION SECTION:
;google.com.			IN	MX

;; ANSWER SECTION:
google.com.		0	IN	MX	20 alt1.aspmx.l.google.com.
google.com.		0	IN	MX	10 aspmx.l.google.com.
google.com.		0	IN	MX	30 alt2.aspmx.l.google.com.
google.com.		0	IN	MX	40 alt3.aspmx.l.google.com.
google.com.		0	IN	MX	50 alt4.aspmx.l.google.com.

In the answer section, there are 5 MX records for the domain google.com. 10.20.30.40.50 are the priorities for these MX records.


The 'priority' numbers before the domains for these MX records indicate preference; the lower 'priority' value is preferred. The server will always try aspmx.l.google.com. first because 10 is lower than 20. In a result of a message send failure, the server will default to others.


This is also sometimes described as distance, which does make it somewhat more intuitive if we think in terms of mail server preferring to attempt delivery to the shortest-distanced MX handler.




Purpose of DNS MX record

In simple words, an MX record is used to tell the world which mail servers accept incoming mail for the domain and where emails sent to the domain should be routed to. If the MX records are not pointed to the correct location, we might not receive email.


For example, if we send an email from hello@yahoo.com to hello@gmail.com, the yahoo mail server will try to send that email to the above servers based on the priority.


If there is no MX record present, the protocol is to try to deliver the email to the A or AAAA record in the destination. For example, an email is sent to hello@howtouselinux.dom but howtouselinux.dom has no MX records. The email server will then do an A-record lookup for howtouselinux.dom and attempt delivery there.





MX record point to a domain name

DNS MX record must point to another host or to the original host. It is illegal for an MX record to point directly to an A or AAAA record.

hostA    IN      MX 0 hostA
hostA    IN      MX 0 192.168.1.1   <-illegal

This line says that mail for hostA will be delivered to hostA.



MX record point to multiple domain names

It is possible to configure several MX records for a domain, typically pointing to an array of mail servers for load balancing and redundancy.


We can understand this from the above google.com MX records.


MX record point to different domains


Usually, MX records point to hosts inside the same domain. Therefore, managing them does not require the cooperation of others. But it is legal for MX records to point to hosts in different domains.


hostA  IN     MX  10 hostB  


MX record point to a domain that has A or AAAA record

An MX record must point to a hostname that has an A or AAAA record. To illustrate, consider the following:


hostA  IN     MX  10 hostB       ←illegal
       IN     MX  20 hostC
hostB  IN     MX  10 hostC
hostC  IN     A   192.168.1.2

Note that hostB lacks an A record but hostC has one. It is illegal to point an MX record at a host that lacks an A or AAAA record. Therefore, the first line in the preceding example is illegal, whereas the second line is legal.




DNS MX record Point to CNAME

A CNAME record is used for referencing a domain's alias instead of its actual name. CNAME records typically point to an A record (in IPv4) or AAAA record (in IPv6) for that domain. Pointing to a CNAME is forbidden by the RFC documents that define how MX records function.


hostA    IN     MX  10 hostB  ←illegal
hostB  IN     CNAME  hostc
hostc   IN     A 123.45.67.89



Check MX record with dig command

We can use dig name + record type + @dns server to query the DNS info from a DNS server. By default, dig performs a lookup for an A record if no type argument is specified.


For Mx record, we can use this command to get it. - dig domain mx.

Here is an example.


% dig google.com mx +answer

; <<>> DiG 9.10.6 <<>> google.com mx +answer
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 59938
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 13, ADDITIONAL: 3

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1280
;; QUESTION SECTION:
;google.com.			IN	MX

;; ANSWER SECTION:
google.com.		0	IN	MX	20 alt1.aspmx.l.google.com.
google.com.		0	IN	MX	10 aspmx.l.google.com.
google.com.		0	IN	MX	30 alt2.aspmx.l.google.com.
google.com.		0	IN	MX	40 alt3.aspmx.l.google.com.
google.com.		0	IN	MX	50 alt4.aspmx.l.google.com.


Related:






33 views
屏幕快照 2021-08-08 下午5.16.32.png