getent — What the System Actually Believes About a Name, a User or a Port

ByLinuxtastic

November 29, 2026

A user “does not exist” on one machine and does on the next with the same LDAP server behind both. A hostname resolves in dig and not in the application, or the other way round. A script needs to know whether a group is there before it adds a member. Each of those is a question about what the system believes, and the system’s belief is not the contents of one file: it is the answer that comes back when a program asks glibc, which consults /etc/nsswitch.conf, which names the sources in order — files, DNS, systemd-resolved, LDAP, SSSD, whatever is installed. getent asks the same way a program does and prints what it was told. That is the whole command, and it is why it is the right first move for a surprising number of “it works here and not there” problems.

getent passwd deploy
getent group docker
getent hosts app.internal

Each takes a database and then one or more keys. The database names are the ones from nsswitch.conf, and the answer comes back in the format of the traditional file for that database, whatever source it actually came from: a passwd line for a user that lives in LDAP, a hosts line for a name that came from DNS. With no key, getent enumerates the whole database, if the source allows it — and that qualification matters, as the section on users explains.

$ grep -v '^#' /etc/nsswitch.conf | grep .
passwd:         files
group:          files
shadow:         files
hosts:          files dns
services:       db files
$ getent passwd root
root:x:0:0:root:/root:/bin/bash
$ getent hosts github.com
140.82.112.3    github.com
$ getent services 443
https                 443/tcp

The difference from cat /etc/passwd, dig or grep /etc/services is the path taken. cat reads a file; dig talks to a DNS server directly, ignoring /etc/hosts and every other source; getent walks the configured sources in the configured order, which is the walk ssh, curl, sudo and your application make. When getent and the application disagree, the problem is in the application; when getent and dig disagree, the problem is in nsswitch.conf or a source that comes before DNS.

hosts and ahosts do not agree, and ahosts is the one that matters

getent hosts answers through the old gethostbyname interface: it tries IPv6 first and returns the first address family that has anything, so a dual-stack name comes back as its IPv6 addresses only. getent ahosts answers through getaddrinfo, which is what every program written this century calls, and it applies the same rules the program would — including dropping address families this machine cannot use. Measured on a host with no IPv6 address at all, with one name given both an A and an AAAA record in /etc/hosts:

$ getent hosts app.internal
fd00::5         app.internal
$ getent ahosts app.internal
10.0.0.5        STREAM app.internal
10.0.0.5        DGRAM
10.0.0.5        RAW
$ ip -br -6 addr
$

The same name, the same sources, two different answers, and only the second one is an address this machine could connect to. When the question is “what will the application get”, ask ahosts (or ahostsv4 / ahostsv6 to force a family); hosts is for reading what a source holds. The three lines per address are the socket types getaddrinfo offers; you can ignore them. The full story of what getaddrinfo does between your call and the answer is DNS, All the Way Down.

The databases

DatabaseAnswersKey
passwdAccount line: name, uid, gid, GECOS, home, shell — from files, LDAP, SSSD, whatever is configuredname or uid
groupGroup line with its member listname or gid
shadow / gshadowPassword-ageing fields; readable by root only (others get exit 2, as if the user did not exist)name
initgroupsThe supplementary group ids a user would be given at loginname
hostsOne address family per name, IPv6 preferred (see the box)name or address
ahosts / ahostsv4 / ahostsv6What getaddrinfo returns: every usable address, the modern answername or address
servicesPort-to-name mapping from /etc/services: what ss and netstat print when you forget -nname or number, optionally /tcp or /udp
protocolsIP protocol numbers: tcp is 6, udp 17, icmp 1name or number
networksNamed networks from /etc/networks; rarely populatedname
netgroupNIS/LDAP netgroups, used in sshd and sudoers rules on some sitesnetgroup name
aliases, ethers, rpcMail aliases, MAC-to-name mappings, RPC program numbers — exist, seldom neededvaries

Two options are worth knowing. -s SERVICE restricts the lookup to one source — getent -s files hosts NAME reads only /etc/hosts, getent -s dns hosts NAME only DNS — which is how you find out which source is producing an unexpected answer without editing nsswitch.conf. -i turns off IDN conversion for hostnames with non-ASCII characters. There is no option to pick a resolver or a server; that is dig @server‘s job, and the two tools are complementary rather than interchangeable.

Users and groups: the one list that is not a list

getent passwd alice                 # does the system know her, and from where
getent passwd 1000                  # who owns uid 1000 (works on a bare uid from ls -n)
getent group docker                 # who is in the group, as the system sees it
getent initgroups alice             # the group ids she would get at login
getent group | awk -F: '$3 >= 1000' # local-style listing of non-system groups

On a machine joined to a directory, getent passwd with no key usually lists only the local accounts, while getent passwd alice finds her instantly. That is not a fault. Enumerating a directory of fifty thousand users is expensive, so SSSD ships with enumeration off and most LDAP configurations do the same; lookups by key work, listings do not include the directory. People hit this when a script does getent passwd | grep alice and concludes she does not exist. Query by key, and test the exit status, which is 2 for “not found” and 0 for found:

if ! getent passwd "$user" >/dev/null; then
    useradd -m "$user"
fi
getent group "$grp" >/dev/null || groupadd "$grp"

The group database shows the members listed on the group; it does not show users whose primary gid is that group, because that fact lives on the user. id alice and getent initgroups alice answer from the user’s side and include both. A related trap is that the passwd entry says what a login would get — shell, home, uid — not what any running process has; a user whose shell was changed to nologin an hour ago still has their open sessions. The Life of a Login walks where each of these is consulted and when.

Names: getent against dig

getent ahosts db.internal           # what the application will get, all sources
getent -s files hosts db.internal   # only /etc/hosts
getent -s dns hosts db.internal     # only DNS, through the stub resolver
getent hosts 10.0.0.5               # reverse: files first, then PTR
getent hosts                        # enumerate: prints /etc/hosts and stops (DNS cannot enumerate)

The hosts: line in nsswitch.conf is where the surprises live. A desktop or a current server often reads hosts: files myhostname resolve [!UNAVAIL=return] dns or similar: files is /etc/hosts; myhostname answers for the machine’s own name and localhost even when /etc/hosts is missing an entry; resolve hands the query to systemd-resolved, which has its own per-interface servers, search domains and cache, and the bracketed action stops the walk there if resolved is running; dns is the classic stub resolver reading /etc/resolv.conf. mdns4_minimal may sit in there too for .local names. dig uses none of this. It reads /etc/resolv.conf for a server address and asks it directly, so dig succeeding while the application fails means a source earlier in the line is answering — a stale /etc/hosts entry, a resolved cache, a split-horizon rule — and getent -s against each source in turn finds which. How a Name Is Actually Resolved is the walk through that line; Networking Basics puts the getent step into the outward diagnosis.

Reverse lookups go through the same switch: getent hosts 10.0.0.5 returns an /etc/hosts name if there is one and a PTR record otherwise. That is why a machine can show a different name for an address than dig -x does, and why sshd with reverse-lookup checks enabled can be slow on a host whose hosts: line waits on a source that is down.

Services and protocols

$ getent services ssh
ssh                   22/tcp
$ getent services 53/udp
domain                53/udp
$ getent services 22/udp
$ echo $?
2
$ getent protocols 6
tcp                   6 TCP

These are the lookups behind every tool that prints a name instead of a port — ss without -n, netstat, lsof -i without -P, tcpdump without -n — and behind the tools that accept a name where a port is expected, nc host https among them. The file is /etc/services; the entries are IANA’s assignments plus whatever the distribution added, and a number with no entry simply prints nothing with exit status 2, as 22/udp above shows. When ss output calls a port something unexpected — 8080 as http-alt, 3000 as ppp — this is where the name came from, and it says nothing about what is actually listening. Use -n on a server and never read anything into the names.

In scripts

The exit status is the useful part. 0 means every key was found; 1 means the database name was wrong or getent was given nothing to do; 2 means at least one key was not found; 3 means the database cannot be enumerated and you asked it to be. With several keys on one line, getent prints the ones it finds and still exits 2 if any was missing, so test one key per call when the answer matters. Output fields are colon-separated for passwd and group and whitespace-separated for the rest, which cut and awk handle without fuss:

home=$(getent passwd "$user" | cut -d: -f6)
shell=$(getent passwd "$user" | cut -d: -f7)
members=$(getent group docker | cut -d: -f4)
addr=$(getent ahostsv4 "$host" | awk 'NR==1{print $1}')
uid=$(getent passwd "$user" | cut -d: -f3)

Prefer these to parsing /etc/passwd directly: the file is correct only on a machine with no directory, no SSSD and no containers that mount their own copy, and a script that reads it will quietly break on the first machine that is not like that. The one place getent is the wrong tool is in a container image’s build step on a system with no name service at all; there, getent still works, because files is always available, but the answer is about the image, not the host.

Common problems

SymptomCauseCheck / fix
getent passwd does not list a user that getent passwd name findsDirectory enumeration is disabled (SSSD default)Query by key; test $?, not a listing
getent hosts gives an IPv6 address the machine cannot reachhosts prefers IPv6 and returns one familygetent ahosts / ahostsv4
dig resolves a name, the application does notA source before dns in nsswitch.conf is answering, or resolved has a different viewgetent -s files, then -s resolve, then -s dns; resolvectl query
Application resolves a name dig cannot/etc/hosts, myhostname or mDNS answeredgetent -s files hosts name
getent shadow returns nothing, exit 2, for a real userNot root; shadow is unreadable, reported as “not found”Run as root
Exit status 2 although output appearedSeveral keys given and one was missingOne key per call
ss shows an odd service name on a port/etc/services has an entry for that numberss -n; getent services NNN to see where the name came from
Lookup hangs for seconds then succeedsA source early in the line is timing out (LDAP, mDNS, a dead DNS server)time getent -s source hosts name per source; resolv.conf timeouts
Unknown database, exit 1Misspelt database, or one glibc does not provide (sudoers, automount are not getent databases)getent --help lists the supported ones

Quick reference

getent passwd alice            # account line, from whichever source has her
getent passwd 1000             # name for a uid
getent group docker            # group line with members
getent initgroups alice        # her supplementary gids at login
getent ahosts name             # addresses the application will get (getaddrinfo)
getent ahostsv4 name           # IPv4 only
getent hosts name              # one family, IPv6 first — reading, not predicting
getent hosts 10.0.0.5          # reverse, files then PTR
getent -s files hosts name     # one source only: files, dns, resolve, myhostname, sss
getent services 443            # port name; services ssh for the number
getent protocols 17            # protocol number to name
getent passwd "$u" >/dev/null  # exit 0 found, 2 not found, 1 bad database

Related reading

  • DNS, All the Way Down — what getaddrinfo does with the hosts: line, and why ahosts is the honest answer
  • How a Name Is Actually Resolved — /etc/hosts, systemd-resolved, the stub resolver and the order they are tried
  • Users, Groups and Root — what the passwd and group fields mean once they are read
  • The Life of a Login — the stage at which the account is looked up, and what happens when a source is slow
  • User Management — useradd, usermod and chage, the commands that change what getent reports
  • ss — the port names in its output come from the services database; -n turns them off