If you look at the manpage for hostname, may be it can point you to the right direction. Also Setting the Hostname and Configuring the Name Service and O'reilly's Name Service and Resolver Configuration from Linux Network Administrator's Guide, 2nd Edition explains the name resolution issue in linux very well. I guess that's enough good resources to help you start with.
The thing is, internet and the idea of name resolution and dns evolved with time and many things what we find today have historical reasons.
To answer your questions, you can set your hosts canonical name simply in /etc/hostname
file. The kernel can access the name through the hostname
command. Now this has nothing to do with dns in general.
The /etc/hosts
file exists for reasons to help name resolution for a handful of clients (along with the local system) and when there is no dns server available.
Now, how the linux system will decide about what to use for the name resolution is defined in the /etc/nsswitch.conf
file. An example partial content of the file may look like this:
# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# Information about this file is available in the `libc6-doc' package.
hosts: files dns
The first line with hosts
means, the system will consult the /etc/hosts
file first and then dns server for name resolution.
As you can see, if you have a working dns, you don't need to add the hostname entry in /etc/hosts
file. And which will have priority, is defined by the /etc/nsswitch
file.
The limitation with the /etc/hosts file is, it is visible to the local system only and one has to maintain it manually (editing/adding/removing ip/hostnames) and can not help other systems in the network to resolve hostnames from that file. This is how the idea of a centrlized hosts file that will be accessible to all computers in LAN, came in and from there evolved the idea of DNS server.