Find Hostname from IP Linux
Names or labels are useful as they allow us to identify one thing or one person from another. Even a person’s name, which might not be globally unique, contains a set of characters that allow a person to become distinguished from another person.
Similarly, computers support labels or names that help give them a unique identity in a network. That is where a hostname comes in. A hostname is a set of alphanumerical characters unique to a computer network, allowing devices to communicate with each other.
Typically, the underlying technology that identifies a computer or a device in a network is a set of numbers known as IP addresses. These are then mapped to a specific name resolved to the IP address of the machine.
In this tutorial, however, I will show you various ways that you can use to get the hostname associated with a specific IP address. The methods discussed in this tutorial will work in almost all Linux and Unix-Like systems.
If you are looking for detailed instructions on how systems such as Domain Names work, check out the resource on the link provided below:
https://linuxhint.com/dns-for-beginners/
With that out of the way, let us dive in.
Method 1: Ping
The simplest method to get the hostname from an IP address is to use ping. Ping is a simple yet powerful command-line utility that uses ECHO packets to communicate with a host.
NOTE: The following command only works in Windows machines. For Linux, check the next method.
To get a hostname from an IP address with ping, use the command below:
Here is the output from the above command:
The above command is not always reliable; it often only works if the hostname is available in the hosts file.
Learn more here:
https://linuxhint.com/modify-etc-host-file-linux/
https://linuxhint.com/edit-hosts-file-on-linux/
Method 2: Host Command
The second and common method for getting the hostname from the IP address in Linux is the host command. This simple tool is part of the dnsutil package.
To install the package, use the commands:
Ubuntu/Debian-based distros
$ sudo apt-get install dnsutils -y
REHL/CentOS
$ sudo yum install dnsutils
Fedora
$ sudo dnf install dnsutils
Arch
$ sudo pacman -S dnsutils
Once you have installed the tool, you can run the command below to get the hostname of an IP address.
An example output is below:
78.223.58.216.in-addr.arpa domain name pointer mba01s07-in-f14.1e100.net.
NOTE: Using the host command requires the system to be registered with a DNS server such as Cloudflare or Google Public DNS or an entry in the host file. Depending on the DNS server the system is configured with, the result may differ or none at all.
If you are using Fedora, ensure to persist your DNS settings before a reboot because the Network Manager often overwrites them.
Method 3: Using Dig
The next method you can try is to use dig. Dig is a command-line utility that is useful in performing DNS queries and reverse lookups. It is a powerful tool that has a collection of features other than looking up a hostname.
To learn more about dig, check out this tutorial.
https://linuxhint.com/install_dig_debian_9/
Once you have installed dig on your system, use the command:
It is also good to note that the server should have reverse DNS lookup enabled; otherwise, you will not get the server hostname.
Method 4: Nslookup
A far simpler and more common way to look up the hostname from an IP address is to use nslookup. Nslookup is a command-line utility, similar to dig, but that allows users to query DNS for hostnames and IP address mappings.
To query a hostname with nslookup, use the command as:
An example is as shown below:
110.223.58.216.in-addr.arpa name = mba01s08-in-f14.1e100.net.
Authoritative answers can be found from:
Conclusion
For this tutorial, we illustrated various ways to get the hostname from an IP address both in Linux and Windows machines. If you are looking for a more in-depth tutorial on how DNS works, consider the resource on the link provided below:
https://linuxhint.com/dns-for-beginners/