gethostbyname2 - Linux


gethostbyname2 is a powerful Linux command used to resolve the canonical name of a given hostname and its IPv6 address. It’s essential for network applications that require accurate IP address and hostname resolution for effective communication.


gethostbyname2 <hostname> [AF_INET6]


  • hostname: The hostname to resolve.
  • AF_INET6 (optional): Specifies IPv6 address resolution. If omitted, IPv4 is used by default.


  • -a: Print the resolved IP addresses in a human-readable format.
  • -c: Enable canonical name lookup, resolving the hostname to its fully qualified domain name (FQDN).
  • -i: Display the dotted quad IP address only.


Simple IPv4 resolution:

$ gethostbyname2

IPv6 resolution:

$ gethostbyname2 AF_INET6

Canonical hostname lookup with IP address display:

$ gethostbyname2 -c -a
IP addresses:

Common Issues

  • Hostname not found: Ensure the provided hostname is valid and accessible.
  • Invalid Address Family: Specify the correct address family (AF_INET for IPv4 or AF_INET6 for IPv6).
  • DNS lookup failure: Check DNS server connectivity and ensure the hostname exists in the DNS records.


gethostbyname2 can be integrated into scripts and command chains to automate hostname and IP address resolution tasks. For example:

IP=$(gethostbyname2 $HOSTNAME)
echo "The IP address of $HOSTNAME is: $IP"

Related Commands

  • host: Retrieves hostname information, including aliases and IP addresses.
  • dig: Performs DNS lookups and displays detailed DNS records.
  • nslookup: Interactively queries DNS servers for hostname and IP address information.

For more information, refer to the official Linux man pages: