Is there a command to perform just DNS lookups on a domain name? Such as
dns unix.stackexchange.com
which would return 151.101.1.69
and gbnjk.com
would return error: domain not found
.
4 Answers
Another DNS query tool, dig
, is very verbose by default, but if you use it as dig +short
, you get just the answers, one per line:
$ dig +short unix.stackexchange.com
151.101.65.69
151.101.193.69
151.101.129.69
151.101.1.69
$ dig +short www.latimes.com
32975.edgekey.net.
e11929.e12.akamaiedge.net.
2.22.63.16
2.22.63.11
If the requested domain does not exist, it just returns nothing:
$ dig +short gbnjk.com
$
Yes, there are several.
ISC tools and various flavours of host
The tools that accompany ISC's BIND are host
and dig
. You can see their operation, including dig
's short mode, in other answers here.
The ISC is not the only source of a host
tool, however.
- Knot DNS comes with its own
khost
tool. On Debian, this is packaged in theknot-host
package which also supplies a symbolic link that makes it accessable ashost
as well.% host unix.stackexchange.com. unix.stackexchange.com. has IPv4 address 151.101.1.69 unix.stackexchange.com. has IPv4 address 151.101.65.69 unix.stackexchange.com. has IPv4 address 151.101.129.69 unix.stackexchange.com. has IPv4 address 151.101.193.69 Host unix.stackexchange.com. has no AAAA record Host unix.stackexchange.com. has no MX record %
- I provide a
host
tool for use with the djbwares toolset, which I also provide in the form of adjbdns-host
Debian package. It is a thin wrapper around the native djbdns query tools. Unlike the Knot and ISChost
tools, which as you can see from the aforegiven do multiple transactions in some cases, it always performs just one DNS transaction.% host unix.stackexchange.com. 1 unix.stackexchange.com: 104 bytes, 1+4+0+0 records, response, noerror query: 1 unix.stackexchange.com answer: unix.stackexchange.com 300 A 151.101.1.69 answer: unix.stackexchange.com 300 A 151.101.65.69 answer: unix.stackexchange.com 300 A 151.101.129.69 answer: unix.stackexchange.com 300 A 151.101.193.69 % % host gbnjk.com 1 gbnjk.com: 27 bytes, 1+0+0+0 records, response, authoritative, nxdomain query: 1 gbnjk.com %
Daniel J. Bernstein tools
The native djbdns query tools are dnsq
, dnsqr
, dnsqrx
, dnsname
, dnsnamex
, dnsmx
, dnsip
, dnsipq
, dnstxt
, and dnstrace
. There are many of them because they split into three categories:
- Tools that produce human-readable output:
dnsq
,dnsqr
,dnsqrx
- Tools that produce machine-parseable output:
dnsname
,dnsnamex
,dnsmx
,dnsip
,dnsipq
,dnstxt
dnstrace
whose output is machine-parseable, and conventionally passed throughdnstracesort
, a filter that turns machine-parseable into human-readable
As aforementioned, the human-readable tools perform just one DNS transaction, and print the contents of the response from the relevant DNS server (different tools being tailored to specific types of DNS server, content or proxy).
% dnsqr ns stackexchange.com. 2 stackexchange.com: 170 bytes, 1+4+0+0 records, response, noerror query: 2 stackexchange.com answer: stackexchange.com 170156 NS ns-1029.awsdns-00.org answer: stackexchange.com 170156 NS ns-925.awsdns-51.net answer: stackexchange.com 170156 NS ns-cloud-d1.googledomains.com answer: stackexchange.com 170156 NS ns-cloud-d2.googledomains.com % % dnsq ns stackexchange.com. ns-925.awsdns-51.net. /package/admin/djbwares [pts/4.10022.1] 2 stackexchange.com: 170 bytes, 1+4+0+0 records, response, authoritative, noerror query: 2 stackexchange.com answer: stackexchange.com 172800 NS ns-1029.awsdns-00.org answer: stackexchange.com 172800 NS ns-925.awsdns-51.net answer: stackexchange.com 172800 NS ns-cloud-d1.googledomains.com answer: stackexchange.com 172800 NS ns-cloud-d2.googledomains.com % % dnsqr a gbnjk.com 1 gbnjk.com: 27 bytes, 1+0+0+0 records, response, authoritative, nxdomain query: 1 gbnjk.com %
The machine-parseable tools are designed to be used in shell scripts. They do not clutter their outputs with extra stuff like "has ipv4 address" or "answer:", and their outputs are well-defined. dnsipq
always prints a single line (that is possibly blank) for each input name or exits with a failure code, for example. dnsnamex
always prints a single line (that is possibly blank or containing multiple space-separated names) for each input IP address, for another example.
% dnsip unix.stackexchange.com. unix.stockexchange.com. 151.101.129.69 151.101.193.69 151.101.1.69 151.101.65.69 % % dnsnamex `dnsip unix.stackexchange.com.` % % dnsmx unix.stackexchange.com. 0 unix.stackexchange.com % % dnsmx stackexchange.com. 1 aspmx.l.google.com 5 alt1.aspmx.l.google.com 5 alt2.aspmx.l.google.com 10 alt3.aspmx.l.google.com 10 alt4.aspmx.l.google.com %
As you can see, they are geared towards doing the DNS lookups for specific tasks rather than doing just one DNS transaction of a specific type. dnsmx
, for example, reports to where MTSes are to transmit SMTP mail and what order they try in, in a machine-parseable one-result-per-line whitespace-separated form; gathering which information involves more than one DNS transaction.
NLlabs tools
NLlabs's drill
from LDNS (not Unbound as another answer has it) is designed to print "even more information than dig
" according to its user manual. It does not have an equivalent of dig
's short mode. Like dig
in normal mode, its output is in the ISC's "zone file" format, complete with copious comment lines, requiring a full "zone file" parser to handle programatically. So it isn't the tool for short machine-parseable output.
Its command-line parsing is rather sloppy:
% drill you can have anything here +short and as much of it as you like '!' unix.stackexchange.com. ;; ->>HEADER<<- opcode: QUERY, rcode: NOERROR, id: 47385 ;; flags: qr rd ra ; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;; unix.stackexchange.com. IN A ;; ANSWER SECTION: unix.stackexchange.com. 277 IN A 151.101.193.69 unix.stackexchange.com. 277 IN A 151.101.129.69 unix.stackexchange.com. 277 IN A 151.101.65.69 unix.stackexchange.com. 277 IN A 151.101.1.69 ;; AUTHORITY SECTION: ;; ADDITIONAL SECTION: ;; Query time: 145 msec ;; SERVER: 127.0.0.1 ;; WHEN: Sun May 27 10:52:12 2018 ;; MSG SIZE rcvd: 104 %
Although this sort of thing aids humans who cannot remember whether the type or the domain name is the first argument to the command, it is the opposite of what one wants for scripting purposes. One wants a well-defined interface, with a defined and documented ordering of parameters, where a misuse is an error and that does not attempt to second-guess what one meant to run.
"just DNS"
So as noted in another answer, it depends from what you want by "just DNS".
- If you want tools with machine-parseable outputs for use in scripts, that output just the resultant information, look to the djbdns toolset.
- If you want tools that only ever perform just one DNS transaction but give an output that you as a human can read, look at my
host
or the tools from the djbdns toolset. - Otherwise, look at any and all of the aforementioned.
Note that one of the several failings of nslookup
is that on several operating systems including some Unices it has been augmented to do more than just DNS lookups alone. nslookup
is a seriously flawed tool, as I and others have been explaining for close to two decades, now. It most definitely should not be "the usual tool" and I hope that because of our efforts over the years nowadays it is not.
Further reading
- Jonathan de Boyne Pollard (2004). DNS diagnosis tools.. Frequently Given Answers.
- Jonathan de Boyne Pollard. commands. The djbwares toolset. Softwares.
- Jonathan de Boyne Pollard (2001).
nslookup
is a badly flawed tool. Don't use it.. Frequently Given Answers.
It depends on what exactly you mean by "just" (compared to what baseline?). host
(from the bind-tools or bind-utils package) is pretty terse in its output, but still has a bit of human-readable text around it:
[0 1016 8:16:41] ~ % host unix.stackexchange.com
unix.stackexchange.com has address 151.101.129.69
unix.stackexchange.com has address 151.101.193.69
unix.stackexchange.com has address 151.101.65.69
unix.stackexchange.com has address 151.101.1.69
Part of the "problem" is that there are more cases that can happen that you'd need to signal in output and/or return code, such as: DNS server failure, CNAME entries (i.e. "linux.example.com is whatever unix.example.com is, go look there"):
[0 1018 8:17:36] ~ % host www.latimes.com
www.latimes.com is an alias for 32975.edgekey.net.
32975.edgekey.net is an alias for e11929.e12.akamaiedge.net.
e11929.e12.akamaiedge.net has address 23.219.130.10
e11929.e12.akamaiedge.net has address 23.219.130.8
and probably many more.
The usual simple tools tool are:
$ host unix.stackexchange.com
unix.stackexchange.com has address 151.101.1.69
unix.stackexchange.com has address 151.101.65.69
unix.stackexchange.com has address 151.101.129.69
unix.stackexchange.com has address 151.101.193.69
$ nslookup unix.stackexchange.com
Server: 127.0.0.1
Address: 127.0.0.1#53
Non-authoritative answer:
Name: unix.stackexchange.com
Address: 151.101.1.69
Name: unix.stackexchange.com
Address: 151.101.65.69
Name: unix.stackexchange.com
Address: 151.101.129.69
Name: unix.stackexchange.com
Address: 151.101.193.69
The host comes from ISC unbound in bind9-host package. Both dig, and nslookup come from dnsutils package. If you need other tools directed to more detailed analysis of DNS, then there is dig
(from bind). A short answer is given with the +short
option (remove to get a more verbose answer):
$ dig +short unix.stackexchange.com
151.101.1.69
151.101.65.69
151.101.129.69
151.101.193.69
Or drill (from unbound):
$ drill @1.1.1.1 unix.stackexchange.com
;; ->>HEADER<<- opcode: QUERY, rcode: NOERROR, id: 48240
;; flags: qr rd ra ; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;; unix.stackexchange.com. IN A
;; ANSWER SECTION:
unix.stackexchange.com. 250 IN A 151.101.1.69
unix.stackexchange.com. 250 IN A 151.101.65.69
unix.stackexchange.com. 250 IN A 151.101.129.69
unix.stackexchange.com. 250 IN A 151.101.193.69
;; AUTHORITY SECTION:
;; ADDITIONAL SECTION:
;; Query time: 3 msec
;; SERVER: 1.1.1.1
;; WHEN: Sun May 27 10:16:27 2018
;; MSG SIZE rcvd: 104
Be careful with dig and specially with trusting drill answers if you do not know how to set trust anchors. Both tools are far away from simple.
Related drill if you can, dig if you have to, nslookup if you must
nslookup
?