To be completely honest I do not like the public IP in /etc/hosts as well. However this is how DA (and many linux distros) behaved for ages, so it is hard to tell what exactly would break (or not break) if we would switch to using 127.0.0.1 there. DA installer adds a record with public IP it if it is not present as
@kristian pointed out.
The historical reason for having hostname in the
/etc/hosts
was to allow requests to the hostname to work even without network connectivity (access to DNS servers). Name resolution would short-circut with
/etc/hosts
instead of failing. This is not that big of an issue nowadays compared to decades ago. Internet access is now ubiquitous and server not having internet access is usesless anyway
.
The difference between having public IP vs having 127.0.0.1 is quite murky. If each and every service on your host is listening on
::
or
0.0.0.0
then it does not matter much. But once you start bounding services to listen on one particular IP you might run into problems.
If you have service listening on
127.0.0.1
, for example
redis
, and you have your public IP in
/etc/hosts
bound to your domain name you can reach it with
redis-cli -h localhost
, but trying
redis-cli -h server.example.net
would fail. This kind of works as expected if you think about it from normal DNS resolution perspecitve. We try connecting to the same IP that we would try there were no /etc/hosts entry at all or if we were connecting from 3rd party server. Same goes the other way around, if you have HTTP sever listening not on all interfaces but only on your local IP address connecting to it via localhost would not work, while connecting via FQDN would work.
When we start adding loopback IP and your hostname to
/etc/hosts
the behaviour becomes a bit less what people would expect. Using the redis example you would be able to connect to it with both
redis-cli -h localhost
and
redis-cli -h server.example.net
. But if you had HTTP server bound to your IP you would not able to reach from local machine using the FQDN, while other machines could reach it just fine. This behaviour could be very unexpected.
All of these /etc/hosts hacks are just trying to mimic what public DNS servers does. Ideally you should have the same records in
/etc/hosts
for your server as you have in your public DNS service. The idea of putting loopback IP there came for supporting laptops or workstations without fixed IP address. These machines does not have FQDN anyway so for the short hostname it is even better to resolve to 127.0.0.1 rather than try catching public IP addresses.
@kristian I a huge fan of the Debian way despite DA sometimes straying away from it ? .
Official Debian docs are saying:
Code:
...
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.
...
I personally would not write entries to the /etc/hosts at all and would always make DNS lookup if server name is used! However this approach clashes with how historically hostname -f is designed to work ?. It tries reading /etc/hosts as source of truth for FQDN!