Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

9 Answers 9

up vote 29 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
This does not work on OS X, so this is not 'somewhat portable'. – user239558 Feb 23 at 21:51
Question is tagged linux. – womble Feb 24 at 5:00
Ah, my bad. The confused language in this question also confuses Google. – user239558 Feb 24 at 7:57

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 at 21:52

"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 prever 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

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.