Bash: using dig for reverse DNS lookup by IP

The dig utility is convenient for doing manual DNS resolution from your system.  Additionally, it uses the same OS resolver libraries as your applications which makes it more accurate than nslookup for emulating application issues and its output is more suitable for machine parsing.

# ensure 'dig' is installed
sudo apt install -y bind9-dnsutils

dig -x <IP> +noall +short

Note that you are unlikely to get the domain you expect when running a reverse IP lookup on a public internet address because shared infrastructure and generic server farms are typically serving the request.

But in a corporate or internal private network, this is much more likely to yield the expected results because hosts are built for single-tenancy or function.

Here is a Bash command that will loop through an IP range of a subnet, attempting reverse lookup in order to discover services.

# do reverse lookup for subnet range
prefix=192.168.1
for i in $(seq 32 254); do echo "$prefix.$i,$(dig -x $prefix.$i +noall +short)"; done

REFERENCES

dig man page

stackoverflow.com, IP to hostname is not 1-1 relationship

unix.stackexchange, dig versus nslookup