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?
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:
This setting will have the /etc/hosts file take precedence for name resolution:
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.