3

We have a Debian 8.2 system foo provided by our IT department for production. Its /etc/hosts file contains these two lines:

127.0.0.1       localhost
127.0.1.1       foo.example.com   foo

This maps the FQDN of the system to 127.0.1.1, while the real IP address of the system is 10.5.1.38 (which is not given in hosts).

Is this correct or should /etc/hosts not contain the FQDN?

Note that the system is networked, has access to DNS and nslookup with the FQDN gives the correct IP address (10.5.1.38).

Martin Schröder
  • 939
  • 1
  • 10
  • 35
  • This has been discussed [before](http://unix.stackexchange.com/questions/77917/include-domain-name-or-not-in-etc-hosts), but overall, answers here are largely opinion-based. – Thomas Dickey Feb 15 '16 at 11:11
  • 2
    As I said in my answer, I have already seen real world problems due to this practice - this is more than an opinion. While it can be used in testing environments, I do not recommend using it in production environments. Cheers – Rui F Ribeiro Feb 15 '16 at 11:15
  • Is the second IP intended to be .0.1? – Jeff Schaller Feb 15 '16 at 11:31
  • @JeffSchaller: No. It really is `.1.1`. – Martin Schröder Feb 15 '16 at 12:26
  • 1
    @ThomasDickey: I'm looking for best practice here. – Martin Schröder Feb 15 '16 at 12:30
  • Then read where it's been discussed [before](http://unix.stackexchange.com/questions/77917/include-domain-name-or-not-in-etc-hosts), in greater detail. Reopening an old issue only attracts me-too's, which don't add useful information. – Thomas Dickey Feb 15 '16 at 13:01
  • @ThomasDickey: This question is really about the wisdom of having a loopback address with the fqdn in hosts. – Martin Schröder Feb 15 '16 at 13:35
  • I'm aware of that. You should have noticed that there were no useful answers. – Thomas Dickey Feb 15 '16 at 14:07
  • Your system's IP address is not 10.5.1.38, it may have an interface which such an address, but it also has another one (lo) with 127.0.0.1 (or any address within `127.0/8`) several IPv6 ones and potentially others. The FQdN should resolve to a network address which a host can use to contact your machine. That may be 10.5.1.38 from a machine on the local subnet attached to that interface of your machine, 3.4.5.6 from the internet, but for the machine itself, a most obvious one is one on the loopback subnet. It's better for 127.0.0.1 to reverse-resolve to "localhost" though, hence the 127.0.1.1 – Stéphane Chazelas Feb 15 '16 at 15:31
  • @StéphaneChazelas: Agreed. In *this* case the machine has a loopback interface with 127.0.0.1 and an `eth0` with `10.5.1.38` (and a local ipv6). – Martin Schröder Feb 15 '16 at 15:44

1 Answers1

5

I regard this as a bad practice, I have seen developers doing that. While it can be used in testing environments, I do not recommend using it in production environments.

By definition, the kernel has a very defined behaviour for the localhost.

There could be also problems too, I do remember having a service opened to the Internet that was not working, because the developer used the name in a config file, that was pointing to the loopback instead of the public address.

I my opinion your hosts files should be:

127.0.0.1       localhost
10.5.1.38       foo.example.com   foo
Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227