Server Fault is a question and answer site for system and network administrators. 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

There are several command line utilities to resolve host names (host, dig, nslookup), however they all use nameservers exclusively, while applications in general look in /etc/hosts first (using gethostbyname I believe).

Is there a command line utility to resolve host names that behaves like a usual application, thus looking in /etc/hosts first and only then asking a nameserver?

(I am aware that it would probably be like 3 lines of c, but I need it inside of a somewhat portable shell script.)

share|improve this question
Could you please explain your situation a little more? Does awk '/hostname/ { print $1 }' /etc/hosts help? – quanta Aug 22 '11 at 11:08
@quanta Actually the current solution is grep/sed magic on /etc/hosts. I wanted to make that more general with a fallback. – Zulan Aug 22 '11 at 11:20
up vote 34 down vote accepted

This is easily achieved with getent:

getent hosts

getent will do lookups for any type of data configured in nsswitch.conf.

share|improve this answer

One tool that would work is getent. So you could use getent hosts, or getent hosts localhost. It will retrieve entries from the databases as specified in your Name Service Switch configuration /etc/nsswitch.conf.

share|improve this answer
Yes, but that would not fall back on DNS. – slowpoison Mar 19 '12 at 19:55
No, it resolves it in nsswitch.conf order. – cjc Mar 19 '12 at 19:58
@slowpoison, Take a look at your nsswitch config. My system has files dns for hosts, which means /etc/hosts is consulted and then the DNS resolver. Your config may be different. – Zoredache Mar 19 '12 at 20:07
@cjc, it does. I don't think I tried it correctly. – slowpoison Mar 19 '12 at 20:07
@Zoredache, I'm quite impressed with getent. Thanks for the intro to this command. – slowpoison Mar 19 '12 at 20:11

You could use [your favorite language here] to write a script that calls getnameinfo. That is how binaries (like ping) should be doing it, so you're ensured you get the same treatment.

share|improve this answer

resolveip will do this.

Oddly, it's part of the mysql-server packages on RHEL and Ubuntu.

share|improve this answer

You can use a gethostbyname() (deprecated) wrapper like:

python -c 'import socket;print socket.gethostbyname("")'

Or a getaddrinfo() wrapper like:

python -c 'import socket;print socket.getaddrinfo("","http")[0][4][0]'

Note that getaddrinfo will return all instances as a list. The last part of the command selects only the first tuple. This can also return IPv6 addresses.

share|improve this answer
This will work, but it's been obsolete for a while. See – Kyle Smith Mar 19 '12 at 21:01
Thank you, I did not know about it. ;) – Mircea Vutcovici Mar 20 '12 at 0:53
Have an upvote. No other semi-portable one-liner has been proposed. – user239558 Feb 23 '15 at 21:52
In case this is of value to anyone else, I made a Python 3 version with a few command-line options: – Chris Adams Feb 2 at 22:42

"gethostbyname" command line version:

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
share|improve this answer

You could be really hacky and use arp:

arp -n somehostname | tr -d '()' | awk '{print $2}'

but that would be really ugly so you shouldn't do that.

share|improve this answer

Try this:

if [ `grep -c "hostname" /etc/hosts` -ge 1 ]; then
    ip=`awk '/hostname/ { print $1 }' /etc/hosts`
    ip=`host hostname | awk '/hostname has address/ { print $4 }'`
share|improve this answer

getent hosts is broken. It prefers IPv6 addresses, gai.conf should be configured to prefer ipv4 but....

The Perl gethostbyname uses the precedence in /etc/nsswitch.conf

hosts: files dns

So this works like getent hosts should work for me.


perl -e 'use Socket; print inet_ntoa(inet_aton("")) . "\n";'

should work.

share|improve this answer
It's not broken, it just doesn't use getaddrinfo (which reads /etc/gai.conf). To use getaddrinfo, run getent ahosts. – craig65535 Oct 12 '15 at 18:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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