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

Utilities like host and dig let you see the IP address corresponding to the host name.

There is also the getent utility that can be used to query /etc/hosts or other NSS databases.

I am looking for a convenient standard utility (which is available in Debian, say) which resolves a host name regardless of where it is defined.

It should be more or less equivalent to

ping "$HOST" | head -1 | perl -lne '/\((.*?)\)/ && print $1'
share|improve this question
What exactly is wrong with getent? – Patrick Apr 5 '13 at 20:16
Yeah, I don't thing it could get more convenient than getent. – Bratchley Apr 5 '13 at 20:19
@Patrick: hmm, I didn't realise getent also looks in DNS (I thought it only looks in /etc/hosts). Now that I've tried it, here's what's wrong with getent: for google.com, it returns a single address, and that address is IPv6. Which is not helpful, since I'm on an IPv4 network, and my command would actually print an IPv4 address. – Roman Cheplyaka Apr 5 '13 at 20:48
@RomanCheplyaka - See eppesuig's answer that shows how to use getent to only look for IPv4 addresses: unix.stackexchange.com/a/71392/7453 – slm Aug 29 '13 at 18:51
up vote 3 down vote accepted

The only command that I am aware that does what you want is resolveip:


However it only comes with mysql-server, which may not be ideal to install everywhere.

share|improve this answer
Nice one, thanks! – Roman Cheplyaka Apr 5 '13 at 21:16

If the problem is that you do not want to resolve these names using ipv6, then just ask getent to use ipv4 only. This will enumerate all ipv4 addresses:

giuseppe@blatta:~$ getent ahostsv4 www.google.com | cut -d' ' -f1 | sort -u
share|improve this answer
For those who do care to forget history (i.e. IPv4), use ahostsv6. – Luc Jul 6 '15 at 21:18

The thing is there are several APIs to resolve host names like gethostbyname, getaddrinfo and inet_pton and some of those can return more than one address and/or you can query the type of address you want.

If you want a portable way to get one IPv4 address, then maybe:

perl -MSocket -le 'print inet_ntoa inet_aton shift' www.google.com
share|improve this answer

gethostip -d name.domain from the syslinux package on Ubuntu (and probably Debian). -d outputs decimal format.

share|improve this answer

(This answer only applies if you're root on the machine.)

I used to be annoyed by this too, and then I standardized on running dnsmasq on all my machines. Dnsmasq is a lightweight DNS cache. As a side benefit, it serves the content of /etc/hosts over DNS.

share|improve this answer

I used my pure perl knowledge and made a little script with error handling:



# inspired by: https://unix.stackexchange.com/questions/71379/host-lookup-that-respects-etc-hosts#71393

use strict;
use Socket;

my $name = $ARGV[0];
if ($name eq '') {
  print STDERR "Usage: gethostip <hostname>\n";
  exit 1;
my $ip = inet_aton($name);
die("Unable to resolve host name $name") if ($ip eq '');
my $ipstr = inet_ntoa($ip);
print "$ipstr\n";

Thx to Stéphane Chazelas for the initial idea

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.