
Dig Command in Linux (With Examples)
The `dig` command, which stands for “Domain Information Groper,” is a versatile and powerful tool designed for DNS (Domain Name System) troubleshooting and querying. It facilitates the retrieval of DNS records, performs DNS lookups, and aids in diagnosing DNS-related issues.
In this article, we will present you with a step-by-step guide on how to install and utilize the `dig` command in Linux distribution.
Updating System Packages
Start by updating the package lists for upgrades and new installations. Open a terminal by pressing ‘Ctrl+Alt+T’ and execute the following command:
$ sudo apt update
Installing the `dnsutils` Package
In most Linux distributions, this package is installed by default. If it is not installed then, to use the `dig` command, we need to install the `dnsutils` package. Execute the following command:
$ sudo apt install dnsutils
Ensure that the installation of the `dig` command was successful by opening a terminal and typing:
$ dig –v
A `dig` command prompt should appear, indicating a successful installation.
Basic Usage of Dig command with Examples
Below, we have mentioned basic use of the Dig command in Linux:
Check Simple DNS Lookup
Perform a basic DNS lookup using the following syntax:
$ dig linuxgenie.com
Replace this domain with the desired domain name. This command will display information related to the domain, including its associated IP address.
Querying a Specific DNS Record Type
You can specify the type of DNS record to retrieve. For instance, to obtain the MX (Mail Exchange) records of a domain, use the command:
$ dig MX linuxgenie.com
This will display the mail server information associated with the domain.
Querying a Specific DNS Server
To query a specific DNS server, utilize the `@` symbol followed by the IP address of the DNS server. For example, to query Google’s public DNS server (8.8.8.8) for a domain’s A (IPv4) records, employ the following command:
$ dig @8.8.8.8 A linuxgenie.com
This command will retrieve the IPv4 address associated with the domain by querying the specified DNS server.
Retrieve Short answers
To retrieve only the IP address linked to a domain name, you can utilize the following command:
$ dig google.com +short
Executing this command will generate a concise output, displaying the IP address associated with the domain name.
Retrieve detailed answers
If you want to obtain detailed information in the answers section, you can use the following command:
$ dig google.com +noall +answer
Executing this command with the +noall +answer options will provide you with a comprehensive output, displaying detailed information about the domain.
Use of ANY to retrieve all Results
If you wish to retrieve all the results of the DNS query, you can utilize the following command:
$ dig linuxgenie.com ANY
Executing this command will prompt the system to list all DNS records associated with the domain “linuxgenie.com” including their respective IP addresses.
Check Reverse DNS Lookup
If you want to perform a reverse DNS lookup, which involves looking up a domain name based on its IP address, you can use the following command:
$ dig -x 162.255.119.178
Executing this command will provide you with the corresponding domain name associated with the given IP address. The output will display the results in a format similar to the image below.
Conclusion
The `dig` command is an invaluable tool for DNS troubleshooting and querying on Ubuntu 22.04. By following the steps provided in this guide, you can effortlessly install the `dig` command and leverage its capabilities. From performing DNS lookups to querying specific record types and servers, `dig` empowers users to effectively diagnose and resolve DNS-related issues. Take advantage of the powerful features of the `dig` command to enhance your DNS management and troubleshooting abilities.