Linux getent Command: Query Users, Groups, and Other NSS Databases

getent queries entries in databases for accounts, groups, hosts, etc., which are used by the Name Service Switch (NSS). It is useful when you want to check not only local files but also external authentication or name services configured on the system using the same query path.

What is the getent command?

It follows the lookup order specified in /etc/nsswitch.conf, so the results may differ from simply reading the /etc/passwd file. Specify the database name first, followed by the necessary key. If the key is omitted, it will attempt enumeration, but full enumeration is not possible in all databases.

Basic syntax

getent database [key...]

Installed implementations and options may differ depending on the distribution. Check the description for your current system using man getent.

Examples

Querying user accounts

Search for users in local files or NSS-configured sources.

getent passwd alice

Querying group information

Check group names and members.

getent group editors

Check Hostname

Query the name through the NSS settings of the current system.

getent hosts localhost

Reading NSS Database Query Results

getent passwd username can look up not only the local /etc/passwd but also accounts provided through the system's NSS (Name Service Switch) configuration.

getent passwd root

Example output (home directory and shell vary by system):

root:x:0:0:root:/root:/bin/bash

In the colon-separated fields, the third is the UID, and the fourth is the primary GID. x does not display the actual password. If there is no result, the account may not exist or the NSS backend may be unavailable. A getent query is not the same as looking only in /etc/passwd.

Main Options and Format

Options/Format Description
passwd Query the user account database.
group Query the group database.
hosts Query the host name and address database.
-s service Specify the lookup service of a particular database in the glibc implementation.

Precautions when using

Even if an account is not in /etc/passwd, it may exist in external NSS sources such as LDAP. Conversely, a getent failure can indicate either a service issue or a missing key, so check both the exit status and NSS settings together. Enumerating all keys may not be supported in some databases or may produce a large output.

Frequently Asked Questions

What is the difference between `getent passwd alice` and `grep alice /etc/passwd`?

`getent` uses the configured NSS lookup path, while `grep` only searches the text in the local file. If there is an external user directory, the results may differ.

Official Documentation

The exact behavior of options and differences by implementation can be found in the official getent documentation.

More in This Category
Linux df Command: Check Filesystem Free Space

Linux df Command: Check Filesystem Free Space

Learn how to check Filesystem Free Space with the Linux df command, including practical examples, key options, and important precautions.

Linux unzip Command: List and Extract ZIP Archives

Linux unzip Command: List and Extract ZIP Archives

Learn how to list and Extract ZIP Archives with the Linux unzip command, including practical examples, key options, and important precautions.

Linux locate Command: Search Indexed File Paths Quickly

Linux locate Command: Search Indexed File Paths Quickly

Learn how Linux locate searches a filename database, how updatedb affects freshness, how to limit and filter matches, and when find is the better tool.

Linux Tutorial / Install and Update Packages with APT and DNF

Linux Tutorial / Install and Update Packages with APT and DNF

Compare APT on Debian or Ubuntu with DNF on Rocky Linux for searching, installing, updating, and removing packages, including repository and transaction checks.

Linux ps Command: Inspect Running Processes

Linux ps Command: Inspect Running Processes

Learn how to inspect Running Processes with the Linux ps command, including practical examples, key options, and important precautions.

Linux systemctl Command: Manage systemd Services

Linux systemctl Command: Manage systemd Services

Learn how to manage systemd Services with the Linux systemctl command, including practical examples, key options, and important precautions.

Linux Tutorial / Understand Users, Groups, and File Ownership

Linux Tutorial / Understand Users, Groups, and File Ownership

Understand UIDs, GIDs, primary and supplementary groups, and file ownership, then inspect them safely with id, groups, getent, ls, and stat.

Linux alias Command: Create Command Shortcuts

Linux alias Command: Create Command Shortcuts

Learn how to use the Linux alias command to create, inspect, and make shell command aliases persistent, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux wc Command: Count Lines, Words, Characters, and Bytes

Linux wc Command: Count Lines, Words, Characters, and Bytes

Learn how to use the Linux wc command to count lines, words, characters, and bytes in files or standard input, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux mktemp Command: Create Secure Temporary Files and Directories

Linux mktemp Command: Create Secure Temporary Files and Directories

Learn how to create unpredictable temporary files and directories with Linux mktemp, use templates and TMPDIR, clean up with trap, and avoid unsafe dry runs.