5

In my /etc/hosts file, I have two lines:

127.0.0.1 localhost
127.0.1.1 hostname

What is the difference between the two lines, where do I find the host name?

Stefan Lasiewski
  • 22,949
  • 38
  • 129
  • 184
user482004
  • 59
  • 1
  • 2
  • 4
    127.0.0.0/8 (so 127.0.0.1 - 127.255.255.255) is reserved for your host's loopback device. – cburn11 Aug 06 '18 at 16:13
  • Your host's hostname is set in /etc/hostname. – cburn11 Aug 06 '18 at 16:25
  • 2
    There are RFCs that cover the `127.0.0.0/8` block. See [this question](https://networkengineering.stackexchange.com/q/50583/8499). – Ron Maupin Aug 06 '18 at 16:30
  • @cburn11 Your comment doesn't explain the difference between the two IPs. – Stefan Lasiewski Aug 06 '18 at 17:28
  • @StefanLasiewski There is a bit of difference and not a bit of difference between the two IPs. – cburn11 Aug 06 '18 at 19:19
  • @cburn11 Please only post comments which are relevant to the question, and avoid comments which muddy the waters. See the FAQ [How do comments work](https://meta.stackexchange.com/a/19757/148318), which says _"Comments are often used to ask for clarification on, suggest corrections to, and provide meta-information about posts."_ – Stefan Lasiewski Aug 06 '18 at 23:14

2 Answers2

4

The other answer quotes an obsolete bogus reference which doesn't match the link and doesn't look like it will be corrected. Here is the correct quote from today's Debian Reference Manual:

5.1.1. The hostname resolution

For example, "/etc/hosts" looks like the following.

127.0.0.1 localhost
127.0.1.1 <host_name>

[...]
Each line starts with a IP address and it is followed by the associated hostname.

The IP address 127.0.1.1 in the second line of this example may not be found on some other Unix-like systems. The Debian Installer creates this entry for a system without a permanent IP address as a workaround for some software (e.g., GNOME) as documented in the bug #719621.

The <host_name> matches the hostname defined in the "/etc/hostname".

For a system with a permanent IP address, that permanent IP address should be used here instead of 127.0.1.1.

For a system with a permanent IP address and a fully qualified domain name (FQDN) provided by the Domain Name System (DNS), that canonical
<host_name>.<domain_name> should be used instead of just <host_name>.

For the curious in bug #719621 there's a link to a discussion list discussing about the very same subject "127.0.0.1 vs. 127.0.1.1". It also mentions that Fedora uses 127.0.0.2 for the same purpose. Indeed it appears a slightly different mechanism is in use on Fedora for this.

A.B
  • 9,037
  • 2
  • 19
  • 37
1

For example, in Section 10.5 of the Debian Reference Manual:

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.​

https://www.debian.org/doc/manuals/debian-reference/ch05.en.html#s-net-dns

Cory Knutson
  • 1,866
  • 12
  • 20
  • 2
    you write "section 10.4" then link section 5. (indeed 5.1.1 deals about 127.0.1.1). 10.4 is about handling source code – A.B Aug 06 '18 at 16:59
  • Correct. Copy and paste hehe... – fvillalba89 Aug 06 '18 at 17:59
  • So, the `127.0.1.1 hostname` line is a placeholder, and is used if the host wasn't assigned an permanent IP address, and should be replaced with a permanent IP address if possible. Contrast that to the `127.0.0.1 localhost` line, which should always exist. – Stefan Lasiewski Aug 06 '18 at 22:28
  • @CoryKnutson hem... so 10.4 was changed into 10.5 (10.5. Version control systems), not 5 (Chapter 5. Network setup) or 5.1.1 (5.1.1. The hostname resolution) ? This quote is from a probable very old Debian ch 10.4 DNS chapter that doesn't exist on debian.org anymore (eg: http://qref.sourceforge.net/Debian/reference/ch-gateway.en.html#s-net-dns) – A.B Aug 07 '18 at 05:53
  • OK, so should I revert my edit? OP can reject it also. – Cory Knutson Aug 07 '18 at 21:29
  • 1
    @CoryKnutson The quote from the OP doesn't match the link provided by the OP anyway. So 10.4 is from an other link not provided, with a text that was completely changed (into nowadays 5.1.1), 10.5 is never ok. OP should juste quote again from the correct link he made (using section 5.1.1). as is this information is hard to corroborate with the link – A.B Aug 08 '18 at 13:52