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'
  • 5
    What exactly is wrong with getent? – Patrick Apr 5 '13 at 20:16
  • 2
    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

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.

| improve this answer | |

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
| improve this answer | |
  • For those who do care to forget history (i.e. IPv4), use ahostsv6. – Luc Jul 6 '15 at 21:18
  • 3
    For those who want to avoid considering the differences between IPv4 and IPv6, use getent ahosts google.com. – Mikko Rantalainen Oct 2 '17 at 10:02

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

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

| 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.

| 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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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