{"id":163,"date":"2024-01-24T14:27:23","date_gmt":"2024-01-24T21:27:23","guid":{"rendered":"https:\/\/dnstrouble.crider.dev\/?docs=common-problems-ssh-login-hangs-delays\/the-name-service-switch-nsswitch"},"modified":"2024-01-24T14:32:24","modified_gmt":"2024-01-24T21:32:24","slug":"the-name-service-switch-nsswitch","status":"publish","type":"docs","link":"https:\/\/dnstrouble.crider.dev\/?docs=common-problems-ssh-login-hangs-delays\/the-name-service-switch-nsswitch","title":{"rendered":"The Name Service Switch (nsswitch)"},"content":{"rendered":"\n<p>nsswitch is what determines the order in which further components are queried for the domain. This is controlled in<strong> \/etc\/nsswitch.conf <\/strong>in the &#8216;<strong>hosts<\/strong>&#8216; entry:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat \/etc\/nsswitch.conf | grep hosts<br># Valid databases are: aliases, ethers, group, gshadow, hosts,<br>hosts: files dns myhostname<\/code><\/pre>\n\n\n\n<p>In this example we see 3 entries &#8212; files, dns, myhostname.<br><br>This means it will check files first. What files? This is answered in the nsswitch.conf man pages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># man nsswitch.conf\n----\n       The following files are read when \"files\" source is specified for respective databases:\n\n           aliases     \/etc\/aliases\n           ethers      \/etc\/ethers\n           group       \/etc\/group\n           hosts       \/etc\/hosts\n           initgroups  \/etc\/group\n           netgroup    \/etc\/netgroup\n           networks    \/etc\/networks\n           passwd      \/etc\/passwd\n           protocols   \/etc\/protocols\n           publickey   \/etc\/publickey\n           rpc         \/etc\/rpc\n           services    \/etc\/services\n           shadow      \/etc\/shadow\n---<\/code><\/pre>\n\n\n\n<p>As noted, <strong>the specific file that is read for the &#8216;hosts&#8217; nsswitch entry is \/etc\/hosts<\/strong>. So it will first try \/etc\/hosts to see if there is a defined DNS entry for www.redhat.com<\/p>\n\n\n\n<p>For example, if we had this defined in \/etc\/hosts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1.2.3.4 www.redhat.com<\/code><\/pre>\n\n\n\n<p>Our flow would be:<\/p>\n\n\n\n<p>command\/application -> glibc -> nsswitch -> \/etc\/hosts<\/p>\n\n\n\n<p>And \/etc\/hosts would send the 1.2.3.4 value back to us as an answer.<\/p>\n\n\n\n<p>If it does not find an entry in \/etc\/hosts, it then moves on to the next entry &#8212; &#8216;dns&#8217;.<\/p>\n\n\n\n<p><strong>When &#8216;dns&#8217; is defined in nsswitch &#8216;hosts:&#8217;, it uses &#8216;resolver&#8217; functions from the glibc library to search what is defined in \/etc\/resolve.conf, and sends the queries to the nameservers defined.<\/strong><\/p>\n\n\n\n<p>So if for example \/etc\/resolv.conf has this nameserver defined:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nameserver 8.8.8.8<\/code><\/pre>\n\n\n\n<p>The full &#8216;flow&#8217; of the outgoing www.redhat.com query is as follows:<\/p>\n\n\n\n<p>command\/application -> glibc -> nsswitch -> \/etc\/hosts ->  \/etc\/resolv.conf -> 8.8.8.8<\/p>\n\n\n\n<p>And the DNS server at 8.8.8.8 would then be expected to send us the domain records for www.redhat.com.<\/p>\n\n\n\n<p>The DNS server at 8.8.8.8 could be any kind of DNS server &#8212; it could be systemd-resolved, dnsmasq, unbound, BIND DNS server, or some other local hosted server. Regardless, the nameserver is where the outgoing path ends.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"featured_media":0,"parent":37,"menu_order":1,"comment_status":"open","ping_status":"closed","template":"","doc_tag":[],"_links":{"self":[{"href":"https:\/\/dnstrouble.crider.dev\/index.php?rest_route=\/wp\/v2\/docs\/163"}],"collection":[{"href":"https:\/\/dnstrouble.crider.dev\/index.php?rest_route=\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/dnstrouble.crider.dev\/index.php?rest_route=\/wp\/v2\/types\/docs"}],"replies":[{"embeddable":true,"href":"https:\/\/dnstrouble.crider.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163"}],"version-history":[{"count":1,"href":"https:\/\/dnstrouble.crider.dev\/index.php?rest_route=\/wp\/v2\/docs\/163\/revisions"}],"predecessor-version":[{"id":165,"href":"https:\/\/dnstrouble.crider.dev\/index.php?rest_route=\/wp\/v2\/docs\/163\/revisions\/165"}],"up":[{"embeddable":true,"href":"https:\/\/dnstrouble.crider.dev\/index.php?rest_route=\/wp\/v2\/docs\/37"}],"next":[{"title":"\/etc\/resolv.conf","link":"https:\/\/dnstrouble.crider.dev\/?docs=common-problems-ssh-login-hangs-delays\/etc-resolv-conf","href":"https:\/\/dnstrouble.crider.dev\/index.php?rest_route=\/wp\/v2\/docs\/166"}],"prev":[{"title":"The first step: glibc","link":"https:\/\/dnstrouble.crider.dev\/?docs=common-problems-ssh-login-hangs-delays\/outgoing-flow","href":"https:\/\/dnstrouble.crider.dev\/index.php?rest_route=\/wp\/v2\/docs\/157"}],"wp:attachment":[{"href":"https:\/\/dnstrouble.crider.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/dnstrouble.crider.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fdoc_tag&post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}