Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

When you call ping name.domain, it goes through both /etc/hosts and the DNS resolver to obtain an IP. It could be an IP hard-coded in /etc/hosts, or it could be one from the DNS server. It does so by calling getaddrinfo() or equivalent, not directly, of course.

How do I call getaddrinfo() from shell? How do I reproduce the effect of "normal" net utilities to obtain an IP from an address?

This is not about using dig/host which only go through DNS, or getent which only goes through hosts. I want to reproduce common application behavior (e.g. ping) when it receives a name it needs to resolve. There are other questions about dig/host. This question is not a duplicate of those.

Update: here are my findings (based partly on answers to other Qs)

  • on Ubuntu (and Debian?) there is gethostip -d name.domain from syslinux.
  • perl -MSocket -le 'print inet_ntoa inet_aton shift' name.domain works reliably and is terser than the accepted answer.
  • Using getent may also work: getent ahostsv4 name.domain | grep STREAM | head -1 | cut -f1 -d' '

This seems to be the best one can do.

share|improve this question

marked as duplicate by Patrick, jasonwryan, slm, Anthon, rahmu Nov 4 '13 at 3:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Was wondering if you were gonna ask this question 8-) – slm Nov 3 '13 at 18:24
Might be a reason you don't want to do this: unix.stackexchange.com/questions/67627/… – slm Nov 3 '13 at 18:25
I'll keep that in mind, meanwhile I need to debug existing behavior of existing applications. But please be sure to add this bit to your answer. – dan3 Nov 3 '13 at 18:26
What's the application you're debugging? – slm Nov 3 '13 at 18:27
Related: unix.stackexchange.com/questions/71379/… – slm Nov 3 '13 at 20:15
up vote 4 down vote accepted

If you have access to Perl I rolled my own (well found it on the internet and used it):

use Socket;

$host = shift @ARGV;
die("usage: gethostbyname hostname\n") unless(defined($host));

$packed_ip = gethostbyname($host);

if (defined $packed_ip) {
    $ip_address = inet_ntoa($packed_ip);
    print "$ip_address\n";
    exit 0
} else {
    warn "$host not found\n";
    exit 1

This code comes from this SF Q&A titled: Linux command line utility to resolve host names using /etc/hosts first.


$ ./gethostbyname.pl skinner

$ ./gethostbyname.pl www.google.com

$ ./gethostbyname.pl localhost

I've used the above method when code was running on multiple Unix machines, not just Linux, and so getent wasn't an option.


I know the man page for getent leaves you thinking that getent will only look in the file databases, but I believe it goes through whatever means are defined in /etc/nsswitch.conf. So if it states dns as a value there, then I believe it will interrogate the DNS server that's configured in /etc/resolv.conf. Assuming there is one defined in that file.

In my testing I do not have an entry in my file, /etc/hosts, for the host "skinner" and yet getent resolves it just fine via DNS.

$ getent hosts skinner     skinner.bubba.net

$ grep skinner /etc/hosts
share|improve this answer
I don't know what the hell getent hosts google.com returns: 2a00:1450:4001:806::10042a00:1450:4001:806::1004 – dan3 Nov 3 '13 at 19:24
OK, this is one answer (I could have also called getaddrinfo() or the obsolete gethostbyname() directly from C of course). Let's see if there's anything shorter. – dan3 Nov 3 '13 at 19:25
That's the IPv6 IP address. – slm Nov 3 '13 at 19:30
@dan3 - correct you can call the functions writing your own, I guess is my point. – slm Nov 3 '13 at 19:32
@dan3 - though obsolete it's likely in your stack of Perl, other ways may not be. – slm Nov 3 '13 at 19:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.