nslookup and dig for DNS in Linux CLI - Time & Space Complexity
When using commands like nslookup and dig to query DNS, it helps to understand how the time to get results grows as we ask for more information.
We want to know how the command's work changes when we query more domains or request more details.
Analyze the time complexity of the following command sequence.
for domain in example.com example.org example.net; do
dig +short $domain
nslookup $domain
sleep 1
done
This script queries three domains using dig and nslookup, pausing briefly between each.
Look for repeated actions that take time.
- Primary operation: Running
digandnslookupcommands for each domain. - How many times: Once per domain, so 3 times in this example.
As the number of domains increases, the total time grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 queries each for dig and nslookup |
| 100 | 100 queries each for dig and nslookup |
| 1000 | 1000 queries each for dig and nslookup |
Pattern observation: The total work grows directly with the number of domains queried.
Time Complexity: O(n)
This means if you double the number of domains, the total time roughly doubles too.
[X] Wrong: "Running dig or nslookup once takes the same time no matter how many domains I query."
[OK] Correct: Each domain query is a separate operation that takes time, so more domains mean more total time.
Understanding how command execution time grows with input size helps you write scripts that scale well and avoid surprises when working with many domains.
What if we combined all domain queries into a single dig command? How would the time complexity change?
Practice
nslookup command in Linux?Solution
Step 1: Understand nslookup's function
Thenslookupcommand is used to query DNS to find IP addresses or domain names.Step 2: Identify the main use case
Its main use is to quickly find the IP address of a given domain name.Final Answer:
To find the IP address of a domain name quickly -> Option CQuick Check:
nslookup = quick IP lookup [OK]
- Thinking nslookup edits DNS settings
- Confusing nslookup with network monitoring tools
- Assuming nslookup creates domains
dig to query the A record of example.com?Solution
Step 1: Recall dig syntax for record type
The correct syntax uses the-toption to specify the record type, e.g.,dig example.com -t A.Step 2: Match syntax to options
dig example.com -t Amatches this syntax.Final Answer:
dig example.com -t A -> Option BQuick Check:
dig -t TYPE domain = query record [OK]
- Placing record type without -t option
- Using invalid -A flag
- Using nonexistent -query flag
nslookup google.com if the DNS server is reachable and working?Solution
Step 1: Understand nslookup output on success
If the DNS server is reachable, nslookup returns the IP address(es) of the domain and info about the DNS server used.Step 2: Eliminate incorrect options
An error message saying 'server can't find google.com' is an error message, which won't appear if DNS works. A blank output with no information is blank output, unlikely. A list of all DNS servers on the internet is unrelated.Final Answer:
The IP address(es) of google.com along with DNS server info -> Option AQuick Check:
nslookup success = IP + server info [OK]
- Expecting nslookup to list all DNS servers
- Thinking nslookup outputs blank on success
- Confusing error messages with normal output
dig example.com MX but get no answer section in the output. What is the most likely reason?Solution
Step 1: Analyze dig output with no answer section
If dig shows no answer section for MX query, it usually means no MX records exist for that domain.Step 2: Check other options
The syntax is correct, eliminating A. Internet down would cause timeout or no response. DNS blocking would cause errors like REFUSED, not empty answer.Final Answer:
The domain example.com has no MX records configured -> Option DQuick Check:
No MX answer = no MX records [OK]
- Assuming syntax error when command is correct
- Blaming internet connection without checking
- Thinking DNS server blocks cause empty answers
dig to check if a domain has an A record and print "Exists" or "Not Found" accordingly. Which command snippet correctly checks this?Solution
Step 1: Understand how to check A record existence with dig
Usingdig example.com A +shortreturns IP addresses if A records exist; empty if none.Step 2: Use grep to check output
Usinggrep -q '.'checks if output has any character (non-empty). If yes, print "Exists", else "Not Found".Step 3: Evaluate options
if dig example.com A +short | grep -q '.'; then echo "Exists"; else echo "Not Found"; fi correctly uses this logic. if dig example.com A | grep -q 'NXDOMAIN'; then echo "Exists"; else echo "Not Found"; fi checks for NXDOMAIN incorrectly. if nslookup example.com | grep -q 'Address'; then echo "Exists"; else echo "Not Found"; fi uses nslookup, not dig. if dig example.com MX +short | grep -q '.'; then echo "Exists"; else echo "Not Found"; fi checks MX records, not A.Final Answer:
if dig example.com A +short | grep -q '.'; then echo "Exists"; else echo "Not Found"; fi -> Option AQuick Check:
dig +short output empty means no record [OK]
- Checking wrong record type (MX instead of A)
- Misinterpreting NXDOMAIN in grep
- Using nslookup output with dig logic
Ready to prove it?
Pass the test and this pattern moves unknown
