| 1 |
<?xml version='1.0'?> <!--*-nxml-*--> |
| 2 |
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" |
| 3 |
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> |
| 4 |
<!-- SPDX-License-Identifier: LGPL-2.1-or-later --> |
| 5 |
|
| 6 |
<refentry id="nss-myhostname" conditional='ENABLE_NSS_MYHOSTNAME'> |
| 7 |
|
| 8 |
<refentryinfo> |
| 9 |
<title>nss-myhostname</title> |
| 10 |
<productname>systemd</productname> |
| 11 |
</refentryinfo> |
| 12 |
|
| 13 |
<refmeta> |
| 14 |
<refentrytitle>nss-myhostname</refentrytitle> |
| 15 |
<manvolnum>8</manvolnum> |
| 16 |
</refmeta> |
| 17 |
|
| 18 |
<refnamediv> |
| 19 |
<refname>nss-myhostname</refname> |
| 20 |
<refname>libnss_myhostname.so.2</refname> |
| 21 |
<refpurpose>Hostname resolution for the locally configured system hostname</refpurpose> |
| 22 |
</refnamediv> |
| 23 |
|
| 24 |
<refsynopsisdiv> |
| 25 |
<para><filename>libnss_myhostname.so.2</filename></para> |
| 26 |
</refsynopsisdiv> |
| 27 |
|
| 28 |
<refsect1> |
| 29 |
<title>Description</title> |
| 30 |
|
| 31 |
<para><command>nss-myhostname</command> is a plug-in module for the GNU Name Service Switch (NSS) functionality of |
| 32 |
the GNU C Library (<command>glibc</command>), primarily providing hostname resolution for the locally configured |
| 33 |
system hostname as returned by |
| 34 |
<citerefentry><refentrytitle>gethostname</refentrytitle><manvolnum>2</manvolnum></citerefentry>. The precise |
| 35 |
hostnames resolved by this module are:</para> |
| 36 |
|
| 37 |
<itemizedlist> |
| 38 |
<listitem><para>The local, configured hostname is resolved to |
| 39 |
all locally configured IP addresses ordered by their scope, or |
| 40 |
— if none are configured — the IPv4 address 127.0.0.2 (which |
| 41 |
is on the local loopback) and the IPv6 address ::1 (which is the |
| 42 |
local host).</para></listitem> |
| 43 |
|
| 44 |
<listitem><para>The hostnames <literal>localhost</literal> and |
| 45 |
<literal>localhost.localdomain</literal> (as well as any hostname |
| 46 |
ending in <literal>.localhost</literal> or <literal>.localhost.localdomain</literal>) |
| 47 |
are resolved to the IP addresses 127.0.0.1 and ::1.</para></listitem> |
| 48 |
|
| 49 |
<listitem><para>The hostname <literal>_gateway</literal> is |
| 50 |
resolved to all current default routing gateway addresses, |
| 51 |
ordered by their metric. This assigns a stable hostname to the |
| 52 |
current gateway, useful for referencing it independently of the |
| 53 |
current network configuration state.</para></listitem> |
| 54 |
|
| 55 |
<listitem><para>The hostname <literal>_outbound</literal> is resolved to the local IPv4 and IPv6 |
| 56 |
addresses that are most likely used for communication with other hosts. This is the preferred source |
| 57 |
addresses of default gateways if specified, or determined by requesting a routing decision to the |
| 58 |
configured default gateways from the kernel and then using the local IP addresses selected by this |
| 59 |
decision. This hostname is only available if there is at least one local default gateway configured. |
| 60 |
This assigns a stable hostname to the local outbound IP addresses, useful for referencing them |
| 61 |
independently of the current network configuration state.</para></listitem> |
| 62 |
</itemizedlist> |
| 63 |
|
| 64 |
<para>Various software relies on an always-resolvable local |
| 65 |
hostname. When using dynamic hostnames, this is traditionally |
| 66 |
achieved by patching <filename>/etc/hosts</filename> at the same |
| 67 |
time as changing the hostname. This is problematic since it |
| 68 |
requires a writable <filename>/etc/</filename> file system and is |
| 69 |
fragile because the file might be edited by the administrator at |
| 70 |
the same time. With <command>nss-myhostname</command> enabled, |
| 71 |
changing <filename>/etc/hosts</filename> is unnecessary, and on |
| 72 |
many systems, the file becomes entirely optional.</para> |
| 73 |
|
| 74 |
<para>To activate the NSS modules, add <literal>myhostname</literal> to the line starting with |
| 75 |
<literal>hosts:</literal> in <filename>/etc/nsswitch.conf</filename>.</para> |
| 76 |
|
| 77 |
<para>It is recommended to place <literal>myhostname</literal> after <literal>files</literal> and before <literal>dns</literal>. |
| 78 |
This resolves well-known hostnames like <literal>localhost</literal> |
| 79 |
and the machine hostnames locally. It is consistent with the behaviour |
| 80 |
of <command>nss-resolve</command>, and still allows overriding via |
| 81 |
<filename>/etc/hosts</filename>.</para> |
| 82 |
|
| 83 |
<para>Please keep in mind that <command>nss-myhostname</command> (and <command>nss-resolve</command>) also resolve |
| 84 |
in the other direction — from locally attached IP addresses to |
| 85 |
hostnames. If you rely on that lookup being provided by DNS, you might |
| 86 |
want to order things differently. |
| 87 |
</para> |
| 88 |
</refsect1> |
| 89 |
|
| 90 |
<refsect1> |
| 91 |
<title>Example</title> |
| 92 |
|
| 93 |
<para>Here is an example <filename>/etc/nsswitch.conf</filename> file that enables |
| 94 |
<command>nss-myhostname</command> correctly:</para> |
| 95 |
|
| 96 |
<!-- synchronize with other nss-* man pages and factory/etc/nsswitch.conf --> |
| 97 |
<programlisting>passwd: files systemd |
| 98 |
group: files [SUCCESS=merge] systemd |
| 99 |
shadow: files systemd |
| 100 |
gshadow: files systemd |
| 101 |
|
| 102 |
hosts: mymachines resolve [!UNAVAIL=return] files <command>myhostname</command> dns |
| 103 |
networks: files |
| 104 |
|
| 105 |
protocols: db files |
| 106 |
services: db files |
| 107 |
ethers: db files |
| 108 |
rpc: db files |
| 109 |
|
| 110 |
netgroup: nis</programlisting> |
| 111 |
|
| 112 |
<para>To test, use <command>glibc</command>'s |
| 113 |
<citerefentry project='man-pages'><refentrytitle>getent</refentrytitle><manvolnum>1</manvolnum></citerefentry> |
| 114 |
tool:</para> |
| 115 |
|
| 116 |
<programlisting>$ getent ahosts `hostname` |
| 117 |
::1 STREAM omega |
| 118 |
::1 DGRAM |
| 119 |
::1 RAW |
| 120 |
127.0.0.2 STREAM |
| 121 |
127.0.0.2 DGRAM |
| 122 |
127.0.0.2 RAW</programlisting> |
| 123 |
|
| 124 |
<para>In this case, the local hostname is <varname>omega</varname>.</para> |
| 125 |
|
| 126 |
</refsect1> |
| 127 |
|
| 128 |
<refsect1> |
| 129 |
<title>See Also</title> |
| 130 |
<para><simplelist type="inline"> |
| 131 |
<member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> |
| 132 |
<member><citerefentry><refentrytitle>nss-systemd</refentrytitle><manvolnum>8</manvolnum></citerefentry></member> |
| 133 |
<member><citerefentry><refentrytitle>nss-resolve</refentrytitle><manvolnum>8</manvolnum></citerefentry></member> |
| 134 |
<member><citerefentry><refentrytitle>nss-mymachines</refentrytitle><manvolnum>8</manvolnum></citerefentry></member> |
| 135 |
<member><citerefentry project='man-pages'><refentrytitle>nsswitch.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry></member> |
| 136 |
<member><citerefentry project='man-pages'><refentrytitle>getent</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> |
| 137 |
</simplelist></para> |
| 138 |
</refsect1> |
| 139 |
|
| 140 |
</refentry> |