MX Lookup using command-line

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

You will see similar results like following:

Non-authoritative answer:	mail exchanger = 30	mail exchanger = 20	mail exchanger = 10	mail exchanger = 5	mail exchanger = 40

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

Non-authoritative answer:	mail exchanger = 40	mail exchanger = 20	mail exchanger = 10	mail exchanger = 5	mail exchanger = 30

> 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 mail is handled by 5 mail is handled by 10 mail is handled by 20 mail is handled by 30 mail is handled by 40

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

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

; EDNS: version: 0, flags:; udp: 512
;			IN	MX

;; ANSWER SECTION:		2104	IN	MX	40		2104	IN	MX	5		2104	IN	MX	20		2104	IN	MX	10		2104	IN	MX	30

;; Query time: 35 msec
;; 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


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 !