Вы находитесь на странице: 1из 11

05/04/2017 Linux and Unix dig Command Examples

MENU

nixCraft
Linux and Unix tutorials for new and seasoned sysadmin.

LinuxandUnixdigCommandExamples
byVIVEKGITEonJANUARY12,2012lastupdatedJANUARY18,2016
inCOMMANDS,FREEBSD,LINUX,MACOSX,UNIX

I
amanewLinux/Unixcommandlineuser.HowdoIusedig
commandlineutilitytoqueryDNS(DomainNameSystem)
serversonLinuxandUnixlikeoperatingsystems?HowdoIusedig
commandtofindtheIPaddressofahostnameorhostNameofan
IPaddress?

ThedigcommandisprimarilyusedtoqueryDNS digcommanddetails
servers.Itisausefultoolfornetwork
Description QueryDNS
troubleshooting.Itispopularduetoitsflexibility,
Category NetworkUtilities
simpletouse,andcrystalclearoutputoverhost
command.Thedigcommandfundamentallyusedfor Difficulty Intermediate

thefollowingpurposes: Rootprivileges No

Estimatedcompletiontime 20m

Contents

Syntax

DNSrecordtypes

Examples

Understandingdigoutput

Queryanameserver

Specifyingthequerytype

ShowtheSOAfordomain/zonename

ReverseDNSlookup

Getalongorshortanswer

Readhostnamesfromafile

Debugtip:Tracednspath
1.PerformsDNSlookups.
FindOutTTL
2.Findhostaddresses,IPaddress,mail
SetIPv4orIPv6querytransport
exchanges(MX),CNAMEs,nameservers,and
more. mode

Options
3.VerifyISPDNSserverandInternetconnectivity.
Video
4.Verifyspamandblacklistingrecords.
Seealso

Purpose

UsedigcommandforDNSlookupandtoqueryDNSnameserversforvarious
resourcerecord.

Syntax

Thebasicsyntax:
dig Hostname
dig DomaiNameHere
dig @DNS-server-name Hostname
dig @DNS-server-name IPAddress

https://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/ 1/11
05/04/2017 Linux and Unix dig Command Examples
dig @DNS-server-name Hostname|IPAddress type
Where:

1.DNS-server-nameThenameorIPaddressofthenameservertoquery(suchas
8.8.8.8orns1.cyberciti.biz).IfnoDNSservernameisprovided,thedigcommandwill
usethe/etc/resolv.conffileandusenameserverslistedthere.
2.Hostname|IPAddressThenameoftheresourcerecordthatistobelookedup
usingDNSservername.
3.typeSetthetypeofquerysuchasA,MX,NSandmore.

ListofDNSrecordtypes
Beforeyouusethedigcommandyoushouldawareofcommontypesofresourcerecords
oftheDNS.Herearemostcommonresourcerecords:

Type Purpose Examples


A IPv4IPaddress 192.168.1.5or75.126.153.206
AAAA IPv6IPaddress 2607:f0d0:1002:51::4
s0.cyberciti.orgisanalias
CNAMECanonicalnamerecord(Alias) for
d2m4hyssawyie7.cloudfront.net
smtp.cyberciti.bizor
MX Emailserverhostnames
mx1.nixcraft.com
ns1.cyberciti.bizorns-
NS Name(DNS)servernames
243.awsdns-30.com
Pointertoacanonicalname.
PTR Mostlyusedforimplementingreverse 82.236.125.74.in-addr.arpa
DNSlookups
AuthoritativeinformationaboutaDNS
SOA seebelow
zone
TXT Textrecord seebelow
Bydefault,digcommandlooksforArecordsonly.

digcommandexamples

TofindtheIPaddressofthehostwww.cyberciti.biz,type:

dig www.cyberciti.biz

Sampleoutputs:

Fig.01:Thedefaultdigcommandoutput

Understandingdigcommandoutput

https://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/ 2/11
05/04/2017 Linux and Unix dig Command Examples
Theaboveissimplequeryforasinglehostcalledwww.cyberciti.biz.Thedigcommand
displayslotsofinformation.Letustrytounderstandthedefaultdigcommandoutput:

1.Thequestionssectiondisplaysquerytype.BydefaultqueryisforA(Internet
address).Inthisexample,IamusingdigcommandtofindoutanIPaddressof
www.cyberciti.biz.
2.Next,yougettheanswertoyourqueryinanswersectiontheaddressof
www.cyberciti.bizis75.126.153.206.
3.Thefinalsectionofthedigcommandcontainsstatistics(statssection)aboutthe
querysuchasnameservernames,querytimeandmore.

HowdoIqueryadifferentnameserversuchasns1.example.com?
Typethefollowingcommand:

dig @ns1.example.com Hostname


dig @ns1.example.com IPAddress
dig @ns1.yahoo.com yahoo.com
dig @ns-1287.awsdns-32.org www.cyberciti.biz

Sampleoutputs(note205.251.197.7inthestatssection):

; <<>> DiG 9.8.3-P1 <<>> @ns-1287.awsdns-32.org www.cyberciti.biz


; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: REFUSED, id: 24233
;; flags: qr rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;www.cyberciti.biz. IN A

;; Query time: 61 msec


;; SERVER: 205.251.197.7#53(205.251.197.7)
;; WHEN: Mon Jan 13 17:25:10 2014
;; MSG SIZE rcvd: 35

HowdoIselecttheDNSquerytype?
digwillletyouquerytypes.Thebasicsyntaxis:

dig Hostname|IPAddress type


dig @ns-server-here Hostname|IPAddress type
dig Hostname|IPAddress type
dig [+options] @ns-server-here Hostname|IPAddress type

Herearemostcommonexamplesofdnslookups.

FindtheIPaddress(A)ofaHostname

dig www.cyberciti.biz A
dig @ns-1075.awsdns-06.org www.cyberciti.biz A
dig +short www.cyberciti.biz A

Sampleoutputs:

75.126.153.206

FindthehostnameofanIPaddress

dig 74.125.236.167

FindtheMX(mailexchanges)ofadomain/hostname

dig cyberciti.biz MX
dig @ns-1075.awsdns-06.org cyberciti.biz MX
dig +short cyberciti.biz MX

https://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/ 3/11
05/04/2017 Linux and Unix dig Command Examples

Sampleoutputs:

1 aspmx.l.google.com.
10 aspmx2.googlemail.com.
10 aspmx3.googlemail.com.
5 alt1.aspmx.l.google.com.
5 alt2.aspmx.l.google.com.

FindtheNS(nameservers)ofadomainname

dig cyberciti.biz NS
dig @ns-1075.awsdns-06.org cyberciti.biz NS
dig +short cyberciti.biz NS

Sampleoutputs:

ns-1075.awsdns-06.org.
ns-1947.awsdns-51.co.uk.
ns-243.awsdns-30.com.
ns-866.awsdns-44.net.

FindtheTXT(textannotations)ofadomainname

dig google.com TXT


dig +short google.com TXT
dig @8.8.8.8 google.com TXT

Sampleoutputs:

; <<>> DiG 9.8.3-P1 <<>> google.com TXT


;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24961
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;google.com. IN TXT

;; ANSWER SECTION:
google.com. 3590 IN TXT "v=spf1 include:_spf.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~al

;; Query time: 29 msec


;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Mon Jan 13 17:41:00 2014
;; MSG SIZE rcvd: 116

ANYqueryi.e.tryallqueriestogetasmuchasinformationpossible
YoucanalsotryANYqueryasfollows:

dig cyberciti.biz ANY


dig +short cyberciti.biz ANY
dig @8.8.8.8 cyberciti.biz ANY

Sampleoutputs:

; <<>> DiG 9.8.3-P1 <<>> @8.8.8.8 cyberciti.biz ANY


; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 54232
;; flags: qr rd ra; QUERY: 1, ANSWER: 12, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;cyberciti.biz. IN ANY

;; ANSWER SECTION:

https://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/ 4/11
05/04/2017 Linux and Unix dig Command Examples
cyberciti.biz. 236 IN A 75.126.153.206
cyberciti.biz. 21536 IN NS ns-1075.awsdns-06.org.
cyberciti.biz. 21536 IN NS ns-1947.awsdns-51.co.uk.
cyberciti.biz. 21536 IN NS ns-243.awsdns-30.com.
cyberciti.biz. 21536 IN NS ns-866.awsdns-44.net.
cyberciti.biz. 836 IN SOA ns-1075.awsdns-06.org. awsdns-hostmaster.amazon.com.
cyberciti.biz. 3536 IN MX 1 aspmx.l.google.com.
cyberciti.biz. 3536 IN MX 10 aspmx2.googlemail.com.
cyberciti.biz. 3536 IN MX 10 aspmx3.googlemail.com.
cyberciti.biz. 3536 IN MX 5 alt1.aspmx.l.google.com.
cyberciti.biz. 3536 IN MX 5 alt2.aspmx.l.google.com.
cyberciti.biz. 21536 IN AAAA 2607:f0d0:1002:51::4

;; Query time: 31 msec


;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Mon Jan 13 17:43:28 2014
;; MSG SIZE rcvd: 406

ShowtheSOArecordsforDNSzonename
Thesyntaxis:

dig cyberciti.biz SOA


dig @8.8.8.8 cyberciti.biz SOA

Sampleoutputs:

; <<>> DiG 9.8.3-P1 <<>> @8.8.8.8 cyberciti.biz SOA


; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 35
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;cyberciti.biz. IN SOA

;; ANSWER SECTION:
cyberciti.biz. 894 IN SOA ns-1075.awsdns-06.org. awsdns-hostmaster.amazon.com.

;; Query time: 28 msec


;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Mon Jan 13 17:44:29 2014
;; MSG SIZE rcvd: 116

ORtry:

dig cyberciti.biz +nssearch

Sampleoutputs:

SOA ns-1075.awsdns-06.org. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400 from server


SOA ns-1075.awsdns-06.org. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400 from server
SOA ns-1075.awsdns-06.org. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400 from server
SOA ns-1075.awsdns-06.org. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400 from server

HowdoIperformareverseDNSlookups?
Tomapaddressestonames(reverselookups),passthe-xoptionasfollows:

dig -x ip-address-here
dig @ns-name-server -x ip-address-here
dig -x 75.126.153.206
dig +short -x 75.126.153.206

Sampleoutputs:

www.cyberciti.biz.

HowdoIgetalongorshortanswer?
Togetashortanswerpassthe+shortoptionasfollows:

https://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/ 5/11
05/04/2017 Linux and Unix dig Command Examples

dig +short query


dig +short cyberciti.biz NS
dig @8.8.8.8 +short cyberciti.biz NS

Bydefaultdigshowsalonganswer.Youcanpassthefollowingadditionaloptionstogeta
finedtunedanswer:

dig +multiline +noall +answer +nocmd cyberciti.biz ANY

Sampleoutputs:

Fig.02:Gettingalongansusefulansweraboutadomainname

Batchmode:Readhostnamesfromafile
Firstcreateatextfileasfollowwithnumberofqueries,oneperlineusingcatcommand:

cat > lookup.txt


+short yahoo.com mx
+short google.com mx
+short cyberciti.biz mx

Passthe-f lookup.txtoptionmakesdigoperateinbatchmode:

dig -f /path/to/lookup.txt
dig -f lookup.txt

Sampleoutputs:

https://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/ 6/11
05/04/2017 Linux and Unix dig Command Examples

Fig.03:Doingabulk/batchdnslookupsusingdigcommand

Debugtip:Tracednspath
Youcanforcedigtofollowthedelegationpathfromtherootnameserversforthename
beinglookedup.Thesyntaxis:

dig +trace cyberciti.biz


dig +short +trace cyberciti.biz
dig +short +trace s0.cyberciti.org

Sampleoutputs:

Fig.04:Tracingofthedelegationpathfromtherootnameservers

FindoutTTL(TimetoLive)valueforDNSrecords
Timetolive(TTL)isamechanismthatlimitsthelifetimeofdnsrecordsintheDomain
NameSystem(DNS).ItissetbyanauthoritativeDNSserverforparticularresourcerecord.
TheTTLissetinsecondsanditisusedbycaching(recursive)dnsservertospeedupdns
nameresolution.ThesyntaxisasfollowstofindoutTTL:

dig +nocmd +noall +answer +ttlid a example.com


dig +nocmd +noall +answer +ttlid A www.cyberciti.biz

Sampleoutputs:

https://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/ 7/11
05/04/2017 Linux and Unix dig Command Examples

Fig.05:FindingTTLvalueusingdigcommand

SetIPv4orIPv6querytransportmode
Passthe-4optionforcesdigtoonlyuseIPv4querytransport:

dig -4 query ...

Passthe-6optionforcesdigtoonlyuseIPv6querytransport:

dig -6 query ..

digcommandoptions

HereisthesummaryofallimportantoptionsofthedigcommandonLinuxorUnixlike
operatingsystems:

Commandlineoption Usage
-x dot-notation shortcutforreverselookups
-i useIP6.INTforIPv6reverselookups
-f filename Batchmode
-b address[#port] Bindtosourceaddress/port
-p port Specifyportnumber
-q name Specifyqueryname
-t type Specifyquerytype
-c class Specifyqueryclass
-k keyfile Specifytsigkeyfile
-y [hmac:]name:keySpecifynamedbase64tsigkey
-4 UseIPv4querytransportonly
-6 UseIPv6querytransportonly
-m Enablememoryusagedebugging
digcommandqueryoptions
digcommandhasanumberofqueryoptions.Eachqueryoptionisidentifiedbyakeyword
precededbyaplussign(+).Somekeywordssetorresetanoption.Thesemaybe
precededbythestringnotonegatethemeaningofthatkeyword.Otherkeywordsassign
valuestooptionslikethetimeoutinterval.Theyhavetheform+keyword=value.The
queryoptionsare:

Option Usage
+[no]vc TCPmode
+[no]tcp TCPmode,alternatesyntax
+time=### Setquerytimeout[5]

https://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/ 8/11
05/04/2017 Linux and Unix dig Command Examples
+tries=### SetnumberofUDPattempts[3]
+retry=### SetnumberofUDPretries[2]
+domain=### Setdefaultdomainname
+bufsize=### SetEDNS0MaxUDPpacketsize
+ndots=### SetNDOTSvalue
+edns=### SetEDNSversion
+[no]search Setwhethertousesearchlist
+[no]showsearch Searchwithintermediateresults
+[no]defname Ditto
+[no]recurse Recursivemode
+[no]ignore DontreverttoTCPforTCresponses.
+[no]fail DonttrynextserveronSERVFAIL
+[no]besteffort Trytoparseevenillegalmessages
+[no]aaonly SetAAflaginquery+[no]aaflag
+[no]adflag SetADflaginquery
+[no]cdflag SetCDflaginquery
+[no]cl Controldisplayofclassinrecords
+[no]cmd Controldisplayofcommandline
+[no]comments Controldisplayofcommentlines
+[no]question Controldisplayofquestion
+[no]answer Controldisplayofanswer
+[no]authority Controldisplayofauthority
+[no]additional Controldisplayofadditional
+[no]stats Controldisplayofstatistics
+[no]short Disableeverythingexceptshortformofanswer
+[no]ttlid Controldisplayofttlsinrecords
+[no]all Setorclearalldisplayflags
+[no]qr Printquestionbeforesending
+[no]nssearch Searchallauthoritativenameservers
+[no]identify IDrespondersinshortanswers
+[no]trace Tracedelegationdownfromroot
+[no]dnssec RequestDNSSECrecords
+[no]nsid RequestNameServerID
+[no]multiline Printrecordsinanexpandedformat
+[no]onesoa AXFRprintsonlyonesoarecord

Relatedmedia

Thistutorialsisalsoavailableinaquickvideoformat:

Seealso

dig(1)commandmanpage
ResourceRecord(RR)TYPEs

Category ListofUnixandLinuxcommands

FileManagement cat

NetworkUtilities dighostip

PackageManager apt

ProcessesManagement bgchrootdisownfgjobskillkillallpwdxtimepidofpstree

Searching whereiswhich

UserInformation groupsidlastlastcommlognameuserswwhowhoamilidmembers

https://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/ 9/11
05/04/2017 Linux and Unix dig Command Examples
RelatedPosts:

HowtomapstaticIP
Howtosetandenable HowToFindOutHP addressusing
MariaDBslowquery NetworkPrinterSerial dnsmasqunderLXD
log NumberRemotely (Linux

KVMlibvirtassign
staticguestIP HowToFindMyPublic HowtoblockanIP
addressesusingDHCP IPAddressFrom addresswithufwon
on CommandLineOna UbuntuLinuxserver

Sharethistutorialon:
Twitter Facebook Google+ DownloadPDFversion Foundanerror/typoonthispage?

{3commentsaddone}

Anonymous August13,2015,9:03am

Wow,niceandcompletetutorialCongratulations!

REPLY LINK

Tom January18,2016,10:39am

ThisisawesometutorialandworkedlikeacharmonmyOSX.

REPLY LINK

Dinesh March21,2016,2:51am

Wowsuperbandonthepointdescription.Veryhelpful.

REPLY LINK

LeaveaComment

Name*

Email*

Comment

https://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/ 10/11
05/04/2017 Linux and Unix dig Command Examples

Submit

YoucanusetheseHTMLtagsandattributes:<strong><em><pre><code><ahref=""
title="">

Taggedwith:digcommand,Intermediate,NetworkUtilities

NextFAQ:CentOSLinux:FindMemory(RAM)UsageInformationCommand
PreviousFAQ:LinuxandUnixhostCommandExamples

Tosearch,typeandhitenter

20002017nixCraft.Allrightsreserved.PrivacyTermsofServiceQuestionsorCommentsDonations
www.cyberciti.bizishostedatLinode.SecureContentDeliveryNetwork(CDN)byStackPath.

https://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/ 11/11

Вам также может понравиться