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
Share:
169,960

Related videos on Youtube

Frank
Author by

Frank

Stack overflow is a good web site showing me how I have grown as a Dev.

Updated on December 24, 2021

Comments

  • Frank
    Frank over 2 years

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

    • Felix Kling
      Felix Kling over 13 years
      You have already asked this in your previous question: ping a computer in ssh? and again, this belongs not on SO.
    • Ignacio Vazquez-Abrams
      Ignacio Vazquez-Abrams over 13 years
      Why do you INSIST on asking all these questions in the wrong place?
    • vaab
      vaab over 9 years
      isn't bash also a programming language ? The same question asked in python or php would have been accepted it seems...
    • Thelambofgoat
      Thelambofgoat almost 9 years
      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.
    • Michael Cole
      Michael Cole over 7 years
      Wow, 27 question upvotes and 53 answer upvotes. Maybe stackoverflow is over-moderated.
  • tripleee
    tripleee about 8 years
    The output from nslookup is less ideal for scripting. dig +short is probably the most correct answer here, as already suggested by @unutbu.
  • CTodea
    CTodea about 7 years
    Neither dig and host are installed by default on all distros, meanwhile nslookup is part of busybox, the base of lightweight distos like Alpine.
  • Chris_Rands
    Chris_Rands about 7 years
    I think you need to drop the www.
  • cubuspl42
    cubuspl42 about 6 years
    @Chris_Rands Domains www.example.com and example.com can point to different IP addresses.
  • Abdul Hameed
    Abdul Hameed over 5 years
    great but if you replace % with $ or just remove then it will save 1 minute for lazy copy cats :)
  • lava-lava
    lava-lava over 5 years
    If you are using Arch Linux or based on that distribution, you'll find these tools and nslookup in bind-tools package.
  • fuweichin
    fuweichin over 5 years
    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.
  • wisbucky
    wisbucky over 4 years
    I know nslookup is being deprecated, but I still prefer its output format over dig +short and host for human readability.
  • mRyan
    mRyan over 3 years
    This returns 127.0.1.1 for me if the domain points to the IP of the machine running the command.
  • 5p0ng3b0b
    5p0ng3b0b almost 3 years
    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
    5p0ng3b0b almost 3 years
    @tripleee nslookup stackoverflow.com | head -n6 | tail -n1 | awk '{print $2}'
  • tripleee
    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 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).
  • 5p0ng3b0b
    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 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.