in reply to /etc/hosts is not evaluated after modification on Linux

I am trying to find out, if a given hostname can be resolved and if not, I update a hosts file and check if the resolution works again.

Very strange approach. It seems you are working in a network with private IP addresses, and have a nameserver that does not "know enough" about that network. Why don't you update the data the nameserver uses instead of messing with /etc/hosts, probably on many computers?

Another question: How to you update records in /etc/hosts when needed, e.g. when test_host moves from 192.168.0.50 to 192.168.0.55? Do you touch each and every /etc/hosts file?

And a third thing: There should be no underscore (_) in a hostname (see Wikipedia for the relevant RFCs). It may work, but only by chance.

UPDATE:

Maybe you don't have a nameserver at all? Maybe you are abusing gethostbyname() to find a hostname in /etc/hosts? If yes, consider using dnsmasq. It provides DNS and DHCP services (optionally also TFTP), and adding hostnames to the DNS service is as easy as editing /etc/hosts - literally, because you simply edit /etc/hosts on the machine running dnsmasq. dnsmasq "watches" /etc/hosts, so you don't have to do more than editing the file.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^2: /etc/hosts is not evaluated after modification on Linux
by Anonymous Monk on May 31, 2015 at 09:02 UTC

    Yes there is no nameserver in this particular example and /etc/hosts is used for resolution.

    How already pointed by philipbailey nscd is used for caching and if after updating of /etc/hosts in the script cache needs to be invalidated by using "nscd -i hosts"