How to Install and Use dig and nslookup Commands in Linux

The dig and nslookup are network administration command-line tools. They are both helpful for network troubleshooting and gathering information related to Domain Name Server (DNS).

The “Domain Information Groper” (dig) command is an essential tool for gathering information or interrogating the DNS name servers to troubleshoot DNS issues.

The “Name Server Lookup” (nslookup) command is a network administration command-line tool for querying and obtaining crucial mapping information between a domain name and an IP address.

Most of the modern Linux operating systems ship dig and nslookup commands, but older ones might be left out of the responsibility of installation to you.

Today, you will learn how to install and use the dig and nslookup commands in Linux.

How to Install dig & nslookup in Linux

The dig and nslookup tools are part of the bind-utils package. Installing it will let you have both of the tools in your system.

How to Install dig & nslookup in Debian/Ubuntu

You can easily install dig & nslookup on your Debian-based distribution system using the apt package manager, as shown below.

$ sudo apt install dnsutils

Below is the output of the above command.

How to install dig & nslookup in Debian-Ubuntu
How to install dig & nslookup in Debian-Ubuntu

How to Install dig & nslookup in RHEL/Fedora

Both commands can be easily installed on RHEL-based distributions using the DNF command, as shown below.

$ sudo dnf install bind-utils

Below is the output of the above command.

How to install dig & nslookup in RHEL-Fedora
How to install dig & nslookup in RHEL-Fedora

How to Install dig & nslookup in Arch/Manjaro

For arch-based distributions, the user can install it using the default pacman package manager, as shown below.

$ sudo pacman -Sy dnsutils

Below is the output of the above command.

How to install dig & nslookup in Arch-Manjaro
How to install dig & nslookup in Arch-Manjaro

Testing dig & nslookup commands are working

After the installation is finished, you can verify whether the package was successfully installed or not by running the below command.

$ dig -v

Below is the output of the above command.

Testing dig & nslookup commands are working
Testing dig & nslookup commands are working

If the package is successfully installed, you will get the above output with the current version of the package without any errors such as “Command ‘dig’ not found“.

How to use the dig command

The dig syntax consists of the hostname/IP address, name, and record as follows:

$ dig [HOSTNAME] [NAME] [RECORD]
  • Hostname consists of the domain name or IP address of the server.
  • Name is DNS (Domain Name Server) of the server to query.
  • Type is the DNS record to retrieve (default A is retrieved if not specified). Other known records are A, MX, TXT, CNAME, and NS.

The usage is pretty simple. We will start by understanding this command’s output by querying my domain’s DNS name server, as shown below.

$ dig trendoceans.com

As shown below, you can replace the domain name with something else for which you want to know the information related to the DNS name server.

dig command output
dig command output
  • The header consists of information related to the dig version, status, id, and other additional information.
  • The question area consists of the query you request while executing the dig command.
  • The answer section is the important part where you can see the information about the query you have asked.
  • The last footer area consists of the information, date, and message size.

If you are just interested in querying the A record (default), add +short at the end of the command, as shown below.

$ dig trendoceans.com +short
OR
$ dig trendoceans.com A +short

Below is the output of the above command.

More specific dig command output
More specific dig command output

Execute the below command to output the MX record for the domain.

$ dig trendoceans.com MX +short

Below is the output of the above command.

Finding the MX record of the domain
Finding the MX record of the domain

Execute the below command to output the specified domain’s Name Server (NS).

$ dig trendoceans.com NS +short

Below is the output of the above command.

Finding the NS record of the domain
Finding the NS record of the domain

By default, dig commands will query the name servers listed in /etc/resolv.conf to perform a DNS lookup for you. If you want to change the default behaviour, specify the hostname or IP address of the name server after the @ symbol.

The below command will send the DNS query to the Google name server.

$ dig @8.8.8.8 trendoceans.com
dig it will ferrell

How to use the nslookup command

You can use the nslookup command to query the name server for various domain records, as shown below.

$ nslookup trendoceans.com

Below is the output of the above command.

How to use nslookup command
How to use the nslookup command

The rest of the commands are easy to follow, and you can find them in the below list of examples.

Examples of dig & nslookup commands

Get a detailed answer for the given domain name (A records):

$ dig +noall +answer [example.com]        # Using dig command
$ nslookup [example.com]                  # Using nslookup command

Query a specific DNS record type for the given domain name:

$ dig [example.com] [A|MX|TXT|CNAME|NS] +short              # Using dig command
$ nslookup -type=[A|MX|TXT|CNAME|NS] [example.com]          # Using nslookup command

Display all the records for the given domain name:

$ dig [example.com] ANY                                     # Using dig command
$ nslookup -vc -type=ANY [example.com]                      # Using nslookup command

Specify a custom name server instead of the default for the given domain name (example, Google DNS):

$ dig @8.8.8.8 [example.com]                                # Using dig command
$ nslookup [example.com] 8.8.8.8                            # Using nslookup command

Find authoritative name servers for the zone and display SOA records for the given domain name:

$ dig +nssearch [example.com]

Perform the entire trace path to resolve a domain name:

$ dig +trace [example.com]

The query for a mail server (MX record) in the domain, showing details of the transaction:

$ nslookup -type=MX -debug [example.com]

Query a given name server on a specific port number for a TXT record of the domain:

$ nslookup -port=[port_number] -type=TXT [example.com] [name_server]

That’s all for now. If you want something more, let us know in the comment section.

Leave a Reply