Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Dig to resolve domain name under Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use Dig to resolve domain names under Linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Dig is a Unix-like command-line mode to query DNS, including NS records, A records, MX records and other related information tool. The source code of Dig is part of the ISC BIND package, but it is not included in most documents that compile and install Bind, but in linux systems, it is usually part of a package, bind-tools in Gentoo, bind-utils in Redhat/Fedora, or dnsutils in Debian.

Query the DNS information of a single domain name

The most typical use of the dig command is to query the information of a single host.

Linuxidc@linuxidc:~$ dig www.linuxidc.com; > DiG 9.11.3-1Ubuntu1.5-Ubuntu > www.linuxidc.com;; global options: + cmd;; Got answer:;;-> > HEADER#53 (127.0.0.53); WHEN: Sun Mar 03 10:55:35 CST 2019; MSG SIZE rcvd: 77

The default output of the dig command is rich, which can be divided into. 5 parts

The first part shows the version of the dig command and the parameters entered. The second part shows some technical details returned by the service, and more importantly, status. If the value of status is NOERROR, this query ends successfully. The "QUESTION SECTION" in the third part shows the domain name we are looking for. The "ANSWER SECTION" in the fourth part is the result of the query. The fifth part is some statistical information of this query, such as how long it took, which DNS server was queried, when the query was conducted, and so on.

By default, the dig command queries the A record, and the A shown in the figure above indicates that the record type of the query is A record. Before trying to query other types of records, let's take a look at the common DNS record types.

Types of common DNS records

Type purpose

An address record, which is used to specify the IPv4 address of the domain name. If you need to point the domain name to an IP address, you need to add the A record. AAAA is used to specify the IPv6 address record corresponding to the hostname (or domain name). CNAME if you need to point the domain name to another domain name, and then another domain name provides the ip address, you need to add the CNAME record. MX if you need to set up a mailbox so that it can receive mail, you need to add a MX record. NS domain name server record. If you need to hand over the subdomain name to another DNS server for resolution, you need to add the NS record. Records like SOA SOA are mandatory in all culture files. It must be the first record in a file. TXT can write anything, and the length is limited to 255. The vast majority of TXT records are used for SPF records (anti-spam).

Query records of type CNAME

In addition to A record, the common DNS record is CNAME. We can specify the type of DNS record to be queried when querying:

Linuxidc@linuxidc:~$ dig m.linuxidc.com CNAME; > DiG 9.11.3-1ubuntu1.5-Ubuntu > m.linuxidc.com CNAME;; global options: + cmd;; Got answer:;;-> > HEADER#53 (127.0.0.53); WHEN: Sun Mar 03 11:20:34 CST 2019; MSG SIZE rcvd: 43

In this way, there is only a record of CNAME in the result. We can actually specify any type of DNS record in the query.

Query from the specified DNS server

For some reason, you want to query from the specified DNS server (the results obtained from the default DNS server may be inaccurate). Specify that the DNS server uses the @ symbol:

Linuxidc@linuxidc:~$ dig @ 8.8.8m.linuxidc.com; > DiG 9.11.3-1ubuntu1.5-Ubuntu > @ 8.8.8.8m.linuxidc.com; (1 server found);; global options: + cmd;; Got answer:;;-> > HEADER#53 (8.8.8.8); WHEN: Sun Mar 03 11:21:48 CST 2019; MSG SIZE rcvd: 75

From the figure above, you can see that the DNS server for this query is 8.8.8.8.

If you do not specify a DNS server, dig will use the address in / etc/resolv.conf as the DNS server in turn:

Command to resolve domain name under Linux-dig command to use the command to resolve domain name under detailed explanation Linux-dig command to use detailed explanation linuxidc@linuxidc:~$ dig m.linuxidc.com

The DNS server queried above becomes:

Reverse query

In the previous query, we specified the query server as 8.8.8.8. Whose DNS server is this? In fact, we can use dig's-x option to reverse resolve the domain name corresponding to the IP address:

Linuxidc@linuxidc:~$ dig-x 8.8.8.8 + shortgoogle-public-dns-a.google.com.

Well, it should be Google, so you can rest assured to use it.

Control the display result

The result returned by the dig command shows detailed information by default. If you want to get a streamlined result, you can use the + short option:

Linuxidc@linuxidc:~$ dig + short m.linuxidc.com122.228.238.15106.119.182.141

The result shown here is much more refreshing.

In fact, we can also have more options to control the output, such as only want to display the "ANSWER SECTION" content:

Command to resolve domain name under Linux-dig command to use detailed explanation Linux to resolve domain name command-dig command to use detailed explanation

The result is good, but there are too many options used (there are many of these options for the dig command, please refer to the user manual for details). We can achieve the same result in a more elegant way:

Linuxidc@linuxidc:~$ dig m.linuxidc.com + noall + answer; > DiG 9.11.3-1ubuntu1.5-Ubuntu > m.linuxidc.com + noall + answer;; global options: + cmdm.linuxidc.com. 5 IN A 113.107.238.212m.linuxidc.com. 5 IN A 122.228.238.15

Track the entire query process

If you are curious about what process the dig command goes through when executing a query, you can try using the + trace option. It outputs all the information from the root domain to the final result:

Linuxidc@linuxidc:~$ dig sohu.com @ 202.102.134.68-p53-t MX + trace; > DiG 9.11.3-1ubuntu1.5-Ubuntu > sohu.com @ 202.102.134.68-P 53-t MX + trace;; global options: + cmd. 482761 IN NS f.rootMurphy servers.net.. 482761 IN NS a.rootcopyright servers.net.net.. 482761 IN NS c. Roothouse servers.net.. 482761 IN NS b.rootcopyright servers.net.. 482761 IN NS j.rootcopyright servers.net.. 482761 IN NS d.rootcopyright servers.net.net.. 482761 IN NS k.rootcopyright servers.net.net.. 482761 IN NS e.rootconversation servers.net.. 482761 IN NS h. Rootcomp servers.net.. 482761 IN NS i.rootMurservers.net.net.. 482761 IN NS m.rootlyservers.net.. 482761 IN NS g.rootcopyright servers.net.. 482761 IN NS l.rootcopyright servers.net.politic; Received 239 bytes from 202.102.134.68 mscom 53 (202.102.134.68) in 40 mscom. 172800 IN NS d.gtld-servers.net.

The picture above is not a complete result. Interested friends can try it on their own.

Show 13 root domain servers

There are 13 root domain servers on internet, which are displayed using the dig command with no parameters

Linuxidc@linuxidc:~$ dig; > DiG 9.11.3-1ubuntu1.5-Ubuntu >; global options: + cmd;; Got answer:;;-> > HEADER#53 (127.0.0.53); WHEN: Sun Mar 03 11:37:11 CST 2019

This is the end of the article on "how to use Dig to resolve domain names under Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report