linux - added entry to hosts file (ubuntu); can ping ok; cannot nslookup

  • user2897

    added entry to /etc/hosts

    can ping ok

    PING ( 56(84) bytes of data.
    64 bytes from localhost ( icmp_seq=1 ttl=64 time=0.100 ms

    but nslookup (and apache2 config) can't resolve the new name...

    ;; connection timed out; no servers could be reached
    sudo /etc/init.d/apache2 reload
    [error] (EAI 2)Name or service not known: Could not resolve host name > -- ignoring!

    note: cat /etc/host.conf

    order hosts,bind
    multi on

    what i ultimately want to achieve is for my apache2 config to be able to resolve the new local name (note: i configured a virtualhost for, but i can't get past the "resolve host name" problem shown above)


  • Answers
  • John T

    This is because the nslookup command works with a DNS, and your DNS can't find You can use the host command instead, which will interact with your hosts file.

    john@awesome:~$ host localhost
    localhost has address
    localhost has IPv6 address ::1
  • Thomas Bratt

    To use both the hosts file and a DNS Server, use getent. For example:

    getent ahosts <hostname>


    • As John T answered, nslookup will use a DNS server and the /etc/hosts file is not a DNS server
    • DNS configuration can be found in /etc/resolv.conf
    • Name service (including the hosts file) can be found at /etc/nsswitch.conf

  • Related Question

    linux - ping does not resolve the host - always appends a domain
  • dma_k

    The problem seems to be relatively easy, but I can't find good solution.


    I have local DHCP and DNS server running on ADSL router. It assigns IP addresses to local hosts and also keeps DNS records for assigned IPs.

    This modem also registers itself via DynDNS services.

    Let's assume I have no control over this modem, as it serves several groups.


    When I look the host via nslookup it works fine:

    $ nslookup vanja
    Name:   vanja

    but with ping it fails:

    $ ping vanja
    ping: unknown host vanja

    This happens, because ping appends the local domain to the host, but DNS server does not know this domain (and I have no ways to set it), see strace output:

    $ strace ping vanja
    open("/lib/i686/cmov/", O_RDONLY) = 4
    stat64("/etc/resolv.conf", {st_mode=S_IFREG|0644, st_size=23, ...}) = 0
    connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("")}, 28) = 0
    send(4, "\377N\1\0\0\1\0\0\0\0\0\0\5vanja\10dynalias\3com\0"..., 36, MSG_NOSIGNAL) = 36
    recvfrom(4, "\377N\201\203\0\1\0\0\0\1\0\0\5vanja\10dynalias\3com\0"..., 1024, 0, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("")}, [16]) = 97

    Also note that nslookup will also fail on the same reason: DNS only maps dynamically assigned IPs to short PC names (which are passed from MS Windows workstations).

    When I set the hostname to name without domain (# hostname centurion) ping magically starts working, but I cannot leave hostname not in FQDN form, as otherwise it may confuse apache & postfix or break other things.

    Question: How can I make ping working together with having hostname in FQDN form?

    Note: My attempts to play with search and domain options of /etc/resolv.conf haven't succeeded. My goal was to force NSS library not to append domain name to the passed argument, or, better, make two tries: without and with domain appended.

    Relative settings

    $ hostname
    $ cat /etc/resolv.conf
    $ grep hosts /etc/nsswitch.conf
    hosts:          files dns

  • Related Answers
  • dtmilano

    You may try

    search . domainname.ext

    to see if just adding the '.' works. Also

    $ ping vanja.

    would give you some clues.

  • Seasoned Advice (cooking)

    Your resolver search path needs to be set.

    In /etc/resolv.conf, add the line:

    search domainname.ext

    (Of course, replace above with your domain name).

    This will ensure that ping hostname also looks up hostname.domainname.ext. Note that, you can add multiple domain names to the search path if you want.