Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get hostname from IP (Linux)?

Tags:

linux

hostname

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?

like image 638
RoyHSIEH Avatar asked Jun 12 '14 11:06

RoyHSIEH


People also ask

How do I find hostname from IP address?

Querying DNS Click the Windows Start button, then "All Programs" and "Accessories." Right-click on "Command Prompt" and choose "Run as Administrator." Type "nslookup %ipaddress%" in the black box that appears on the screen, substituting %ipaddress% with the IP address for which you want to find the hostname.

What is hostname from IP?

A host, or website, on the Internet is identified by a host name, such as www.example.com . Host names are sometimes called domain names. Host names are mapped to IP addresses, but a host name and an IP address do not have a one-to-one relationship. A host name is used when a web client makes an HTTP request to a host.

How do I find my host name?

Locating Your Computer's Hostname on a PC (Windows 10) In the window the window that appears on the bottom-left hand corner of your screen, type in cmd and click OK. The command prompt window will appear. In this window, type hostname and press Enter. The name of your computer will be displayed.

How do I find the FQDN of an IP address in Linux?

You'll have to log in and use the hostname command. If you're looking for the Fully Qualified Domain Name (FQDN) of a network address, you can use DNS query tools like dig or nslookup , as described by @firm and @Richard Holloway. Show activity on this post. To be clear, hostname will just return the short name.


3 Answers

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>
like image 58
HarlemSquirrel Avatar answered Oct 19 '22 19:10

HarlemSquirrel


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.

like image 20
Chris Ryding Avatar answered Oct 19 '22 20:10

Chris Ryding


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
like image 8
Марк Павлов Avatar answered Oct 19 '22 20:10

Марк Павлов