resolvectl and getent
On modern Linux an app's name lookup goes through NSS (/etc/nsswitch.conf) and then usually to systemd-resolved. resolvectl controls and inspects systemd-resolved (per-link DNS, cache, split DNS); getent looks names up exactly the way an app does, so together they explain "why does this name resolve like that on this box".
90.1 Why is it used?
- resolvectl: resolve names, IPs and services, show the DNS servers of every interface, flush the cache, set up split DNS for VPN links.
- getent: query NSS databases (hosts, passwd, services...) in the nsswitch.conf order, i.e. the same answer an app gets (hosts file + DNS).
- Both are Linux only; resolvectl needs systemd-resolved.
- Lab: Debian 12 with systemd-resolved 254 (eth0 192.168.110.10, eth1 192.168.110.140), Alpine BIND 192.168.110.153 authoritative for shop.lab.
90.2 How a name travels: NSS and resolved
- Step 1
1.getent hosts nas.home: glibc reads the hosts line of nsswitch.conf
- Step 2
2.First source "files" = /etc/hosts: nas.home found, 192.168.110.50, no packet sent
- Step 3
3.example.com not in /etc/hosts: the NSS resolve module hands it to systemd-resolved (stub 127.0.0.53)
- Step 4
4.resolved asks the eth0 DNS server 1.1.1.1
- Step 5
5.Answer is cached in resolved and returned to the app
- Step 1
1.eth1 = VPN-like link, DNS 192.168.110.153, route-only domain ~shop.lab
- Step 2
2.App asks shop.lab -> resolved sends it only to the eth1 DNS
- Step 3
3.App asks example.com -> goes out via eth0 (domain ~., default route)
- Step 4
4.eth1 has DefaultRoute off, so it never sees other names
90.3 Per-link configuration (building split DNS)
resolvectl dns eth0 1.1.1.1 8.8.8.8resolvectl domain eth0 '~.'resolvectl dns eth1 192.168.110.153resolvectl domain eth1 '~shop.lab'resolvectl default-route eth1 noresolvectl llmnr eth1 noresolvectl mdns eth1 noresolvectl status eth0 eth1$ resolvectl status eth0 eth1Link 9 (eth0) Current Scopes: DNS LLMNR/IPv4 mDNS/IPv4 Protocols: +DefaultRoute +LLMNR +mDNS -DNSOverTLS DNSSEC=no/unsupported DNS Servers: 1.1.1.1 8.8.8.8 DNS Domain: ~. Link 10 (eth1) Current Scopes: DNS Protocols: -DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported DNS Servers: 192.168.110.153 DNS Domain: ~shop.lab- ~ prefix = route-only domain (routing, not a search suffix); ~. = default route for all names.
- Lab runs as root; on a normal machine these need sudo.
| Command | Meaning |
|---|---|
| dns LINK IP... / dns LINK '' | Set / clear the link's DNS servers |
| dns LINK IP:853#SNI | DNS-over-TLS server with port and TLS server name |
| domain LINK ~name / domain LINK name | Route-only domain / search domain |
| default-route LINK yes|no | Use this link for names no other link claims |
| llmnr / mdns LINK yes|no|resolve | LLMNR / mDNS on the link |
| dnssec / dnsovertls LINK MODE | yes / no / allow-downgrade, yes / no / opportunistic |
| nta LINK DOMAIN | Negative trust anchor: skip DNSSEC for a domain |
| revert LINK | Reset all DNS settings of the link |
90.4 Lookup commands
resolvectl query example.comresolvectl query shop.labresolvectl query www.shop.labresolvectl query 8.8.8.8$ resolvectl query example.comexample.com: 172.66.147.243 -- link: eth0 104.20.23.154 -- link: eth0 2606:4700:90d5:72db:f239:0:ef6b:ff98 -- link: eth0 -- Information acquired via protocol DNS in 43.5ms.-- Data from: network$ resolvectl query shop.labshop.lab: 192.168.110.180 -- link: eth1 -- Information acquired via protocol DNS in 17.3ms.-- Data from: network$ resolvectl query www.shop.labwww.shop.lab: 192.168.110.180 -- link: eth1 (shop.lab) -- Information acquired via protocol DNS in 5.8ms.-- Data from: cache$ resolvectl query 8.8.8.88.8.8.8: dns.google -- link: eth0 -- Information acquired via protocol DNS in 26.4ms.-- Data from: network- Every answer shows the link, protocol, time and whether it came from network or cache.
- shop.lab answered on eth1: the route-only domain works. A CNAME shows the real name in brackets. An IP does a reverse lookup.
resolvectl service _http._tcp.shop.labresolvectl service 'My Printer' \ _ipp._tcp shop.labresolvectl tlsa shop.labresolvectl openpgp alice@shop.lab$ resolvectl service _http._tcp.shop.lab_http._tcp.shop.lab: web.shop.lab:80 [priority=0, weight=0] 192.168.110.181 -- link: eth1 (_http._tcp/shop.lab)$ resolvectl service 'My Printer' _ipp._tcp shop.labMy Printer/_ipp._tcp/shop.lab: printer.shop.lab:631 [priority=0, weight=0] 192.168.110.190 -- link: eth1 rp=ipp/print ty=Lab Laser$ resolvectl tlsa shop.lab_443._tcp.shop.lab IN TLSA 3 1 1 8d02536c887482bc34ff54e41d2ba659bf85b341a0a20afadb5813dcfbcf286d -- Matching type: SHA-256 -- link: eth1WARNING: The resources shown contain cryptographic key data which could not be authenticated. It is not suitable to authenticate any communication.$ resolvectl openpgp alice@shop.lab2bd806c97f0e00af1a1fc3328fa763a9269723c8db8fac4f93af71db._openpgpkey.shop.lab IN OPENPGPKEY bGFiLWRlbW8ta2V5 -- link: eth1- service: SRV lookup plus the target's IP; with name + type + domain it is a full DNS-SD lookup including TXT metadata.
- tlsa (certificate hash for port 443) and openpgp warn because DNSSEC is off in the lab.
90.5 Status, cache and monitoring
resolvectl statisticsresolvectl show-cacheresolvectl flush-cachesresolvectl reset-statisticsresolvectl statistics$ resolvectl statisticsDNSSEC supported by current servers: no TransactionsCurrent Transactions: 0 Total Transactions: 23 Cache Current Cache Size: 16 Cache Hits: 2 Cache Misses: 17$ resolvectl show-cacheScope protocol=dns ifindex=10 ifname=eth1web.shop.lab IN A 192.168.110.181shop.lab IN A 192.168.110.180www.shop.lab IN CNAME shop.lab_http._tcp.shop.lab IN SRV 0 0 80 web.shop.lab Scope protocol=dns ifindex=9 ifname=eth0example.com IN AAAA 2606:4700:90d5:72db:f239:0:ef6b:ff988.8.8.8.in-addr.arpa IN PTR dns.googleexample.com IN A 104.20.23.154example.com IN A 172.66.147.243$ resolvectl flush-caches$ resolvectl reset-statistics$ resolvectl statisticsDNSSEC supported by current servers: no TransactionsCurrent Transactions: 0 Total Transactions: 0 Cache Current Cache Size: 0 Cache Hits: 0 Cache Misses: 0timeout 8 resolvectl monitor \ > /tmp/monitor.txt &getent hosts shop.labcat /tmp/monitor.txtresolvectl log-levelresolvectl reset-server-featuresresolvectl show-server-state$ getent hosts shop.lab192.168.110.180 shop.lab$ cat /tmp/monitor.txt→ Q: shop.lab IN AAAA← S: success← A: shop.lab IN SOA ns.shop.lab admin.shop.lab 2026110901 3600 600 86400 300 → Q: shop.lab IN A← S: success← A: shop.lab IN A 192.168.110.180$ resolvectl log-levelinfo$ resolvectl reset-server-features$ resolvectl show-server-stateUnknown command verb show-server-state.- monitor shows live which names apps ask for (here getent triggered AAAA and A queries).
- flush-caches after a wrong or stale answer; log-level debug for detailed logs; reset-server-features makes resolved re-probe servers.
- show-server-state exists only in newer systemd; 254 says "Unknown command verb".
90.6 resolvectl options
resolvectl -t MX query shop.labresolvectl -t TXT --legend=no \ query shop.labresolvectl -4 query example.comresolvectl -i eth1 query example.comresolvectl --cache=no query shop.labresolvectl --cname=no query www.shop.labresolvectl --network=no \ query mail.shop.labresolvectl --synthesize=no \ query localhostresolvectl query localhost$ resolvectl -t MX query shop.labshop.lab IN MX 10 mail.shop.lab -- link: eth1 -- Information acquired via protocol DNS in 2.6ms.-- Data from: network$ resolvectl -t TXT --legend=no query shop.labshop.lab IN TXT "v=spf1 mx -all" -- link: eth1$ resolvectl -4 query example.comexample.com: 172.66.147.243 -- link: eth0 104.20.23.154 -- link: eth0$ resolvectl -i eth1 query example.comexample.com: resolve call failed: No appropriate name servers or networks for name found$ resolvectl --cache=no query shop.labshop.lab: 192.168.110.180 -- link: eth1 -- Information acquired via protocol DNS in 2.6ms.-- Data from: network$ resolvectl --cname=no query www.shop.labwww.shop.lab: resolve call failed: CNAME loop detected, or CNAME resolving disabled on 'www.shop.lab'$ resolvectl --network=no query mail.shop.labmail.shop.lab: resolve call failed: All suitable resolution sources turned off$ resolvectl --synthesize=no query localhostlocalhost: resolve call failed: No appropriate name servers or networks for name found$ resolvectl query localhostlocalhost: 127.0.0.1 -- link: lo ::1 -- link: lo -- Information acquired via protocol DNS in 1.7ms.-- Data from: synthetic| Option | Meaning |
|---|---|
| -t TYPE / -c CLASS | Low-level record lookup (MX, TXT...), class |
| --legend=no | Only the answer lines (good for scripts) |
| -4 / -6, -i LINK | Address family, only this interface |
| -p dns|llmnr|mdns | Choose protocol (--protocol=help lists them) |
| --cache=no | Always go to the network (test the upstream server) |
| --cname=no, --network=no, --synthesize=no | No CNAME following, cache/local only, no synthetic answers (localhost) |
| --validate=no, --search=no, --stale-data=no, --zone=no, --trust-anchor=no | Per-query behaviour switches |
| --service-address=no, --service-txt=no | SRV without IP lookup / without TXT |
| --raw[=packet], --json=pretty / -j | Binary dump, JSON (in 254 json only changes show-cache) |
90.7 resolvconf compatibility mode
printf 'nameserver 9.9.9.9\n' \ | resolvconf -a eth1resolvectl dns eth1resolvconf -d eth1resolvectl dns eth1resolvconf -f -d eth9resolvconf -lresolvectl revert eth1resolvectl status eth1$ printf 'nameserver 9.9.9.9\n' | resolvconf -a eth1$ resolvectl dns eth1Link 10 (eth1): 9.9.9.9$ resolvconf -d eth1$ resolvectl dns eth1Link 10 (eth1):$ resolvconf -f -d eth9$ resolvconf -lresolvconf: option requires an argument -- 'l'$ resolvectl revert eth1$ resolvectl status eth1Link 10 (eth1) Current Scopes: LLMNR/IPv4 mDNS/IPv4 Protocols: -DefaultRoute +LLMNR +mDNS -DNSOverTLS DNSSEC=no/unsupported- On Debian resolvconf is a symlink to resolvectl, so old VPN scripts keep working.
- -a LINK reads nameserver lines from stdin, -d LINK removes them, -f ignores a missing interface, -x exclusive mode; -m and -p are silently ignored; unsupported options like -l fail.
90.8 getent: databases and options
getent hosts nas.homegetent hosts shop.labgetent ahostsv4 example.comgetent services 443getent protocols tcpgetent passwd rootgetent hosts nahi.shop.lab; echo exit $?$ getent hosts nas.home192.168.110.50 nas.home nas$ getent hosts shop.lab192.168.110.180 shop.lab$ getent ahostsv4 example.com104.20.23.154 STREAM example.com104.20.23.154 DGRAM 104.20.23.154 RAW 172.66.147.243 STREAM 172.66.147.243 DGRAM 172.66.147.243 RAW $ getent services 443https 443/tcp$ getent protocols tcptcp 6 TCP$ getent passwd rootroot:x:0:0:root:/root:/bin/bash$ getent hosts nahi.shop.lab; echo exit $?exit 2| Database | Use |
|---|---|
| hosts / ahosts / ahostsv4 / ahostsv6 | Name lookup like gethostbyname / getaddrinfo (with socket types) |
| services, protocols, rpc, networks | Port names, protocol numbers, RPC programs, networks |
| passwd, group, initgroups | Users, groups, all groups of a user |
| shadow, gshadow | Password entries (root only) |
| aliases, ethers, netgroup | Mail aliases, MAC-to-host, netgroups (can come from LDAP/NIS) |
| Exit codes | 0 ok, 1 bad arguments / unknown db, 2 key not found, 3 listing not supported |
getent -s files hosts nas.homegetent -s files hosts shop.lab; echo exit $?getent -s hosts:dns hosts shop.labgetent nosuchdb x; echo exit $?getent --usage$ getent -s files hosts nas.home192.168.110.50 nas.home nas$ getent -s files hosts shop.lab; echo exit $?exit 2$ getent -s hosts:dns hosts shop.lab192.168.110.180 shop.lab$ getent nosuchdb x; echo exit $?Try `getent --help' or `getent --usage' for more information.Unknown database: nosuchdbexit 1$ getent --usageUsage: getent [-Ai?V] [-s CONFIG] [--no-addrconfig] [--no-idn] [--service=CONFIG] [--help] [--usage] [--version] database [key ...]- -s files = only /etc/hosts; -s hosts:dns overrides the source for the hosts database only.
- -i / --no-idn turns off IDN encoding; -A / --no-addrconfig, --help, --usage, --version.
90.9 Common mistakes
- Seeing nameserver 127.0.0.53 and thinking DNS is broken: it is the resolved stub. resolvectl dns shows the real upstream servers (here 1.1.1.1 8.8.8.8 on eth0, 192.168.110.153 on eth1).
- Editing /etc/resolv.conf to change DNS on a resolved system: use resolvectl or the network config.
- Debugging with dig only: dig bypasses NSS and ignores /etc/hosts; use getent for what apps see.
- Forgetting default-route no on a VPN link: all names may leak to the VPN DNS.
$ cat /etc/resolv.confnameserver 127.0.0.53options edns0 trust-adsearch .$ resolvectl dnsGlobal:Link 9 (eth0): 1.1.1.1 8.8.8.8Link 10 (eth1): 192.168.110.15390.10 Interview tips
- dig vs getent vs resolvectl query: dig asks a DNS server directly (no hosts file); getent goes through NSS like an app; resolvectl query asks resolved and reports link, protocol and cache vs network.
- VPN on, internal names fail, google works: run resolvectl status first and check the VPN link's DNS server and ~domain.
- Split DNS = per-link DNS servers + route-only ~domains + default-route.
- getent exit 2 = not found, handy in scripts.