gethostip
Get IP address for given hostname
SYNOPSIS
gethostip [-4] [-6] [-d] [-r] [-f file] [-s server] [-h] [--version] [name-or-ip]
PARAMETERS
-4
Force IPv4 lookup only
-6
Force IPv6 lookup only
-d
Enable debug output for troubleshooting
-h, --help
Display usage help and exit
-r
Use DNS only, ignore /etc/hosts
-f file
Use alternative file instead of /etc/hosts
-s server
Query specific DNS server
--version
Print version information and exit
DESCRIPTION
gethostip is a simple, lightweight Linux utility for resolving hostnames to IP addresses (forward lookup) or IP addresses to hostnames (reverse lookup). It leverages standard C library functions such as gethostbyname(), gethostbyname2(), gethostbyaddr(), and getaddrinfo() to perform queries against DNS servers configured in /etc/resolv.conf or entries in /etc/hosts.
This tool is particularly useful in shell scripts for quick, reliable name resolution without the complexity or dependencies of fuller-featured DNS tools like dig or host. It supports both IPv4 and IPv6, with options to force one protocol, enable debugging, specify alternative hosts files or DNS servers, or restrict lookups to DNS only (bypassing /etc/hosts). Output is concise, typically showing the resolved address(es) and any aliases.
For example, running gethostip example.com might return IP addresses like 93.184.216.34, while gethostip 93.184.216.34 resolves back to the hostname. Debug mode (-d) provides verbose details on the lookup process, aiding troubleshooting. While not a replacement for advanced DNS diagnostics, its speed and simplicity make it ideal for automation and embedded systems.
CAVEATS
Not installed by default (requires gethostip package); relies on libc DNS functions, so limited to A/AAAA/PTR records; may fail on complex DNS setups without /etc/hosts fallback.
EXAMPLES
gethostip google.com
Resolve hostname to IPs (e.g., 142.250.191.78).
gethostip -4 -r 8.8.8.8
IPv4 reverse DNS only via configured servers.
gethostip -d localhost
Debug local resolution.
EXIT CODES
0: Success.
1: Lookup failed or invalid input.
2: Usage error.
HISTORY
Developed by Thomas Dreibholz in the early 2000s as part of his open-source networking tools; available via gethostip package on Debian/Ubuntu and source releases; focuses on portability and minimalism.
SEE ALSO
host(1), dig(1), nslookup(1), getent(1), resolvectl(1)


