MX Lookup using command-line

1
11113
MX lookup using command-line

While trying to configure mail servers, you might want to verify if your MX records are properly setup. This article will show you how to do MX lookup using command-line interface.

MX Lookup

In case you don’t know about MX record, here is a simple explanation.

MX record is an entry defined in the DNS zone configuration to determine what mail server is responsible for handling domain’s email. For that, you should configure at least one MX record in order to receive email for your domain.

Furthermore, you can add many MX records, where each record should point to a different mail server. It is to handle load-balancing on your email infrastructure. However, it is still fine to have only one mail server.

Using nslookup utility

After adding MX record into DNS zone, you can use the nslookup utility to verify it.

The syntax of nslookup for MX lookup is very simple. It is following:

$ nslookup -q=MX DOMAIN_NAME

You need to pass one parameter, -q to specify which type of nameserver record you want to lookup. In this case, we use MX for MX lookup.

For example, if you want to lookup MX records for gmail domain, then the command would be:

$ nslookup -q=MX gmail.com

You will see similar results like following:

Non-authoritative answer:
gmail.com	mail exchanger = 30 alt3.gmail-smtp-in.l.google.com.
gmail.com	mail exchanger = 20 alt2.gmail-smtp-in.l.google.com.
gmail.com	mail exchanger = 10 alt1.gmail-smtp-in.l.google.com.
gmail.com	mail exchanger = 5 gmail-smtp-in.l.google.com.
gmail.com	mail exchanger = 40 alt4.gmail-smtp-in.l.google.com.

The last right column of the result is the MX record you’re looking for. Specifically, those are email servers.

You can also access into nslookup prompt console to make request. The steps are demonstrated in following console logs:

$ nslookup
> set type=mx
> gmail.com

Non-authoritative answer:
gmail.com	mail exchanger = 40 alt4.gmail-smtp-in.l.google.com.
gmail.com	mail exchanger = 20 alt2.gmail-smtp-in.l.google.com.
gmail.com	mail exchanger = 10 alt1.gmail-smtp-in.l.google.com.
gmail.com	mail exchanger = 5 gmail-smtp-in.l.google.com.
gmail.com	mail exchanger = 30 alt3.gmail-smtp-in.l.google.com.

> exit

To exit the prompt, just type in exit, you’ll back to the terminal.

This nslookup utility is available on most of platforms: Windows, Unix, Linux, BSD, Mac by default, so you don’t have to install it.

Using host utility

Beside nslookup, there is another utility you can use for MX lookup is host utility.

Typing the command into terminal, if it’s available you can use it. Otherwise, if you’re on Linux/Unix/BSD, you can install by your install command based on distribution.

Specifically, for Debian-based Linux, ex. Ubuntu, you can use following command:

$ apt-get install host

The syntax of host utility is as simple as nslookup.

$ host -t MX DOMAIN_NAME

For example, trying to lookup MX records for gmail domain again using `host`.

$ host -t MX gmail.com
gmail.com mail is handled by 5 gmail-smtp-in.l.google.com.
gmail.com mail is handled by 10 alt1.gmail-smtp-in.l.google.com.
gmail.com mail is handled by 20 alt2.gmail-smtp-in.l.google.com.
gmail.com mail is handled by 30 alt3.gmail-smtp-in.l.google.com.
gmail.com mail is handled by 40 alt4.gmail-smtp-in.l.google.com.

The result should be the same as using nslookup.

Using dig utility

Another utility that is very useful for MX lookup is dig. Just make sure it is available on your system.

The way to use dig is no difference from host and nslookup at all. However, dig provides a pretty much verbose output for nameserver record query.

$ dig -t mx gmail.com

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

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

;; ANSWER SECTION:
gmail.com.		2104	IN	MX	40 alt4.gmail-smtp-in.l.google.com.
gmail.com.		2104	IN	MX	5 gmail-smtp-in.l.google.com.
gmail.com.		2104	IN	MX	20 alt2.gmail-smtp-in.l.google.com.
gmail.com.		2104	IN	MX	10 alt1.gmail-smtp-in.l.google.com.
gmail.com.		2104	IN	MX	30 alt3.gmail-smtp-in.l.google.com.

;; Query time: 35 msec
;; SERVER: [REMOVED]
;; WHEN: Wed Jan 09 15:38:03 +07 2019
;; MSG SIZE  rcvd: 161

Look pretty much details. However, you can filter to make it output only what we need. Here it is

$ dig +short mx gmail.com
5 gmail-smtp-in.l.google.com.
10 alt1.gmail-smtp-in.l.google.com.
40 alt4.gmail-smtp-in.l.google.com.
30 alt3.gmail-smtp-in.l.google.com.
20 alt2.gmail-smtp-in.l.google.com.

Summary

MX lookup is an easy task and should be well-handled if you’re in charge of mail server configuration or DevOps.

So far, nslookup is more commonly used than host and dig utility, because it’s available by default on many systems.

If you know any other methods for MX lookup, please share with me in comments. I’d love to know !