NSS-MYHOSTNAME(8)

โœจ NSS-MYHOSTNAME(8) โœจ

okay bestie, gather round, because this is the little glibc plug-in that makes your own machine's hostname Just Work, and it lives in section 8 of the manual (that's the system-administration girlies ๐Ÿ’…). It's called nss-myhostname, the actual library file is libnss_myhostname.so.2, and it does hostname resolution for the hostname your system has locally configured. No cap.


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

libnss_myhostname.so.2

๐Ÿ’– DESCRIPTION ๐Ÿ’–

So nss-myhostname is a plug-in module for the GNU Name Service Switch (NSS) functionality of the GNU C Library (glibc). Its main character job? Hostname resolution for the locally configured system hostname โ€” the one you get back from gethostname(2). ๐Ÿ’ซ

And the exact hostnames it resolves are these, bestie, write them down:

  • The local, configured hostname resolves to all locally configured IP addresses, ordered by their scope. And if, like, none are configured at all? Then it resolves to the IPv4 address 127.0.0.2 (which is on the local loopback) and the IPv6 address ::1 (which is the local host). iconic fallback behaviour fr fr ๐ŸŒธ
  • The hostnames "localhost" and "localhost.localdomain" โ€” plus any hostname ending in .localhost or .localhost.localdomain โ€” resolve to the IP addresses 127.0.0.1 and ::1. ๐Ÿ’œ
  • The hostname "_gateway" resolves to all current default routing gateway addresses, ordered by their metric. This assigns a stable hostname to the current gateway, which is useful for referencing it independently of the current network configuration state. obsessed with the stability ngl ๐Ÿฆ‹
  • The hostname "_outbound" resolves to the local IPv4 and IPv6 addresses that are most likely used for communication with other hosts. How does it know? It's the preferred source addresses of default gateways if those are specified, or else it's determined by requesting a routing decision to the configured default gateways from the kernel, and then using the local IP addresses selected by that decision. Heads up though: this hostname is only available if there is at least one local default gateway configured. This assigns a stable hostname to the local outbound IP addresses, useful for referencing them independently of the current network configuration state. ๐Ÿ‘‘

okay so here's the โœจloreโœจ of why this exists at all. Various software out here relies on having an always-resolvable local hostname. When you're using dynamic hostnames, the traditional way people did this was by patching /etc/hosts at the exact same time as changing the hostname. And likeโ€ฆ that's problematic, bestie. ๐Ÿ˜ญ It requires a writable /etc/ file system, and it's fragile because the file might be edited by the administrator at the same time. Chaos. With nss-myhostname enabled, changing /etc/hosts becomes unnecessary, and on many systems the file becomes entirely optional. the way that fixes everythingโ€ฆ ๐Ÿ’…

To activate the NSS modules, add "myhostname" to the line starting with "hosts:" in /etc/nsswitch.conf. ๐Ÿซถ

It is recommended to place "myhostname" after "files" and before "dns". That resolves well-known hostnames like "localhost" and the machine hostnames locally. It's consistent with the behaviour of nss-resolve, and it still allows overriding via /etc/hosts. slay ๐Ÿ’–

โš ๏ธ Please keep in mind that nss-myhostname (and nss-resolve) also resolve in the other direction โ€” from locally attached IP addresses to hostnames. So if you rely on that lookup being provided by DNS, you might want to order things differently. lowkey important, don't skip this one ๐Ÿ™ˆ

๐ŸŒˆ EXAMPLE ๐ŸŒˆ

Here is an example /etc/nsswitch.conf file that enables nss-myhostname correctly:

passwd:         files systemd
group:          files [SUCCESS=merge] systemd
shadow:         files systemd
gshadow:        files systemd

hosts:          mymachines resolve [!UNAVAIL=return] files myhostname dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis

To test, use glibc's getent(1) tool:

$ getent ahosts `hostname`
::1       STREAM omega
::1       DGRAM
::1       RAW
127.0.0.2       STREAM
127.0.0.2       DGRAM
127.0.0.2       RAW

In this case, the local hostname is omega. โญ


๐ŸŒธ SEE ALSO ๐ŸŒธ

systemd(1), nss-systemd(8), nss-resolve(8), nss-mymachines(8), nsswitch.conf(5), getent(1)


and that's the whole page, bestie!! this one came from systemd 262.1, and it's the nss-myhostname(8) manual page ๐Ÿ’– go forth and resolve your own hostname, you absolute icon ๐Ÿ‘‘โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: systemd-libs 262.1-1 ยท LGPL-2.1-or-later AND CC0-1.0 AND GPL-2.0-or-later WITH Linux-syscall-note
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms