Problem scenario
You notice that on some Linux servers the /etc/hosts file controls the resolution of hostnames and on other servers the DNS server overrides the /etc/hosts file. Which has precedence in DNS resolution, /etc/hosts or the DNS server on the network?
Answer
It depends. The /etc/nsswitch.conf file will decide. There is a "hosts" stanza. This setting will have the DNS server override the /etc/hosts file:
hosts:dns files
This setting will have the /etc/hosts file take precedence for name resolution:
hosts:files dns
To get the hostname, there is a systemcall called gethostname() which can help you understand how the hostname is retrieved and determined. To learn more, see this posting.