This question already has an answer here:

In a bash, I have an IP like This is the address of unix.stackexchange.com.

But, from the IP, how can I resolve it to the hostname? Like:

>>> magic
you get: unix.stackexchange.com

Any idea?

marked as duplicate by Jeff Schaller, chaos, heemayl, Jakuje, Anthon Apr 13 '16 at 19:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    It's the other way around though. – Jakob Lenfers Apr 13 '16 at 15:25
  • yes, @JeffSchaller, I want the other way. I really have read this question before post. – Pedro Henrique Pedro Apr 13 '16 at 15:49
  • nslookup, host, and dig (with -x) from the other Q all (attempt) reverse resolution. In this particular case, the IP's for unix.stackexchange.com do not have reverses, as thrig pointed out. – Jeff Schaller Apr 13 '16 at 15:57
  • BTW, this question has nothing to do with bash. You're looking for a command, and that command will run a program, and so will work equally well in any shell — commands like this aren't built into shells. – Scott Apr 13 '16 at 18:36

Reverse records (for IPv4) are stored (backwards) somewhere under the in-addr.arpa zone, which tools like host will handily reverse for you, while other tools may need to be fed the reversed IP address and so forth.

% host | awk '{print $NF}'
% host
Host not found: 3(NXDOMAIN)
% dig +short PTR

Whether appropriate reverse records have been setup, well, maybe you get lucky, maybe you do not.

Over in IPv6istan, you'll probably want a tool (e.g. sipcalc) that can expand out an IPv6 address instead of typing things out...

% dig +short PTR
  • It din't work for me yet. When I tried the google ( I have no problem. But to get the hostname of stackexchange don't work. But if I copy and paste the IP ( in my browser I don't get to stackexchange. The problem now is the cloudflare? – Pedro Henrique Pedro Apr 13 '16 at 16:01
  • If there's no reverse record, then there's no reverse record. They're not always set these days, especially for things like load balancers that probably host all sorts of different domains, or cloud services where there's no API to fiddle around with records in the appropriate reverse zone. – thrig Apr 13 '16 at 17:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.