getent¶
The getent command is used to display entries from databases that are supported
by the Name Switch Service (NSS) libraries.
-
NSS is the system that lets Linux resolve names and info from different sources (local files, LDAP, AD, NIS, DNS, etc).
-
NSS behavior is configured in
/etc/nsswitch.conf.
Overview¶
The getent command can be used to retrieve a lot of information, but it is most notably
used for gathering information about users and groups.
getent is NSS-aware, unlike cat /etc/passwd or grep, which only queries
local files.
This is why getent apsswd USER works even if the user is in LDAP, NIS, SSSD, AD,
etc.
If getent returns nothing for a query, it usually means that either the entry does
not exist, or NSS is misconfigured in /etc/nsswitch.conf.
Supported Databases¶
The most common databases you can query with getent:
passwd: List of user accounts like in/etc/passwdgroup: List of groups like in/etc/groupshadow: User passwords and aging info (expirations).- This one requires root.
hosts: Hostnames and IP addresses like in/etc/hosts/ DNSservices: Network services like in/etc/servicesprotocols: Network protocols like in/etc/protocolsnetworks: Network names and addressesaliases: Mail aliases (not really used anymore)
The list depends on your system's libc and NSS modules.
Use Cases¶
-
User lookup
This will show thepasswdentry for the userkolkhis.- This works even if the account is from LDAP or AD, hence it's sometimes
necessary instead of looking in
/etc/passwd.
- This works even if the account is from LDAP or AD, hence it's sometimes
necessary instead of looking in
-
Group lookups
This displays all the members of thesudogroup.- Again, this works even with LDAP and AD groups.
-
Host lookups
This uses the order specified in/etc/nsswitch.conffor host lookup.- It may consult
/etc/hosts, DNS, LDAP, etc. depending on yournsswitch.conf.
- It may consult
-
Service lookups
This will print the port and protocol forssh(e.g.,22/tcp)
Examples¶
-
Show the full user database (local and remote):
-
Print only user names (first field) from NSS:
-
Check which groups a user belongs to:
-
Resolve an IP using NSS rules:
tl;dr¶
getent: "get entries" from NSS databases.-
More reliable than grepping
/etc/passwdor/etc/groupbeacuse it works with remote identity sources. -
Databases include:
passwd,group,shadow,hosts,services, and more. - Sources (databases) are configured by
/etc/nsswitch.conf
getent Cheatsheet¶
| Database | Example Command | What It Shows |
|---|---|---|
passwd |
getent passwd kolkhis |
User account info: username:x:UID:GID:comment:home:shell |
group |
getent group sudo |
Group info: groupname:x:GID:members |
shadow |
sudo getent shadow kolkhis |
Secure password/aging info (root only) |
hosts |
getent hosts example.com |
IP <-> hostname mappings (follows /etc/nsswitch.conf) |
services |
getent services ssh |
Network service to port/protocol mapping (ssh 22/tcp) |
protocols |
getent protocols tcp |
Protocol numbers (tcp 6) |
networks |
getent networks loopback |
Network names and addresses (loopback 127) |
ethers |
getent ethers 00:11:22:33:44:55 |
Ethernet MAC address database (rare) |
aliases |
getent aliases postmaster |
Mail alias database (rare, old-school Unix mail) |
netgroup |
getent netgroup mygroup |
Netgroups (used in NIS/LDAP environments) |