Why not assign the hostname to the loopback address in /etc/hosts

hostnamehosts-file

So I understand that the hostname should (at least in Debian systems) be set in /etc/hostname. To get the FQDN (through hostname -f) the system finds the IP from the hostname through /etc/hosts and then returns the first entry in the line.

So if the hostname is server1 and this is in /etc/hosts:

192.0.2.1    server1.example.com    server1

It will return server1.example.com. So this is how it is desribed on many websites. But I was thinking: Why not assign the hostname to the loopback address? Like you do with localhost:

127.0.0.1    server1.example.com    server1    localhost

With this approach you don't have to know the external IP address. Also, applications that might use the FQDN will make the requests directly on the system instead of going through the network.

So, why not do it like this? Why are most examples on the internet using the external IP address?

Best Answer

It could be a bad idea, for several reasons

  • if you do have an ip (and communicate to other hosts), its highly recommended to put the hostname in front of the externally known ip.

    • Some protocols could say "tell the other guy your hostname and its ip address" "ok. Other guy, i'm foo.localnetwork(127.0.0.1)". The other guy will receive this packet with, at the IP level the external IP, but at the Protocol level, the 127.0.0.1 IP, so it could have a hard time to exchange if that protocol needs to use the advertised info instead of the IP level ones (SIP, for example, is likely to be problematic with this...)

    • Additionnaly some services bind only on the interface holding the ip associated to the hostname and therefore those services will only be able to talk with the host, via loopback device, no-one else...