/usr/bin/host not picking up changes to /etc/hosts even after reboot

6,615

Solution 1

The host command doesn't check the hosts file. From the manpage:

host is a simple utility for performing DNS lookups.

If you want to test lookups while respecting the hosts file, then use ping or getent.

$ tail -1 /etc/hosts
127.0.0.1   google.com
$ ping -c1 google.com | head -1
PING google.com (127.0.0.1) 56(84) bytes of data.
$ getent ahosts google.com
127.0.0.1       STREAM google.com
127.0.0.1       DGRAM  
127.0.0.1       RAW    

Solution 2

The host utility is used for DNS lookups. It doesn't care about hosts files or non-DNS methods of resolving a hostname. If you want to see how your system would resolve a hostname under normal circumstances (taking nsswitch.conf into account), you can use getent. The host utility should be reserved for DNS testing. Here is an example:

$ host foobar.com
foobar.com has address 69.89.31.56
foobar.com mail is handled by 0 foobar.com.
$ getent hosts foobar.com
10.188.14.16    foobar.com

Solution 3

Programs like dig, host and nslookup query the DNS only. They don't query other sources of host names such as /etc/hosts, NIS or LDAP.

In most setups, the easiest way to smoothly add host names locally is to run a DNS server. Running a DNS cache is a good idea anyway for performance. In other words, the fact that no DNS server is running locally is something to fix rather than something to work around.

Dnsmasq is a common choice: it's widely available, small (it's used on many Linux-based routers) and easy to configure. Dnsmasq caches DNS requests and can serve additional names from a hosts file. It also provides a basic DHCP server suitable for small networks, but you don't have to use that part. If you have an isolated machine, run Dnsmasq on it. If you have a local network, run Dnsmasq (or some other equivalent software that's already there) on your router. See How to make a machine accessible from the LAN using its hostname for more information including how to set up Dnsmasq.

Share:
6,615

Related videos on Youtube

0xC0000022L
Author by

0xC0000022L

human father bibliophile geek & ~nerd misanthropic philanthropist skeptic code necromancer programmer reverse engineer (RCE) / software archaeologist / grayhat hacker moderator on reverseengineering system administrator FLOSS enthusiast Debian, FreeBSD and Ubuntu aficionado

Updated on September 18, 2022

Comments

  • 0xC0000022L
    0xC0000022L almost 2 years

    I have a Ubuntu Server 12.04 (amd64) machine on which, when I change /etc/hosts, the changes aren't picked up, even after a reboot. I am using /usr/bin/host to test, but none of the other programs seems to pick it up either.

    This is a server and nscd and dnsmasq aren't installed. Also, the file /etc/nsswitch.conf contains the line:

    hosts:          files dns
    

    so that I would expect it to work. I also checked that the mtime of the file changes with editing and tried running service networking restart (against all odds) and also resolvconf -u.

    All commands where run as root where needed. The machine has network configured manually in /etc/network/interfaces and not via Network Manager (it isn't installed either).

    Basically what I want to achieve is that the IP for a few hosts can be manipulated. The reason being that inside our network I get an IP to which I have no route, but I can use the external IP for that service via HTTPS.

    What am I missing?

    Note: no DNS server is locally running and the nameserver lines in /etc/resolv.conf (and the respective lines in interfaces) point to the DNS server that gives me the wrong IP.

    Also note: I've searched on the web and read through the "similar questions", but my case doesn't seem to be covered.

    /etc/host.conf is:

    # The "order" line is only used by old versions of the C library.
    order hosts,bind
    multi on
    
  • jordanm
    jordanm over 10 years
    @0xC0000022L That statement is incorrect. If you look at the manpage it uses gethostbyaddr(3), which is a libc function.
  • jordanm
    jordanm over 10 years
    @0xC0000022L libc functions are not system calls, so they won't show up in strace. Only things in the "2" section of manpages would show up in strace.
  • jordanm
    jordanm over 10 years
    @0xC0000022L - also, since getent is part of libc on my system, it wouldn't make any sense for it to not use the library it is distributed with.