2

On linux systems, I've seen many variations of /etc/hosts configurations with regards to the loopback address, localhost, and server aliases. Some include the servername as an alias for the localhost/loopback address (127.0.0.1), and others don't. I'm trying to figure out which of these is the most "correct", and what issues incorrect setups might cause.

For example, for an imaginary server named alice, with an FQDN of alice.example.com, and an IP of 192.168.42.42, I've seen these /etc/hosts configurations (and more):

Option 1:
127.0.0.1 alice.example.com alice localhost.localdomain localhost
192.168.42.42 alice.example.com alice

Option 2:
127.0.0.1 localhost.localdomain localhost alice.example.com alice
192.168.42.42 alice.example.com alice

Option 3:
127.0.0.1 localhost.localdomain localhost alice
192.168.42.42 alice.example.com alice

Option 4:
127.0.0.1 localhost.localdomain localhost
192.168.42.42 alice.example.com alice

So of these options, which is most correct? I tend to go with option 4 myself, but I have no justifiable reasoning for this. I'm particularly suspicious about option 1, since doesn't that set the FQDN machine name as canonical for 127.0.0.1 as well as 192.168.42.42? I'm not sure how that's different than, for instance, option 2 where it's just an "alias" for 127.0.0.1 instead.

nezroy
  • 122
  • 6

1 Answers1

1

I think of this question as, "how should the local machine name a given IP address?". The short answer might be that in the modern world none of this really matters since everybody just does dns lookups all the time anyway.

Still, I recognize there are lots of ways that /etc/hosts is still useful. I tend to go with option 4 out of habit because it seems simplest. Hopwever now that I look at it, something like option 3 seems most correct. If you don't have dns, you need to answer two basic questions:

  1. what IP address does the local machine name map to?
  2. what IP address does localhost map to?

If you don't have dns and are relying entirely on /etc/hosts, you also probably don't care about the fqdn. In that case, Option 3 allows your machine to answer both questions 1 and 2 with the loopback IP address, which you can assume is always alive and working. I guess there is a chance that the real IP address for the machine might not be configured on an interface, causing that network path to fail.

Thus my poorly-reasoned answer is to pick #3 because it answers the basic questions and is most robust in the face of uncertain network configurations.

This is really one of those questions that seem simple but the longer you think about it the more complex it gets.

Phil Hollenback
  • 14,647
  • 4
  • 34
  • 51