[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Revisiting the hostname/FQDN issue, adding libnss-myhostname



Hi,

* Marc Haber <mh+debian-devel@zugschlus.de> [2025-10-13 22:22]:
The biggest question that I still have is why we are writing an /etc/hosts with "127.0.1.1 apollo.example.com apollo". Without that, the FQDN of the system is incorrect. But why 127.0.1.1? Arch Linux does it the same way, but they don't explain why, either.
Even though most people associate 127.0.0.1 with the loopback interface, the whole 127.0.0.0/8 block is actually reserved for that purpose, and *any* address in that block loops back to your own host (just for fun, try "ssh 127.234.56.78" if you have an SSH server running on your computer).

127.0.1.1 is not special, it merely happens to be an address that
a) maps to your own host, and
b) has no other name assigned to it which would conflict in a reverse lookup (127.0.0.1 is mapped to "localhost").

The usual "trick" to get your own domain name is via hostname canonicalization, i.e, you run gethostbyname("apollo"), which returns 127.0.1.1, and afterwards gethostbyaddr(127.0.1.1), which returns "apollo.example.com". The 127.0.1.1 entry in /etc/hosts makes this work and still lets the software on your computer behave exactly as if your computer had a static IP with a proper DNS entry, regardless of your actual network setup.

And why do we handle systems that get installed with IP autoconfiguration in a different way than we do for systems with their IP statically set. Should we not generate the 127.0.1.1 line even in the latter case? Do we, maybe?
I don't see any reason not to. I for one configured /etc/hosts that way on all servers I set up. Maybe someone else has additional insight.

If you ask me, the roaming client is the more problematic case, as you need to have a well-defined FQDN to generate the /etc/hosts entry. I suppose it can be a bit awkward if you want the domain name to be configured dynamically and the DHCP client has to rewrite /etc/hosts each time you connect to a network, which is probably why systemd is exploring alternative resolution mechanisms.


Cheers
Timo

--
⢀⣴⠾⠻⢶⣦⠀   ╭────────────────────────────────────────────────────╮
⣾⠁⢠⠒⠀⣿⡁   │ Timo Röhling                                       │
⢿⡄⠘⠷⠚⠋⠀   │ 9B03 EBB9 8300 DF97 C2B1  23BF CC8C 6BDD 1403 F4CA │
⠈⠳⣄⠀⠀⠀⠀   ╰────────────────────────────────────────────────────╯

Attachment: signature.asc
Description: PGP signature


Reply to: