Super User is a question and answer site for computer enthusiasts and power users. 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

Ubuntu 10.10+

In my script I need to lookup an IP for a given host name.

If that name is listed in /etc/hosts, then command should print IP from /etc/hosts, not from DNS server.

What commands I tried (nslookup, dig, host), completely ignore /etc/hosts — at least for names that are not known to the DNS server.

Note: I would prefer solution that would not require me to grep /etc/hosts by hand.

share|improve this question
up vote 16 down vote accepted

getent uses the low-level glibc information functions to query all configured sources.

$ getent ahosts  STREAM  DGRAM  RAW    
$ getent ahosts
2001:4860:b009::69 STREAM
2001:4860:b009::69 DGRAM  
2001:4860:b009::69 RAW    
share|improve this answer
getent hosts is probably a little simpler – higuita Oct 22 '15 at 13:35
$ gethostip localhost
localhost 7F000001
$ gethostip -d

From the syslinux package, at least in Ubuntu 12.04.

share|improve this answer

This is super-hacky, but I've been using it for ages, and it works (for ipv4):

function ipfor() {
  ping -c 1 $1 | grep -Eo -m 1 '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';

Use like: ipfor

share|improve this answer
Hacky but portable. Me like. – luis.espinal Jul 22 '14 at 14:11

I simply use the following as replacement for inapt 'host' cmd. This automatically will do the right thing with some restrictions (IPv4 only).


#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>

#define TOIN(a) ((struct sockaddr_in *)&(a))

main(argc, argv)
    char **argv;
    int err;
    struct sockaddr sa;
    char hbuf[NI_MAXHOST];

    if (argc <= 1) {
        printf("more args\n");
    TOIN(sa)->sin_family = AF_INET;
    if (inet_pton(AF_INET, *(argv + 1), &TOIN(sa)->sin_addr) != 1) {
        printf("can't inet_pton: %s\n", errno ? strerror(errno) : "format err");
    if (err = getnameinfo(&sa, sizeof(struct sockaddr_in), hbuf, sizeof hbuf, 0, 0, NI_NAMEREQD)) {
//        printf("%s\n", gai_strerror(err));
        printf("Host %s not found: 3(NXDOMAIN)\n", *(argv + 1));
    } else {
        printf("%s\n", hbuf);
share|improve this answer
nmap -sP|grep SEARCHED_HOSTNAME|sed -n 's/.*[(]\([0-9\.]*\)[)].*/\1/p'

No DNS Query

share|improve this answer
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Raystafarian Aug 14 '15 at 16:18
While this may indeed answer the question it would be good to explain how and why it does so. A command line with little or no explanation as to what it is doing may not help future visitors who might need to solve a similar problem. – Mokubai Aug 15 '15 at 7:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .