I am using the following commands to get the domain name using the IP address

dig -x IPaddress

But it gives me some host names like ns1 etc. Is there any bash command that can give the actual domain name?


Using the +short option of dig you can get only the dns name.

dig -x +short | awk -F '.' '{print $2"."$3}'

There's probably a nicer way to reassemble it with awk Or just return the DNS Domain.

Have a read through the QUERY OPTIONS in man dig


When I do it I get something like this

dig +short -x $(dig +short example.com)

I get


This is not the original domain name, because the domain name system has no easy way to look it up, as the real name is not registered in the database, however the one that was returned was. It would be to expensive to ask every DNS server in the world.

In addition there is a many to one relationship. Each IP can have many domain names (sometimes a domain name has many IP addresses, used for load balancing).

  • There is no need to ask every DNS server in the world. The domain name system has a way to look it up, and it's not more complicated than a forward lookup. In fact, it's very similar: instead of looking up, say, google-public-dns-b.google.com, the PTR record for the domain name is searched hierarchically starting from arpa. – Johan Myréen May 18 '18 at 16:03
  • @Yes sorry If I suggested that it can not be done, I only mean that It can not be done, if it is not entered into the database. Searching the world is what would be needed to do it in reverse, but we don't have to do this, as sometimes there reverse lookup is added to the database. – ctrl-alt-delor May 18 '18 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.