Skip to main content
RunBook Academy

LinuxIV · Users, Groups and IdentityIdentity

Name Service Switch (NSS) and getent

Foundation⏱ ~8 minbashgetentcatnscd

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

Not yet marked complete on this device.

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.

Read-only / Safegetent vs cat
$ getent passwd alice; cat /etc/passwd | grep alice
alice:x:1000:1000:Alice Smith:/home/alice:/bin/bash
alice:x:1000:1000:Alice Smith:/home/alice:/bin/bash

Illustrative output

/etc/nsswitch.conf

The configuration file is /etc/nsswitch.conf. It maps each database to a list of sources:

Read-only / Safensswitch.conf
$ cat /etc/nsswitch.conf
passwd:         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:

SourcePurpose
filesLocal files (/etc/passwd, /etc/hosts, /etc/services, …)
dnsDNS resolution for hosts
systemdDynamic users synthesised by systemd
sssSSSD — System Security Services Daemon (LDAP, FreeIPA, Active Directory)
ldapDirect LDAP lookup
nis / nisplusNetwork Information Service (legacy)
winbindSamba’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).

Read-only / Safenscd status
$ systemctl status nscd 2>/dev/null | head -3; getent passwd | wc -l; cat /etc/passwd | wc -l
nscd.service - Name Service Cache Daemon
Active: inactive (dead)
5 /etc/passwd lines
5 /etc/passwd lines

Illustrative output

Diagnosing NSS failures

Read-only / Safestrace getent
$ strace -e openat getent passwd alice 2>&1 | grep -E passwd-nss
openat(AT_FDCWD, /etc/nsswitch.conf, O_RDONLY) = 3
openat(AT_FDCWD, /etc/passwd, O_RDONLY) = 4
...

Illustrative output

Knowledge check

Knowledge check · 3 questions

  1. Q1. Why should scripts use `getent` instead of reading /etc/passwd directly?

  2. Q2. The first source on an nsswitch.conf line that returns a match ends the lookup, so the order decides which answer wins.

  3. Q3. Which of the following are common NSS sources? Select all that apply.

Passing score: 75%. Answers are checked in this browser.