What is the easiest way to get the IP address from a hostname?

I was thinking about trying a ping and parse it from the output. However, that doesn't seem very nice and will probably not work the same way on all systems.

I searched a bit around and found solutions with nslookup, but that doesn't work for hostnames in /etc/hosts.

migrated from stackoverflow.com Aug 15 '10 at 15:29

This question came from our site for professional and enthusiast programmers.

  • I still wonder a bit why Unix shells are not considered as programming languages... – Albert Aug 16 '10 at 3:11
  • Because shell traditionally is seen as scripting, not programming ;) – Tino Mar 28 '13 at 17:10

You can do this with standard system calls. Here's an example in Perl:

use strict; use warnings;
use Socket;
use Data::Dumper;

my @addresses = gethostbyname('google.com');
my @ips = map { inet_ntoa($_) } @addresses[4 .. $#addresses];
print Dumper(\@ips);

produces the output:

$VAR1 = [

(On the command-line, the same script can be written as: perl -MSocket -MData::Dumper -wle'my @addresses = gethostbyname("google.com"); my @ips = map { inet_ntoa($_) } @addresses[4 .. $#addresses]; print Dumper(\@ips)')

You can do this similarly in other languages -- see the man page for the system calls at man -s3 gethostbyname etc.

  • Cool, that works. Esp., perl -MSocket -MData::Dumper -wle'my @addresses = gethostbyname("www.google.com"); my @ips = map { inet_ntoa($_) } @addresses[4 .. $#addresses]; print $ips[0]'. – Albert Aug 14 '10 at 17:44
  • Very strange that this answer is some program code... :) Looks almost like a Stackoverflow answer. Doesn't really belong on Serverfault. But I'll accept the answer anyway. – Albert Aug 16 '10 at 3:13
  • @Albert: well to be fair: 1. the question was posted on SO originally and migrated to SF, and 2. the type of data you're looking for needs to be parsed with something; some people consider Perl a better form of shell script :D – Ether Aug 28 '10 at 19:03
  • @Ether sed -e '/some people consider Perl a better form/s/better/worse/' SCNR – Tino Mar 8 '15 at 3:32

host <hostname>


serv ~ $ host stackoverflow.com
stackoverflow.com has address


On Linux, (and some OS X variants, at least), you might be able to use resolveip, which is part of the MySQL server package:

 ...     localhost localhost.localdomain foo

serv ~ $ resolveip foo
IP address of foo is
  • Also does not work. (For hosts in /etc/hosts.) Try host localhost. – Albert Aug 14 '10 at 17:01
  • serv ~ $ host localhost \n localhost has address – Amber Aug 14 '10 at 17:07
  • Hm, well, not here. I'm curious why that works for you. Or why it does not for me. – Albert Aug 14 '10 at 17:09
  • 2
    See my edit - you might try resolveip. – Amber Aug 14 '10 at 17:13
  • Hm, resolveip is not installed by default on my Debian. – Albert Aug 14 '10 at 17:16

This ancient post seem to have many creative solutions.

If I need to make sure also /etc/hosts gets accessed, I tend to use

getent hosts somehost.com

This works, at least if `/etc/nsswitch.conf' has been configured to use files (as it usually is).

  • Nice, and works for IPv6, too. – Tino Mar 8 '15 at 3:35
  • And doesn't need installing syslinux! – Claudiu Mar 21 '16 at 19:18
  • Interesting that for some IPs it returns nothing while ahosts does return multi-line info. – akostadinov Nov 3 '17 at 23:59

For IPv4 there is a standard program which works out of the box using the resolver including /etc/hosts:

ip="`gethostip -d "$host"`"

It is part of Debian, install it with:

apt-get install syslinux

For other protocols than IPv4 (like IPv6) I currently don't know a similar tool. Update: Because of this I just wrote a small tool which is capable to resolve IPv6, too:


It is thought for a quick and dirty shell use like gethostip but allows IPv6, too:

ip="`ipof -6 -- heise.de`"

It also can be used interactively, for example:

ipof -a -d -x -v -h -


  • Came here to give this answer, up-voted you instead. – Not Now Oct 31 '11 at 23:17

Well, my current solution:

ping -c1 -n www.google.com | head -n1 | sed "s/.*(\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)).*/\1/g"
  • this doesn't give only the IP: PING www.l.google.com ( 56 data bytes – Federico klez Culloca Aug 14 '10 at 17:18
  • Yea, seems that sed behaves slightly different on each system. :) How annoying. I changed it a bit, I think it should work everywhere now. – Albert Aug 14 '10 at 17:19

Why not dig +short hostname ?

(query DNS)

  • It does not take /etc/hosts into account. – Janne Pikkarainen Nov 1 '11 at 13:38
  • DNS is supposed to be a definitive source for Name Resolution, and if you use the host file to override, that's fine. But that's an override, not definitive... – gWaldo Nov 1 '11 at 15:06
  • I know, but the original question wanted /etc/hosts :) – Janne Pikkarainen Nov 1 '11 at 15:14
  • sure enough; my reading comprehension fail... – gWaldo Nov 1 '11 at 16:49

On some Unices, the following will work:

arp <hostname>

For example on Mac OS X, I get this:

arp My-iMac.local
My-iMac.local ( -- no entry
  • Well, it finds the entry but it only lists its MAC, not its IP. :) – Albert Aug 14 '10 at 17:14
  • Ah, arp -n hostname shows the IP. – Albert Aug 14 '10 at 17:14
  • Only, this way doesn't work for servers outside the network. :P – Albert Aug 14 '10 at 17:15

Using ping is not that bad since you generally do not have any strong dependencies.

Here is the function I used on Linux systems :

getip () { ping -c 1 -t 1 $1 | head -1 | cut -d ' ' -f 3 | tr -d '()' ; }
nmap -sP|grep SEARCHED_HOSTNAME|sed -n 's/.*[(]\([0-9\.]*\)[)].*/\1/p'
  • Nmap gets from your subnet ( or whatever) the adresses
  • with grep get only the line of the hostname you are looking for
  • With sed get only the ip address inside the parentheses
  • 2
    A bit more info on the various switches and why used would add to your answer – Dave M Aug 14 '15 at 15:33
  • There are at least four different reasons why this answer is not going to work. You make assumptions about the range of IP addresses within which the answer will be found. It is inefficient due to producing a lot more network traffic than needed. It only works for IP addresses which responds to probes. It assumes reverse DNS contains the exact same mappings as forward DNS. – kasperd Aug 14 '15 at 16:38

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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