0

This is on my home LAN which has a DNS server (Bind9) and a DHCP server (dhcpd).

My desktop machine gets its IP address from the DHCP server which includes a search domain of mylan.

So, the FQDN of my desktop machine is mydesktop.mylan.

Debian convention seems to be that the /etc/hostname should just contain the host's shortname, so mydesktop.

But what about /etc/hosts?

What should the entry be, short or FQDN?

127.0.0.1  localhost
127.0.1.1  mydesktop.mylan  mydesktop

Is there a convention? A recommendation?

If the FQDN is in the /etc/hosts won't that "override" the DNS resolution? Does that matter?

Bridgey
  • 123
  • 5

2 Answers2

1

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.

Diamond
  • 9,001
  • 3
  • 24
  • 38
0

I have always configured my /etc/hosts file with both the local hostname and FQDN.

Per the /etc/hosts manpage:

   127.0.1.1       thishost.mydomain.org  thishost
   192.168.1.10    foo.mydomain.org       foo
   192.168.1.13    bar.mydomain.org       bar
   146.82.138.7    master.debian.org      master

If the FQDN is in the /etc/hosts won't that "override" the DNS resolution? Does that matter?

Yes, your hosts file will always override DNS resolution, as it is the first thing checked by your local DNS resolver. However this should not be an issue in any network configuration I can think of, unless you need your client to ask the DNS server to resolve the client's own hostname for some reason.

zymhan
  • 1,371
  • 1
  • 17
  • 30
  • Thanks @WildVelociraptor! Before I accept as the answer, would you mind clarifying some points. I have working DNS so why would I add `foo`, `bar` and `master` (as per the example)? Wouldn't I leave that up to the DNS server? Also, re "your client should always convert `mydesktop` to `mydesktop.mylan`", when I ping `mydesktop` it actually pings 127.0.1.1, whereas if I ping `mydesktop.mylan` (the FQDN) it pings the DHCP IP 192.168.1.10. Presumably because the short name isn't being converted to the FQDN?? Thanks so much for your help! – Bridgey Mar 18 '16 at 23:40