Why does my hostname appear with the address 127.0.1.1 rather than 127.0.0.1 in /etc/hosts?

Solution 1:

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.

Solution 2:

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.


Solution 3:

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

Solution 4:

I read several answers around, and I'm frankly a bit confused.

I first tried to understand what is canonical hostname here.

What I can say is that @cmroanirgo is right in saying that on my Ubuntu laptop, invoking

hostname --fqdn 

with /etc/hosts configured like this

127.0.0.1   localhost 
127.0.1.1   laptop

returns laptop, whereas changing the file like follows

127.0.0.1   localhost laptop

returns localhost.

What impacts this can have, I have no idea, except the fact that if you have software that binds to hostname, and you need to talk to it internally to your machine, don't expect to be able to use the "localhost" hostname to do the job, in this setup, since they end up resolving two distinct separate addresses.