Often new system administrators need to find a hostname or domain from a given IP address in Linux. It might seem like a difficult task but it quite easy. This process is called reverse DNS lookup. Generally, operating systems and browsers lookup IP address of a given domain name. This is called DNS lookup. In this article, we will cover how to do reverse DNS lookup. In other words, we will learn how to get hostname/domain name from IP address in Linux.
How to Get Hostname/Domain Name from IP Address in Linux
The most basic requirement is that the IP address you are targeting must, in fact, have a hostname/domain name. It is defined using an A Record that basically maps a hostname/domain name with the IP. It is generally defined with a domain registrar such as GoDaddy, Namecheap, etc. Once you have verified this, there are several tools you can use to do a reverse lookup on an IP address.
1. Using dig
Domain Information Groper (Dig) is a powerful tool that allows you to query DNS records. You can use it to obtain a wide range of information about an IP or domain, such as CNAME, A Record, MX and SOA records.
Here is the simple command to get hostname/domain name of an IP address. Replace <ip_address> with IP address whose hostname/domain name you want to find out.
$ dig -x <ip_address> +noall +answer
Here is an example to get hostname/domain name of 54.43.32.21 using dig command.
$ dig -x 54.43.32.21 +noall +answer
If dig is not present on your system, run the following commands in your terminal to install dig on your system.
# Debian/Ubuntu systems $ apt update $ apt install dnsutils # RHEL/CentOS/SUSE $ sudo yum install bind-utils
2. Using NSLookup
You can also use nslookup command to fetch CNAME, A, MX, and other type of DNS querying. Here is the command to get hostname/domain name of host IP address.
$ nslookup <ip_address>
Here is an example to get domain name of ip 54.43.32.21.
$ nslookup 54.43.32.21
If nslookup is not present on your system, run the following commands in your terminal to install nslookup on your system.
# Debian/Ubuntu systems $ apt update $ apt install dnsutils # RHEL/CentOS/SUSE $ sudo yum install bind-utils
It is the same command as the one mentioned above to install dig, because bindutils and dnsutils install both dig and nslookup on your system.
3. Using Host Command
You can also use host command to get hostname/domain name form IP address. Here is the command to get hostname/domain name using host command.
$ host <ip_address>
Here is an example to get hostname/domain name from IP address 54.43.32.21.
$ host 54.43.32.21
Again, if you don’t have host command on your system, you need to install dnsutils on Debian/Ubuntu , and bind-utils on RHEL/CentOS/SUSE systems.
# Debian/Ubuntu systems $ apt update $ apt install dnsutils # RHEL/CentOS/SUSE $ sudo yum install bind-utils
In this article, we have learnt how to do reverse DNS lookup in Linux using dig, nslookup and host commands to get hostname/domain name from IP address in Linux. It is important to note that you can also use these commands for forward DNS lookups also, where you find out the IP address of a hostname/domain name. Each of these commands provides many useful options to customize your tasks. You can refer to their man documentation for this purpose.
Also read:
How to Check if String Matches Regular Expression
How to Modify XML File in Python
Plot Graph CSV Data in Python
How to Encrypt Password in Python
How to Create Thumbnail in Python
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.