Skip to content

Instantly share code, notes, and snippets.

@gengwg
Created April 14, 2023 01:24
Show Gist options
  • Save gengwg/f34ba9b62725a3725573df3d41df2cd7 to your computer and use it in GitHub Desktop.
Save gengwg/f34ba9b62725a3725573df3d41df2cd7 to your computer and use it in GitHub Desktop.
"host" Command Does Not Observe /etc/nsswitch.conf

Problem

Despite modifying my hosts file, the DNS address for hosts is still displayed when using the host command. For instance:

$ grep -E "hosts|ipnodes" /etc/nsswitch.conf
hosts: files dns mymachines myhostname

$ grep google.com /etc/hosts
2401:db00:25ff:c83:: google.com

$ host google.com
google.com has address 142.250.217.78
google.com has IPv6 address 2607:f8b0:400a:80a::200e
google.com mail is handled by 10 smtp.google.com.

Root cause

The BIND 9.3 command host does not follow the /etc/nsswitch.conf policy.

$ host -V
host 9.11.36-RedHat-9.11.36-8.el8
$ cat /etc/redhat-release
CentOS Stream release 8

nslookup, dig, and host are tools for querying DNS name servers. They don't follow nsswitch.

$ dig AAAA google.com +short
2607:f8b0:400a:80a::200e
$ nslookup google.com
Server:		2401:db00:eef0:a52::
Address:	2401:db00:eef0:a52::#53

Non-authoritative answer:
Name:	google.com
Address: 142.250.217.110
Name:	google.com
Address: 2607:f8b0:400a:80a::200e

What command works?

ping

$ ping google.com
PING google.com(google.com (2401:db00:25ff:c83::)) 56 data bytes
^C
--- google.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1061ms

getent

$ getent hosts google.com
2401:db00:25ff:c83:: google.com

python

>>> import socket
>>> socket.getaddrinfo('google.com', None, socket.AF_INET6)
[(<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('2401:db00:25ff:c83::', 0, 0, 0)), (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('2401:db00:25ff:c83::', 0, 0, 0)), (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_RAW: 3>, 0, '', ('2401:db00:25ff:c83::', 0, 0, 0))]

resolveip

resolveip follows nsswitch, but only works for IPv4.

$ resolveip google.com
IP address of google.com is 1.2.3.4

Ref

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment