90

I am just learning using Ubuntu and when I tried to configure the DNS, I need to deal with something call localhost, loopback interface. Can anyone explain me the differences between them and the meaning of the addresses 127.0.0.1 and 127.0.1.1, when should I use 127.0.0.1 and when should I use 127.0.1.1

2

4 Answers 4

62

As your machine gets started, it will need to know the mapping of some hostnames to IP addresses before DNS can be referenced. This mapping is kept in the /etc/hosts file. In the absence of a name server, any network program on your system consults this file to determine the IP address that corresponds to a host name.

The loopback device is a special, virtual network interface that your computer uses to communicate with itself. It is used mainly for diagnostics and troubleshooting, and to connect to servers running on the local machine.

The loopback is interface lo and the IP address is 127.0.0.1 by default.

127.0.0.1 localhost

Debian, then Ubuntu, choose to define 127.0.1.1 for mapping the IP address of your host_name in case that you have no network.

The host_name matches the hostname defined in /etc/hostname.

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

127.0.1.1 host_name
1
  • 1
    I understand the philosophy, but I disagree with the form of it in /etc/hosts. Jul 1, 2019 at 14:53
38

It doesn't really mean anything in particular (it's just another IP in the 127.0.0.0/8 block).

The reason it's used as the IP for your hostname is explained in Section 10.4 of the Debian Reference Manual.

Quote:

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.

6

The following screen capture tells an interesting story.

Ubuntu default /etc/hosts file and ifconfig output

Effectively, it shows that 127.0.0.1 is the true loopback address. Thus, 127.0.1.1 is there for reasons other than that of basic IP networking.

As mentioned by @Videonauth, the 127.0.1.1 line is there as a crutch, a fix, to make finicky software be happy.

In my view, the loopback entry should almost always be in /etc/hosts. If you change your hostname from the default of localhost, then change the host name on the 127.0.1.1 line, not the 127.0.0.1 line.

If you change your hostname and give your machine a static IP, change both the hostname and IP fields in the 127.0.1.1 line.

In short, leave the 127.0.0.1 record alone.

3
  • 2
    So to sum up, if you have applied a static ip say, 192.168.1.1 then the 127.0.1.1 line should be replaces with 192.168.1.1 hostname.Is that correct? Sep 19, 2019 at 20:58
  • 2
    192.168.1.1 <hostname> Yes. Sep 21, 2019 at 23:28
  • Thank you, I wasnt sure, im setting up a few vm's and the host files all had that in, i understood it was a loopback for some applications but then so was 127.0.0.1, so i wasnt sure if it had to stay or not. Sep 22, 2019 at 1:05
2

127.0.0.1 is the loopback address, commonly known as localhost, which is the name that resolves to it by the local hosts file. This file is in different locations depending on the OS, but in most Linux distros it is in /etc/hosts. This address always refers to the computer which made the request, so 127.0.0.1 is not a "real" address so to speak. Each device has it's own localhost. 127.0.1.1 on the other hand is used by certain software to mean the same thing, an alias in a way, but not really. The loopback address used by your machine internally should be 127.0.0.1, so don't mess around with that address in your hosts file!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .