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. The best way is to check dns MX record in Linux is using the dig command. For example: dig domain.com mx will output all the MX records for domain.com.
- DNS MX record was first defined in RFC 1035.
- DNS MX record must always point to a domain name.
- DNS MX record can not point to a IP address
- DNS MX record can not point to a domain which doesn’t have A record
- DNS MX record can not point to a Cname
- DNS MX record can point to a different domain
- DNS MX record supports multiple DNS records
Table of Contents
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.
When someone sends an email to an address at the domain “example.com,” their email server performs a DNS lookup for the MX records of “example.com.” Based on the priorities specified in the MX records, the sending server attempts to deliver the email to the mail server with the lowest priority. If the first-choice mail server is unreachable or unavailable, the sender’s server falls back to the mail server with the next highest 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 [email protected] but howtouselinux.dom has no MX records. The email server will then do an A-record lookup for www.howtouselinux.com and attempt delivery there.
MX records play a crucial role in email delivery, allowing proper routing of messages across the internet and ensuring they reach the intended recipients’ mail servers.
Example of DNS MX record
An MX record consists of two main components:
Each MX record has a priority value assigned to it, indicating the order in which the mail servers should be used. The priority is represented by a numerical value, where a lower number indicates a higher priority. Email servers attempt to deliver messages to the server with the lowest priority value first. If that server is unavailable, the next server with a higher priority is tried.
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.
Mail Server Address:
The mail server address associated with an MX record is the domain name or IP address of the mail server responsible for receiving email messages for the domain.
Let’s consider the domain “example.com” and its corresponding MX records:
example.com. MX 10 mail.example.com.
example.com. MX 20 backup-mail.example.com.
In this example, the domain “example.com” has two MX records.
MX Record 1:
Mail Server Address: mail.example.com
MX Record 2:
Mail Server Address: backup-mail.example.com
The MX records indicate that email servers should first attempt to deliver messages to the mail server “mail.example.com” with a priority of 10. If that server is unavailable, the backup mail server “backup-mail.example.com” with a priority of 20 should be used.
Check MX record with dig command in Linux
The best way to check MX record in Linux is using dig command. Open the terminal and type dig domain name mx. It will return all the MX records of this domain. For example, dig example.com mx will output the MX records for domain example.com.
Here is an example.
% dig google.com mx
; <<>> 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.
By default, dig performs a lookup for an A record if no type argument is specified.
Check MX record with nslookup Command in Linux
nslookup (from name server lookup) is a network administration command-line tool for querying the Domain Name System (DNS) to obtain the mapping between domain name and IP address, or other DNS records.
- To check MX records using the nslookup command in Linux, you can follow these steps:
- Open a terminal on your Linux system.
- Type nslookup followed by a space.
- Set the query type to MX by typing set type=MX.
- Enter the domain name for which you want to check the MX records. For example, to check the MX records for “example.com”, type example.com.
- Press Enter to execute the command.
% nslookup -debug -type=mx google.com
google.com, type = MX, class = IN
mail exchanger = 10 aspmx.l.google.com.
ttl = 0
mail exchanger = 40 alt3.aspmx.l.google.com.
ttl = 0
mail exchanger = 50 alt4.aspmx.l.google.com.
ttl = 0
MX record points to a domain name
MX records can point to a domain name instead of directly specifying an IP address. This allows for flexibility in managing email infrastructure and allows the domain owner to change the underlying mail server’s IP address without modifying the MX records.
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 points 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.
By specifying multiple MX records, you can distribute email delivery across different mail servers or provide redundancy in case of server failures. Email servers attempting to deliver messages to the domain “example.com” will follow the priorities specified in the MX records and attempt to deliver to the servers in the order of their priority.
This approach ensures that if one mail server is unavailable or overloaded, the email can be routed to an alternative server, providing better reliability and fault tolerance in email delivery for the domain.
MX record points to different domains
It is common for organizations to configure MX records to specify different domains for handling email delivery. This can provide flexibility in managing email infrastructure and allow delegation of email handling to specialized mail servers or third-party email services. Here’s an example:
Suppose we have a domain called “example.com,” and we want to configure MX records to handle email delivery through different domains:
example.com. MX 10 mail.example.net.
example.com. MX 20 smtp.example.org.
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 points 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
hostA 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 Points 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 184.108.40.206
- Free DNS Servers in 2021
- Flush DNS Cache with Command Quick Guide
- Exploring DNS TTL with Examples
- Understanding Linux Dig Command
- Exploring EDNS with Examples
- Best and Fastest DNS Server For PS4 PS5
- Best and Fastest DNS Servers For Gaming
- What is DNS? DNS Meaning
- Query DNS Txt Record with Dig Command