About '/etc/hosts'

I've noticed the same pattern for the contents of /etc/hosts for a long time (don't remember when I first noticed it).

The file always contains the following 2 entries:

  • 127.0.0.1 localhost
  • 127.0.1.1 ${HOSTNAME}

What I don't know is the rationale behind having the two distinct IP addresses, rather than having only the one shared address. I remember using the form

  • 127.0.0.1 localhost ${HOSTNAME}

decades ago in a multinational corporate environment.

Some explanation is given in this ServerFault posting, which then points to this Debian Manual Page, but it still doesn't address the need for the two to be distinct, instead of being shared.

Does anyone know for sure?

The main thing is that, for specifying a "local" host web address in a software configuration file, should I be using

or

  • https://${HOSTNAME}:${portNum}/${pathToFile} ?

The below text is not directly related to my primary issue/concern stated above.

( === START of "suppressed" posting === )

Summary

Also of note, using "localhost", there is no "security" issue.

However, using ${HOSTNAME}, Firefox is reporting a security issue:

and further details offered:

Correction: I had omitted the required "s" for https, so there is no discrepancy in behaviour between the use of localhost or ${HOSTNAME}, but I am still getting a report of "not secure", likely because I did not personally configure any security. :slight_smile:

( === END of "suppressed" posting === )

1 Like

On a whim, I "asked Leo" (the AI for the Brave Browser) and this is the response I got:

The official and recommended format for the /etc/hosts file is to have one entry per IP address. So, instead of having one entry for 127.0.0.1 with multiple hostnames, it's better to have two separate entries:

127.0.0.1 localhost
127.0.0.1 ${HOSTNAME}

This format makes it easier to manage and troubleshoot the file.

Asking for the source, Leo responded

The recommended format for the /etc/hosts file is specified in the IANA Hosts File Format and the Unix Hosts File Format on Wikipedia.

In the IANA Hosts File Format, it's stated that:

"Each line is a comment, if it starts with a #, otherwise it is a hostname followed by one or more aliases in the following format: IP_address canonical_hostname [aliases...]"

This implies that one entry per IP address is the recommended format.

My reading doesn't follow Leo's reasoning, but the idea of troubleshooting separate entries makes sense.

3 Likes

Very interesting, Fred! Thank you for those details. Much appreciated.

1 Like

According to the

Some software (e.g., GNOME) expects the system hostname to be resolvable to an IP address with a canonical fully qualified domain name. This is really improper because system hostnames and domain names are two very different things; but there you have it. In order to support that software, it is necessary to ensure that the system hostname can be resolved. Most often this is done by putting a line in /etc/hosts containing some IP address and the system hostname. If your system has a permanent IP address then use that; otherwise use the address 127.0.1.1.

        127.0.0.1 localhost
        127.0.1.1 uranus
5 Likes

There is more to that. Look, ifconfig shows only loopback interface having 127.0.0.1 address. One can ping as assigned 127.0.0.1, as an unassigned 127.0.1.1. When noted, that makes one try to ping any 127.x.x.x address. And then one can see that packet counters on loopback interface grow after pinging unassigned addresses from 127.0.0.0/8.

In the end of the day one can conclude, that using 127.0.1.1 for the hostname is just an arbitrary decision without some hidden wisdom behind it.

4 Likes

In the end, it all comes down to Dorothy, in the Wizard of Oz: "There's no place like 127.0.0.1!"

3 Likes