This may be a bit of a noobish question, but I was taking a look at /etc/hosts on my new Xubuntu install and saw this:

127.0.0.1 localhost
127.0.1.1 myhostname

On most 'nixes I've used, the second line is omitted, and if I want to add my hostname to the hosts file, I'd just do this:

127.0.0.1 localhost myhostname

Is there a difference between these two files in any practical sense?

  • I wonder can i just delete the second line or will some process in debian/ubuntu regenerate the line and add it back? – simgineer Apr 4 '17 at 20:45
up vote 148 down vote accepted

There isn't a great deal of difference between the two; 127/8 (eg: 127.0.0.0 => 127.255.255.255) are all bound to the loopback interface.

The reason why is documented in the Debian manual in Ch. 5 Network Setup - 5.1.1. The hostname resolution.

Ultimately, it is a bug workaround; the original report is 316099.

  • 2
    Exactly what I was looking for, thanks! – Tom Feb 23 '12 at 18:27
  • 5
    The actual reason: "Associating the system hostname with the latter had the unwanted effect of making 'localhost.localdomain' the canonical hostname associated with the system hostname. That is, 'hostname --fqdn' returned 'localhost.localdomain'." – cmroanirgo May 29 '15 at 20:58
  • I actually traced it back to this doc lists.debian.org/debian-boot/2005/06/msg00938.html and it appears that its because Thomas was feeling the need to use 1.1 instead of 0.1, which they are equivalent, correct me if im wrong, but that means this answer is really only a breadcrumb? – Brian Thomas Aug 2 '16 at 4:56
  • @BrianThomas that message suggests why they're not equivalent. They wanted localhost and myhostname to be distinct - otherwise, one would be an alias of the other. Note this doesn't work in IPv6 where there is only one loopback address. It can be avoided by using an NSS module as mentioned in the thread, because it can be more flexible than /etc/hosts and return different canonical names despite them having the same IP address. I know this because my system is configured using such an NSS module. – sourcejedi Jul 21 '17 at 17:09

To sum up the linked-to information:

  • It is (debatably) useful to have an entry in your /etc/hosts translating the machine's fully-qualified domain name into its permanent IP address.
  • debian-installer, and more specifically, its netcfg component, currently (up to March 2013 at least) creates this entry.
  • If the machine is not known to have a permanent IP address, the debian-installer still wants it to have that kind of entry.
  • The address 127.0.1.1 uses the loopback interface, answered by your own machine, just like 127.0.0.1 but is a distinct entry in /etc/hosts which can be considered separately from 127.0.0.1 if/when necessary.

Thomas Hood explains adding this entry as follows:

[This] will ensure that if the UNIX hostname is resolved then it will always be its own canonical hostname

but:

In the long run the UNIX hostname should not be put in /etc/hosts at all.

  • .. and so... ? must I user 127.0.0.1 for my fdqn? or 127.0.1.1? or the static lan ip? – realtebo Aug 27 at 21:46
  • 1
    @realtebo: 1. It's useful, and thus it's added by the installer. 2. There is no "must" here; and it's only relevant when your FQDN doesn't have a permanent address. – einpoklum Aug 27 at 22:29

I was curious myself, and i didnt like any of the other answer because they didnt seem to answer what i was looking for atleast.

The Answer: Looking back at this doc it almost appears as if Thomas was stating is giving it another dedicated ip on the loopback allows it to be canonical.

Both point to your loopback. Using the following 127.0.1.1 is an actual IP, on the loopback, whereas 127.0.0.1 is either the device itself, or another ip on the loopback. Both end up on the same subnet, representing the loopback, but are separated by ip. They are equivalent dns wise, but separated because of having dedicated ip.

The point being, you can have all your entries on one line like this

127.0.0.1 localhost localhost.domain www.myfakednsname.com myakednsname.com 

If your hostname is local, meaning doesn't have a global internet DNS entry mapped to an actual internet ip, then in this case Thomas was saying you NEED TO have the 2nd entry line, like this to dedicate it there (to canonical).

127.0.0.1 localhost localhost.localdomain
127.0.1.1 myfakednsname
  • in all fairness, after i finally composed this to a way that made sense to me, i see thats pretty much what einpoklum was trying to explain it looks like in hindsight. – Brian Thomas Aug 2 '16 at 5:15
  • OK negative vote, not sure why. I still believe THIS is the BEST answer, or i wouldnt have put it, after landing here. the original answer helped, but didnt capture all the details. I would go with This one. – Brian Thomas Aug 10 '16 at 20:05

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.