Understand hostname and /etc/hosts

hostnamehosts

In /etc/hostname, I have myname.

In /etc/hosts, I have :

127.0.0.1   localhost.localdomain   localhost myname
::1     localhost.localdomain   localhost myname

I have a vague understanding of what really is a hostname and what it is used for.

These more precise questions could help me to understand it better:

  1. What really is my hostname in the example above? myname or something else? If I had instead myname.domain.com in /etc/hostname, what would be my real hostname then?

  2. In the example, the command hostname returns myname, whereas the command hostname -f returns localhost.localdomain. I had in mind that /etc/hosts was only a mapping between names and ip addresses. It seems to serve another function here. What is a fqdn and what is it used for? How is it retrieved? Why is it localhost.localdomain?

  3. If I had instead myname.domain.com in /etc/hostname, hostname -f would return myname.domain.com. Why?

  4. Why aren't fqdn ending with a dot in these files?

  5. Are hostname -d and dnsmydomain equivalent?

  6. In what context can I use my hostname (myname) and when do I have to use my fqdn (localhost.localdomain)?

Best Answer

  1. /etc/hostname contains name of the machine, as known to applications that run locally. /etc/hosts and DNS associate names with IP addresses. myname may be mapped to whichever IP address the machine can access itself, but mapping it to 127.0.0.1 is unæsthetic.
  2. Not /etc/hosts, but /bin/hostname serves another function with -f.
  3. Because /etc/hosts can override the common sense. Edit it with caution and don’t leave garbage there after temporary patches and experiments.
  4. Both styles (⋯.TLD. and ⋯.TLD) are acceptable. The former is unambiguous, whereas the latter is ubiquitous.
  5. Don’t know what dnsmydomain is, but unlikely. Seeking something in /etc/hosts isn’t technically “DNS”.
  6. Remember the point 1. Hostname – for applications that run locally; also for such peculiar network protocols as SMB (samba). FQDN (if not a fakery or placeholder like localhost.localdomain) – for accessing the machine from outside. FQDN must be resolvable where it is used.
Related Question