Rate this page

Convert an IP address to the corresponding domain name

Tested on

Debian (Lenny)


To convert an IPv4 or IPv6 address to the corresponding domain name (if there is one)


Suppose that a network service has logged connection attempts from the IPv4 address In order to more easily identify the machine to which this address belongs, you wish to convert it to a domain name.

Similarly for the IPv6 address 2001:db8::89.


Query the hosts database using the getent command:

getent hosts

The request will be routed through the Name Service Switch (NSS). This is able to resolve addresses from the Domain Name System (DNS), from the hosts file (/etc/hosts), or using other mechanisms such as LDAP and NIS where appropriate.

If a matching domain is found then it is written to stdout preceded by the address:   www.example.com

If nothing was found then getent exits without writing anything to stdout (but with a non-zero return code). The method works equally well for IPv6 addresses:

getent hosts 2001:db8::89


Sometimes you may want to bypass the Name Service Switch and query the relevant directory service directly. This might be preferable to using getent if (for example) you wished to check that the DNS had been configured correctly, without any risk of being misled by the hosts file or the Name Service Cache Daemon (nscd).

See also

Further reading