How to Use dig Command in Linux: Syntax and Examples
The
dig command in Linux is used to query DNS servers for information about domain names, such as IP addresses or mail servers. You run dig followed by a domain name and optional query type to get detailed DNS records.Syntax
The basic syntax of the dig command is:
dig [@server] [name] [type]
Here:
@serveris optional and specifies which DNS server to query (default is your system's DNS).nameis the domain name you want to look up.typeis the DNS record type you want (likeA,MX,TXT, etc.).
bash
dig [@server] [name] [type]Example
This example shows how to use dig to find the IP address (A record) of example.com:
bash
dig example.com A
Output
; <<>> DiG 9.18.1-1ubuntu1.3-Ubuntu <<>> example.com A
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12345
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; QUESTION SECTION:
;example.com. IN A
;; ANSWER SECTION:
example.com. 86399 IN A 93.184.216.34
;; Query time: 20 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Thu Jun 15 12:00:00 UTC 2024
;; MSG SIZE rcvd: 56
Common Pitfalls
Common mistakes when using dig include:
- Not specifying the query type, which defaults to
Arecord but may not be what you want. - Forgetting to use
@serverif you want to query a specific DNS server. - Misreading the output sections; the ANSWER SECTION contains the useful data.
bash
dig example.com
dig @8.8.8.8 example.com MXQuick Reference
| Option | Description |
|---|---|
| @server | Query a specific DNS server (e.g., @8.8.8.8) |
| name | Domain name to query (e.g., example.com) |
| type | DNS record type (A, MX, TXT, NS, etc.) |
| +short | Show only the answer section for quick results |
| +trace | Trace the query path from root servers |
Key Takeaways
Use
dig [@server] [name] [type] to query DNS records.Specify the DNS record type to get the exact information you need.
Use
@server to query a specific DNS server.Read the ANSWER SECTION in the output for the main results.
Use options like
+short for simpler output.