getent command

Retrieves entries from administrative databases.

Overview

getent is a Unix command that displays entries from administrative databases (like /etc/passwd, /etc/group, etc.) using the same library functions that applications would use. It's useful for querying system databases in a standardized way, regardless of whether the information comes from local files, NIS, LDAP, or other sources.

Options

database

Specifies which database to query (passwd, group, hosts, services, protocols, networks, etc.)

$ getent passwd root
root:x:0:0:root:/root:/bin/bash

-s, --service=CONFIG

Specify service configuration to be used

$ getent -s files passwd root
root:x:0:0:root:/root:/bin/bash

-h, --help

Display help information

$ getent --help
Usage: getent [OPTION...] database [key ...]
Get entries from administrative database.

  -i, --no-idn               disable IDN encoding
  -s, --service=CONFIG       Service configuration to be used
  -?, --help                 Give this help list
      --usage                Give a short usage message
  -V, --version              Print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

Supported databases:
ahosts ahostsv4 ahostsv6 aliases ethers group gshadow hosts netgroup networks
passwd protocols rpc services shadow

-V, --version

Display version information

$ getent --version
getent (GNU libc) 2.31

-i, --no-idn

Disable IDN (Internationalized Domain Names) encoding

$ getent -i hosts example.com
93.184.216.34   example.com

Usage Examples

Looking up a user by username

$ getent passwd username
username:x:1000:1000:Full Name:/home/username:/bin/bash

Finding a host by name

$ getent hosts google.com
142.250.190.78  google.com

Listing all groups

$ getent group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
[additional groups...]

Looking up a service port

$ getent services ssh
ssh                  22/tcp

Tips:

Use with grep for filtering

Combine getent with grep to filter results from large databases:

$ getent passwd | grep username

Check if a user exists

Use the exit code to check if a user exists in the system:

$ getent passwd username > /dev/null && echo "User exists" || echo "User does not exist"

Find all members of a group

Use the group database to see all members of a specific group:

$ getent group sudo
sudo:x:27:user1,user2,user3

Frequently Asked Questions

Q1. What databases can I query with getent?

A. Common databases include passwd, group, hosts, services, protocols, networks, shadow, and aliases. The available databases may vary by system.

Q2. How do I check if a hostname resolves?

A. Use getent hosts hostname. If the hostname resolves, it will return the IP address and hostname.

Q3. Can getent query LDAP or other directory services?

A. Yes, getent uses the Name Service Switch (NSS) configuration, so it can query any source configured in your system's /etc/nsswitch.conf file, including LDAP, NIS, DNS, and local files.

Q4. How is getent different from just reading files like /etc/passwd directly?

A. getent respects the system's NSS configuration, so it retrieves information from all configured sources (local files, LDAP, NIS, etc.), not just the local files.

References

https://man7.org/linux/man-pages/man1/getent.1.html

Revisions