Coder Perfect

How can I retrieve the hostname from the IP address in Linux?

Problem

I’d like to know the IP address of a remote machine or hostname. I found lots of answer such as nslookup, host, resloveip, etc.. but I still can’t get hostname from my target machine(cent OS, ubuntu etc…) It seems need to register to DNS server?

I have a test machine with the IP 10.1.27.97.

However, despite using the method described above, I am still unable to obtain “test.”

Is there anyone who can assist me in obtaining the hostname from an IP address?

Asked by RoyHSIEH

Solution #1

You can use nmblookup from the samba suite to find a hostname on your local network by IP address:

nmblookup -A <ip>

You can use the host software to look up a hostname on the internet:

host <ip>

Alternatively, you can execute the following command to install nbtscan:

sudo apt-get install nbtscan

And use:

nbtscan <ip>

*Adapted from https://askubuntu.com/questions/205063/command-to-get-the-hostname-of-remote-server-using-ip-address/205067#205067

Update 2018-05-13

nslookup is a command that can be used to query a name server. It works in both directions!

nslookup <IP>
nslookup <hostname>

Answered by HarlemSquirrel

Solution #2

The target’s name must be registered with DNS or statically declared in the hosts file on the system running your software in order to utilize nslookup, host, or gethostbyname(). Yes, you could use SSH or another application to connect to the target and query it directly, but for a generic solution, you’ll need a DNS record for it.

Answered by Chris Ryding

Solution #3

Another simple method I discovered for use in a LAN is

ssh [username@ip] uname -n

If you need to log in, use the command line.

sshpass -p "[password]" ssh [username@ip] uname -n

Answered by Марк Павлов

Post is based on https://stackoverflow.com/questions/24182950/how-to-get-hostname-from-ip-linux