Linux command to translate domain name to IP
169,960
Solution 1
Use this
$ dig +short stackoverflow.com
69.59.196.211
or this
$ host stackoverflow.com
stackoverflow.com has address 69.59.196.211
stackoverflow.com mail is handled by 30 alt2.aspmx.l.google.com.
stackoverflow.com mail is handled by 40 aspmx2.googlemail.com.
stackoverflow.com mail is handled by 50 aspmx3.googlemail.com.
stackoverflow.com mail is handled by 10 aspmx.l.google.com.
stackoverflow.com mail is handled by 20 alt1.aspmx.l.google.com.
Solution 2
You can use:
nslookup www.example.com
Related videos on Youtube
Author by
Frank
Stack overflow is a good web site showing me how I have grown as a Dev.
Updated on December 24, 2021Comments
-
Frank over 2 years
Is there any Linux command to translate a domain name to an IP address?
-
Felix Kling over 13 yearsYou have already asked this in your previous question: ping a computer in ssh? and again, this belongs not on SO.
-
Ignacio Vazquez-Abrams over 13 yearsWhy do you INSIST on asking all these questions in the wrong place?
-
vaab over 9 yearsisn't bash also a programming language ? The same question asked in python or php would have been accepted it seems...
-
Thelambofgoat almost 9 yearsWhy are you closing so important questions for developers? Yes, this question seems to be of ServerFault authority, but ServerFault is the most user-unfriendly StackExchange site ever, you ask questions there and never get answers.
-
Michael Cole over 7 yearsWow, 27 question upvotes and 53 answer upvotes. Maybe stackoverflow is over-moderated.
-
-
tripleee about 8 yearsThe output from
nslookup
is less ideal for scripting.dig +short
is probably the most correct answer here, as already suggested by @unutbu. -
CTodea about 7 yearsNeither
dig
andhost
are installed by default on all distros, meanwhilenslookup
is part of busybox, the base of lightweight distos like Alpine. -
Chris_Rands about 7 yearsI think you need to drop the
www.
-
cubuspl42 about 6 years@Chris_Rands Domains
www.example.com
andexample.com
can point to different IP addresses. -
Abdul Hameed over 5 yearsgreat but if you replace % with $ or just remove then it will save 1 minute for lazy copy cats :)
-
lava-lava over 5 yearsIf you are using Arch Linux or based on that distribution, you'll find these tools and nslookup in
bind-tools
package. -
fuweichin over 5 yearsTo make it compatible with CNAME records or multi-value A records, use
basename $(dig +short stackoverflow.com A | tr '\n' '/')
instead. This is useful when you want to get-IP-by-domain in shell scripts. -
wisbucky over 4 yearsI know
nslookup
is being deprecated, but I still prefer its output format overdig +short
andhost
for human readability. -
mRyan over 3 yearsThis returns
127.0.1.1
for me if the domain points to the IP of the machine running the command. -
5p0ng3b0b almost 3 yearsI didn't have a dig or host command on an embedded device with a very slimmed down busybox.
ping -q -W1 -c1 stackoverflow.com | head -n1 | cut -d "(" -f2 | cut -d ")" -f1
got the job done for me. -
5p0ng3b0b almost 3 years@tripleee
nslookup stackoverflow.com | head -n6 | tail -n1 | awk '{print $2}'
-
tripleee almost 3 years@5p0ng3b0b To the extent that that's robust (one of the problems with
nslookup
is that its output format can vary) you will want to refactor thehead
andtail
commands into the Awk script.nslookup stackoverflow.com | awk '/^Address: / { print $2 }'
gives me four distinct IP addresses, and loses the warning that this result is non-authoritative.awk 'NR==6 { print $2 }'
would do what yourhead
andtail
combo does, but only fetches one of the IP addresses (the first one in the result set). -
5p0ng3b0b almost 3 years@triplee awk better for sure! Just saying some systems have basic commands to work with. I'm not suggesting a one size fits all method, just a way of getting 1st IP which is all that is needed for a DNS lookup vs public IP check for a DDNS update script for example. The
toybox
command in android doesn't have nslookup, ping or any other network command except traceroute (and no awk for that matter) so here I would usetoybox traceroute -r stackoverflow.com 2>/dev/null | head -n1 | cut -d "(" -f2 | cut -d ")" -f1
. I'm not a linux guru like yourself, I just get it done with what is available.