NSS-MYHOSTNAME(8)

๐Ÿ’… NSS-MYHOSTNAME(8) โ€” like, the whole manual, bestie

okay so real quick, this page is from the nss-myhostname section 8 manual, and it's all about nss-myhostname and libnss_myhostname.so.2. yes girl, that long library name is the whole vibe. let's get into it ๐Ÿซถ


โœจ NAME โœจ

nss-myhostname, libnss_myhostname.so.2 โ€” hostname resolution for the locally configured system hostname โœจ (yes, that's the whole point, and it slays)


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

libnss_myhostname.so.2

that's it. that's the synopsis. she's lowkey minimal and I respect it ๐Ÿ’…


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so nss-myhostname is a plug-in module for the GNU Name Service Switch (NSS) functionality of the GNU C Library (glibc) โ€” ngl that's a whole mouthful, but what she actually does is provide hostname resolution for the locally configured system hostname as returned by gethostname(2). main character behavior fr fr ๐Ÿ‘‘

the precise hostnames this module resolves are, and I'm quoting the receipts:

  • the local, configured hostname gets resolved to all locally configured IP addresses, ordered by their scope โ€” or, if literally none are configured, then the IPv4 address 127.0.0.2 (which is on the local loopback, cute) and the IPv6 address ::1 (which is the local host, iconic)
  • the hostnames "localhost" and "localhost.localdomain" โ€” plus any hostname ending in ".localhost" or ".localhost.localdomain" โ€” get resolved to the IP addresses 127.0.0.1 and ::1 ๐Ÿ’–
  • the hostname "_gateway" gets resolved to all current default routing gateway addresses, ordered by their metric. so it 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 tbh โœจ
  • the hostname "_outbound" gets resolved to the local IPv4 and IPv6 addresses that are most likely used for communication with other hosts. this is the preferred source addresses of default gateways if specified, or determined by requesting a routing decision to the configured default gateways from the kernel and then using the local IP addresses selected by this decision. heads up though: this hostname is only available if there is at least one local default gateway configured! and yeah, this assigns a stable hostname to the local outbound IP addresses, useful for referencing them independently of the current network configuration state ๐Ÿฆ‹

okay so side quest, and this part is actually tea โ˜• โ€” various software relies on an always-resolvable local hostname. when using dynamic hostnames, this is traditionally achieved by patching /etc/hosts at the same time as changing the hostname. and that'sโ€ฆ problematic, bestie ๐Ÿ˜ญ it requires a writable /etc/ file system, and it's fragile because the file might get edited by the administrator at the same time. messy. with nss-myhostname enabled though, changing /etc/hosts is unnecessary, and on many systems the file becomes entirely optional. she ate and left no crumbs ๐Ÿ’…

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". this resolves well-known hostnames like "localhost" and the machine hostnames locally. it's consistent with the behaviour of nss-resolve, and still allows overriding via /etc/hosts. ๐ŸŒธ

โš ๏ธ 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. no cap, this one matters ๐Ÿ™ˆ

๐ŸŒˆ EXAMPLE ๐ŸŒˆ

here's an example /etc/nsswitch.conf file that enables nss-myhostname correctly โ€” copy the vibe, not my typos:

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, it's giving effortless:

$ 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. cute name, honestly ๐ŸŽ€


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

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


okay that's the whole page, you understood it perfectly, I'm so proud ๐Ÿฅน๐Ÿ’œ this was brought to you by systemd 259.10 โ€” mwah, go resolve something, bestie! โญ

๐Ÿ“ฆ where this page comes from
  • fedora 44: systemd 259.10-1.fc44 ยท LGPL-2.1-or-later AND MIT AND GPL-2.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms