RoyHSIEH
RoyHSIEH

Reputation: 955

How to get hostname from IP (Linux)?

I'd like to get remote machine/hostname through IP Address. 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 machine named test and using IP 10.1.27.97

but I used the method above still can't not get "test"

Does anyone can help me to get the hostname form IP Address?

Upvotes: 95

Views: 478738

Answers (7)

Bink
Bink

Reputation: 2171

Other ways to do this are with the dig and drill commands, but your distribution might require the installation of a package for these.

Upvotes: 1

Leon McClatchey
Leon McClatchey

Reputation: 1

I've found, after installing sshpass, that

sshpass -p "password" ssh user@IP hostname

Returns the hostname of the remote host.

Upvotes: 0

Patrick M
Patrick M

Reputation: 1

Don't use the host command. On some operating systems, you will set the name of your host with whatever you type after it, so typing host 23.23.23.23 will set your hostname to that.

Upvotes: 0

user21164536
user21164536

Reputation:

no need to know connected ip :

sh -c "IP=\$(curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'); nslookup \$IP | grep 'name =' | awk '{print \$NF}'"

This code is a shell command that does the following:

  • Uses the curl command to retrieve the public IP address of the machine on which it is executed from the website checkip.dyndns.org. The output of curl is piped into the sed command, which is used to extract just the IP address from the response.
  • The IP address is then stored in the IP variable.
  • The nslookup command is used to perform a reverse DNS lookup on the IP address. This will return the domain name associated with the IP address.
  • The output of nslookup is piped into the grep command, which is used to extract the line containing the string 'name ='.
  • Finally, the awk command is used to extract the last field in the line, which is the domain name associated with the IP address.

Upvotes: -1

HarlemSquirrel
HarlemSquirrel

Reputation: 10114

To find a hostname in your local network by IP address you can use nmblookup from the samba suite:

nmblookup -A <ip>

To find a hostname on the internet you could use the host program:

host <ip>

Or you can install nbtscan by running:

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

You can query a name server with nslookup. It works both ways!

nslookup <IP>
nslookup <hostname>

Upvotes: 122

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

Reputation: 101

Another simple way I found for using in LAN is

ssh [username@ip] uname -n

If you need to login command line will be

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

Upvotes: 10

Chris Ryding
Chris Ryding

Reputation: 1533

In order to use nslookup, host or gethostbyname() then the target's name will need to be registered with DNS or statically defined in the hosts file on the machine running your program. Yes, you could connect to the target with SSH or some other application and query it directly, but for a generic solution you'll need some sort of DNS entry for it.

Upvotes: 64

Related Questions