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

What is the easiest way to get the IP 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.

share|improve this question

migrated from 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

8 Answers 8

up vote 4 down vote accepted

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('');
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(""); 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.

share|improve this answer
Cool, that works. Esp., perl -MSocket -MData::Dumper -wle'my @addresses = gethostbyname(""); 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 at 3:32

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

It also can be used interactively, for example:

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


share|improve this answer
Came here to give this answer, up-voted you instead. – Not Now Oct 31 '11 at 23:17
Thanks. And updated for IPv6. – Tino Nov 1 '11 at 11:28

host <hostname>


serv ~ $ host 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
share|improve this answer
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
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

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

share|improve this answer
Nice, and works for IPv6, too. – Tino Mar 8 at 3:35

Well, my current solution:

ping -c1 -n | head -n1 | sed "s/.*(\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)).*/\1/g"
share|improve this answer
this doesn't give only the IP: PING ( 56 data bytes – klez 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

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

Why not dig +short hostname ?

(query DNS)

share|improve this answer
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
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
share|improve this answer
A bit more info on the various switches and why used would add to your answer – Dave M Aug 14 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 at 16:38

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.