0
0
Linux CLIscripting~5 mins

nslookup and dig for DNS in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to find the address of a website or check if a domain name is working correctly. nslookup and dig are tools that help you ask the internet's phone book, called DNS, to find this information for you.
When you want to find the IP address of a website like example.com
When you need to check if your domain name is pointing to the right server
When troubleshooting internet connection problems related to domain names
When you want to see detailed information about DNS records for a domain
When you want to verify if DNS changes have been updated properly
Commands
This command asks the DNS to find the IP address for example.com using nslookup.
Terminal
nslookup example.com
Expected OutputExpected
Server: 192.168.1.1 Address: 192.168.1.1#53 Non-authoritative answer: Name: example.com Address: 93.184.216.34
This command uses dig to get detailed DNS information about example.com, including its IP address.
Terminal
dig example.com
Expected OutputExpected
; <<>> DiG 9.16.1-Ubuntu <<>> example.com ;; 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. 3600 IN A 93.184.216.34 ;; Query time: 20 msec ;; SERVER: 192.168.1.1#53(192.168.1.1) ;; WHEN: Fri Jun 14 12:00:00 UTC 2024 ;; MSG SIZE rcvd: 56
This command asks dig to show the mail servers (MX records) for example.com, which handle email for the domain.
Terminal
dig example.com MX
Expected OutputExpected
; <<>> DiG 9.16.1-Ubuntu <<>> example.com MX ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12346 ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1 ;; QUESTION SECTION: ;example.com. IN MX ;; Query time: 15 msec ;; SERVER: 192.168.1.1#53(192.168.1.1) ;; WHEN: Fri Jun 14 12:01:00 UTC 2024 ;; MSG SIZE rcvd: 43
Key Concept

If you remember nothing else, remember: nslookup is simple for quick lookups, while dig gives detailed DNS info for deeper troubleshooting.

Common Mistakes
Typing only 'nslookup' or 'dig' without a domain name
The tools need a domain name to look up; without it, they wait for input or show help, which is not useful.
Always include the domain name you want to check, like 'nslookup example.com' or 'dig example.com'.
Expecting dig output to be short and simple like nslookup
dig shows a lot of technical details which can be confusing if you only want the IP address.
Use dig when you want detailed info; for quick IP lookups, nslookup is easier.
Summary
Use nslookup to quickly find the IP address of a domain.
Use dig to get detailed DNS records and troubleshoot DNS issues.
Always specify the domain name when running nslookup or dig commands.