gethostbyname() function for DNS lookup On Linux with C example.

DNS lookup is to resolve a hostname (in string format, eg ) into the actual IP address (e.g  The gethostbyname() function on Linux provides a way to do the lookup programmatically.   In this tutorial, we will explain the function along with a C program example.

Why do we need to resolve a host?

A computer network connects to each other over IP protocol. In the network, each node has an address to communicate with other nodes. The address could be a type of IPv4 or IPv6. An IPv4 address is a string separated by the dots(.) for example is a type of IPv4 address, the IPv6 address is separated by the “::” for example fe80::a883:444a:b088:cb4c.

So it’s obvious that before starting any communication, the hostname should be resolved to the actual IP address. The same happens when you browse a website, the first request from your laptop is a DNS lookup. Then web page request goes to the actual server using the resolved IP address.

gethostbyname() function prototype

The get host by name function is declared in the file netdb.h. It has the following prototype.

struct hostent *gethostbyname(const char *hostname);
  • Return Value – It return the pointer of the hostent structure type. The hostent structure having information of the translated address. If system function could not reolve the given hostname, NULL is returned and errno is set.
  • Argument – It takes the host name string as an argument.
  • Errors – In case of failures it could rerurn any one of the error, HOST_NOT_FOUND, NO_DATA, NO_RECOVERY and TRY_AGAIN.

Where gethostbyname() look for translation?

On Linux, the lookup query scan the /etc/hosts local directory. If it has the host entry, the corresponding Ip address is returned else request is sent to the gateway configured.   localhost localhost.localdomain localhost4 localhost4.localdomain4
 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

C Program for DNS lookup with gethostbyname() Function.

 include <stdio.h>
 include <string.h>
 include <stdlib.h>
 include <sys/socket.h>
 include <errno.h>
 include <netdb.h>
 include <arpa/inet.h> 

int resolveHost(char * hostname , char* ip);
 int main(int argc , char *argv[])
         if(argc <2)
                 printf("No hostname is given to resolve");
 char *hostname = argv[1];     
char ip[100];     resolveHost(hostname,ip);     
printf("%s resolved to %s" , hostname , ip);     

int resolveHost(char * hostname , char* ip)
         struct hostent *hent;
         struct in_addr **addr_list;
         int i;
 if ( (hent = gethostbyname( hostname ) ) == NULL)     
herror("gethostbyname error");             
return 1;     }     
addr_list = (struct in_addr **) hent->h_addr_list;     
for(i = 0; addr_list[i] != NULL; i++)     
strcpy(ip , inet_ntoa(*addr_list[i]));             
return 0;     
}     return 1;

Output-> Compile and Run with GCC Linux compiler.

# gcc -o getHostByName getHostaddr.c
# ./getHostByName localhost
 localhost resolved to
# ./getHostByName resolved to