LinuxIV · Users, Groups and IdentityIdentity
Name Service Switch (NSS) and getent
What you'll learn
- Read /etc/nsswitch.conf and understand its sources
- Use getent instead of reading /etc/passwd directly
- Recognise when central identity (SSSD, LDAP) is in play
- Diagnose NSS misconfiguration that breaks login or tooling
Prerequisites
Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09
When you run id alice or getent passwd alice, the kernel does not
read /etc/passwd directly. It asks glibc’s NSS layer, which
consults /etc/nsswitch.conf to decide which backing stores to
query — and in what order.
Why this matters
On a host with only local accounts, NSS reads /etc/passwd. On a host joined to LDAP via SSSD, NSS reads both — and the LDAP results may include users that do not exist in /etc/passwd at all. A sysadmin who reads /etc/passwd to audit accounts on a centrally managed host sees only a fraction of the truth.
$ getent passwd alice; cat /etc/passwd | grep alicealice:x:1000:1000:Alice Smith:/home/alice:/bin/bash
alice:x:1000:1000:Alice Smith:/home/alice:/bin/bashIllustrative output
/etc/nsswitch.conf
The configuration file is /etc/nsswitch.conf. It maps each
database to a list of sources:
$ cat /etc/nsswitch.confpasswd: files systemd
group: files systemd
shadow: files
hosts: files dns
networks: files
services: files
protocols: files
rpc: files
ethers: files
netgroup: nis
automount: files
Illustrative output
The common sources:
| Source | Purpose |
|---|---|
files | Local files (/etc/passwd, /etc/hosts, /etc/services, …) |
dns | DNS resolution for hosts |
systemd | Dynamic users synthesised by systemd |
sss | SSSD — System Security Services Daemon (LDAP, FreeIPA, Active Directory) |
ldap | Direct LDAP lookup |
nis / nisplus | Network Information Service (legacy) |
winbind | Samba’s winbind for NT-style domains |
The order matters
passwd: files sss means: look in /etc/passwd first; if not found,
ask SSSD; if SSSD does not know, fail. passwd: sss files would
invert the priority and mean SSSD answers first.
getent — the right way to read identity
getent queries the NSS layer for a database:
getent passwd # all users from all sources
getent passwd alice # a specific user
getent group sudo # a specific group
getent hosts example.com # hosts resolution
getent services ssh # service-to-port mapping
In scripts, always use getent instead of reading /etc/passwd
or /etc/hosts directly. Tools that read these files directly
break on centrally-managed hosts.
nscd and caching
NSS queries go through glibc on every call. To reduce latency, the nscd daemon caches results. On most modern distributions, nscd is deprecated in favour of SSSD’s own cache (which is nscd-compatible and behaves similarly).
$ systemctl status nscd 2>/dev/null | head -3; getent passwd | wc -l; cat /etc/passwd | wc -lnscd.service - Name Service Cache Daemon
Active: inactive (dead)
5 /etc/passwd lines
5 /etc/passwd linesIllustrative output
Diagnosing NSS failures
$ strace -e openat getent passwd alice 2>&1 | grep -E passwd-nssopenat(AT_FDCWD, /etc/nsswitch.conf, O_RDONLY) = 3
openat(AT_FDCWD, /etc/passwd, O_RDONLY) = 4
...Illustrative output
Knowledge check
Knowledge check · 3 questions
Q1. Why should scripts use `getent` instead of reading /etc/passwd directly?
Q2. The first source on an nsswitch.conf line that returns a match ends the lookup, so the order decides which answer wins.
Q3. Which of the following are common NSS sources? Select all that apply.
Passing score: 75%. Answers are checked in this browser.