219

Is there any Linux command to translate a domain name to an IP address?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Frank
  • 7,235
  • 9
  • 46
  • 56
  • 1
    You have already asked this in your previous question: [ping a computer in ssh?](http://stackoverflow.com/questions/3962941/ping-a-computer-in-ssh) and again, this belongs not on SO. – Felix Kling Oct 18 '10 at 20:27
  • 4
    Why do you INSIST on asking all these questions in the wrong place? – Ignacio Vazquez-Abrams Oct 18 '10 at 20:28
  • isn't bash also a programming language ? The same question asked in python or php would have been accepted it seems... – vaab Sep 30 '14 at 04:19
  • 13
    Why 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. – Thelambofgoat May 06 '15 at 07:38
  • 12
    Wow, 27 question upvotes and 53 answer upvotes. Maybe stackoverflow is over-moderated. – Michael Cole Nov 29 '16 at 04:02
  • 1
    what a useful question and answer this was for me! – user3425506 Jun 14 '22 at 09:52

2 Answers2

311

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.
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 3
    great but if you replace % with $ or just remove then it will save 1 minute for lazy copy cats :) – Abdul Hameed Aug 25 '18 at 12:22
  • 4
    If you are using Arch Linux or based on that distribution, you'll find these tools and nslookup in `bind-tools` package. – lava-lava Oct 12 '18 at 12:13
  • 2
    To 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. – fuweichin Dec 03 '18 at 12:21
  • This returns `127.0.1.1` for me if the domain points to the IP of the machine running the command. – mRyan Oct 29 '20 at 11:12
  • 1
    I 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 May 03 '21 at 04:54
  • I have a situation where this command does not work. If you are on localhost of the server that the domain points to, you simply get back 127.0.1.1 which is NOT the IP address from the outside point of view which is what I want. How do I get the IP address when it HAS to run on the local machine that shares the IP with the domain? – Joseph Astrahan Jul 24 '22 at 01:14
111

You can use:

nslookup www.example.com
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • 11
    The output from `nslookup` is less ideal for scripting. `dig +short` is probably the most correct answer here, as already suggested by @unutbu. – tripleee Jan 08 '16 at 10:45
  • 9
    Neither `dig` and `host` are installed by default on all distros, meanwhile `nslookup` is part of busybox, the base of lightweight distos like Alpine. – Cristian Todea Feb 01 '17 at 16:38
  • I think you need to drop the `www.` – Chris_Rands Feb 22 '17 at 08:55
  • 2
    @Chris_Rands Domains `www.example.com` and `example.com` can point to different IP addresses. – cubuspl42 Feb 27 '18 at 18:00
  • I know `nslookup` is being deprecated, but I still prefer its output format over `dig +short` and `host` for human readability. – wisbucky Jun 28 '19 at 21:42
  • @tripleee `nslookup stackoverflow.com | head -n6 | tail -n1 | awk '{print $2}'` – 5p0ng3b0b May 03 '21 at 05:03
  • @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 the `head` and `tail` 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 your `head` and `tail` combo does, but only fetches one of the IP addresses (the first one in the result set). – tripleee May 03 '21 at 05:05
  • @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 use `toybox 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. – 5p0ng3b0b May 03 '21 at 05:52