Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

What's the command to find the name of a computer given its IP address?

I always forget what this command is, but I know it exists in Windows and I assume it exists on the *nix command-line.

share|improve this question

10 Answers 10

up vote 13 down vote accepted

dig and host should be what you're looking for ;)

http://www.unix.com/unix-dummies-questions-answers/9866-nslookup-linux.html

EDIT : nslookup work too finally, I had a blank on that one and tought it wasn't available so I deleted my post lol :P More infos on nslookup command whether it seems to have been replace since a while now : http://linuxreviews.org/man/nslookup/

share|improve this answer

on *nix you can use dig -x [address]

share|improve this answer
1  
This definitely seems to be the easiest way. Add +short at the end to return nothing but the rdns result. dig -x [address] +short –  ColinM Mar 1 '12 at 3:36
    
That +short flag is really useful! –  Neil Sep 13 '12 at 13:59

I'm well aware that dig/host/nslookup are the standard tools for these, but I keep these around for testing the OS's resolution (essentially, to test nsswitch.conf is working correctly):

gethostbyname:

#!/usr/bin/perl

use Socket;

my @t = gethostbyname($ARGV[0]);
print "\$name     = $t[0]\n"; shift(@t);
print "\$aliases  = $t[0]\n"; shift(@t);
print "\$addrtype = $t[0]\n"; shift(@t);
print "\$length   = $t[0]\n"; shift(@t);

foreach (@t) {
  print "          = ", inet_ntoa($_), "\n";
}

gethostbyaddr:

#!/usr/bin/perl

use Socket;

my @t = gethostbyaddr(inet_aton($ARGV[0]), AF_INET);
print "\$name     = $t[0]\n"; shift(@t);
print "\$aliases  = $t[0]\n"; shift(@t);
print "\$addrtype = $t[0]\n"; shift(@t);
print "\$length   = $t[0]\n"; shift(@t);

foreach (@t) {
  print "          = ", inet_ntoa($_), "\n";
}

example:

g3 0 /home/jj33/swap > gethostbyname www.google.com
$name     = www.l.google.com
$aliases  = www.google.com
$addrtype = 2
$length   = 4
          = 72.14.205.147
          = 72.14.205.103
          = 72.14.205.104
          = 72.14.205.99
g3 0 /home/jj33/swap > gethostbyaddr 72.14.205.147 
$name     = qb-in-f147.google.com
$aliases  = 
$addrtype = 2
$length   = 4
          = 72.14.205.147
share|improve this answer
1  
you can do "getent hosts [IP or HOSTNAME]" –  hayalci May 27 '09 at 9:44
    
Hmmm... I wrote the tools originally just to play with the functions, so no loss there but I certainly wouldn't have pasted them into serverfault if I had known about the getent tool. Thanks for the pointer. –  jj33 May 27 '09 at 14:06
    
-1: they are limited to IPv4, gethostbyname does not retrieve IPv6 addresses when they exist and gethostbyaddr does not accept IPv6 addresses. –  bortzmeyer Sep 23 '09 at 7:14

If you're using nslookup it's this (assuming 192.168.0.1 as the IP in question)

> set type=ptr
> 1.0.168.192.in-addr.arpa

EDIT: Remember a reverse lookup only works if there is a PTR record created for the IP, and it's not guaranteed to return the hostname you're looking for. Completely depends on how DNS is configured and maintained in your situation.

share|improve this answer
3  
nsloookup is no longer maintained and its authors recommend dig. Besides, dig -x is much simpler than inversing the bytes yourself. –  bortzmeyer May 12 '09 at 13:23
    
That's good to know, thanks a lot for the input! Old habits die hard ;) –  squillman May 12 '09 at 13:47

Well, some friendly person just wrote nslookup is the command, and he's right. It works on both Unix and Windows. Not sure why you deleted your answer, but you are correct sir.

share|improve this answer
    
err, maybe not. –  Peter Turner May 11 '09 at 15:57
    
Yeah well, I did post a bit fast, and after a check I wasn't sure at all of my answer, I just put back my post and edit it to add more details ;) –  Marc-Andre R. May 11 '09 at 15:58
    
OK, it is, but it isn't I'm accepting that answer. Too bad we can't get that real time Googley AJAX here. –  Peter Turner May 11 '09 at 15:59
    
lol yeah well, we can't have everything ;) Have a nice day, I hope I help you ;) –  Marc-Andre R. May 11 '09 at 16:05

On Windows I got in to the habit of using:

ping -a <ip address>

as this will also reflect data from your hosts file and WINS and so on.

share|improve this answer

Powershell:

[net.dns]::gethostentry("69.59.196.212").HostName
share|improve this answer

On most of the Linux systems that I am aware of you can use:

 nslookup <ip-number EX: 127.0.0.1>

will work on the command line.

Come to think of it, isn't nslookup available on Windows XP?

share|improve this answer
    
Yes, indeed. And in previous versions of Windows. –  kubanczyk Jul 1 '09 at 18:30

I prefer the command-line dig for Windows (available here: http://members.shaw.ca/nicholas.fong/dig/) to nslookup any day.

If you have to test/administer DNS from a Windows workstation, grab this tool. Then:

C:\dig>dig -x <IP Address>

...also, remember to add c:\dig to your path!

share|improve this answer

nbtstat -a < ip address >

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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