← Full reference

getent

User, group, host and port entries as the system sees them through NSS

getent (get entries) reads the "administrative databases" — users, groups, hosts, services — through the same NSS mechanism every program on the system uses. So it shows the truth: not only lines from /etc/passwd and /etc/hosts, but also entries from DNS, LDAP, systemd and other sources, if they are configured.

Syntax

getent [options] database [key…]

Without a key it prints the whole database (if it can be enumerated). With keys — only the entries found, one line per key.

Databases

DatabaseWhat's in itKeyLine format
passwdusersname or UIDname:x:UID:GID:comment:home_dir:shell
groupgroupsname or GIDname:x:GID:member1,member2
shadowpassword hashes (root only)namename:hash:last_change:min:max:warn:::
gshadowgroup passwords (root only)namename:hash:admins:members
initgroupsall groups of a usernamename GID1 GID2 …
hostsname ↔ address (gethostbyname)name or IPaddress name aliases
ahostsaddresses via getaddrinfonameaddress STREAM|DGRAM|RAW name
ahostsv4 / ahostsv6same, IPv4 / IPv6 onlynamelike ahosts
servicesservice ports (/etc/services)name, port or port/protocolname port/protocol aliases
protocolsIP protocol numbersname or numbertcp 6 TCP
networksnetwork namesnamelink-local 169.254.0.0
ethers, rpc, aliases, netgroupless commonly needed databases

getent --help prints the list of databases your system supports at the end.

Options

OptionWhat it does
-s sourcequery only the given NSS source, e.g. -s files — local files only
-Afor ahosts*: don't drop addresses of a family not present on the interfaces (by default ::1 may disappear)
-idon't convert IDN names (internationalized domains)

Exit codes

CodeMeaning
0everything found
1unknown database or missing arguments
2at least one key not found
3the database can't be enumerated (query without a key)

Where the data comes from: NSS

For each database, /etc/nsswitch.conf sets the order of sources:

passwd:   files systemd
hosts:    files dns
services: db files

files means /etc/passwd, /etc/hosts and so on; dns is the DNS resolver; systemd, ldap, sss are external sources. getent walks this list exactly like ssh, sudo, ls -l and any other process.

Examples

# does the user exist — handy in scripts: code 0 or 2
getent passwd deploy >/dev/null && echo yes || echo no

# a user's home directory and shell
getent passwd root | cut -d: -f6,7

# who has UID 1000
getent passwd 1000 | cut -d: -f1

# members of the sudo group (only "supplementary" ones, see pitfalls)
getent group sudo | cut -d: -f4

# all of a user's groups, including the primary one — by GID
getent initgroups ubuntu

# several keys at once
getent passwd root nobody www-data

# what a name resolves to — taking /etc/hosts AND DNS into account
getent hosts localhost
getent ahostsv4 localhost

# only from /etc/hosts, no DNS
getent -s files hosts myapp.local

# which port a service uses and which service is on a port
getent services ssh
getent services 443
getent services 53/udp

# list all logins
getent passwd | cut -d: -f1

Pitfalls

  • grep /etc/passwd doesn't see everyone. Users from LDAP/SSSD or systemd aren't in the file, but getent passwd shows them. To check whether a user exists, use getent, not grep.
  • getent hosts also asks DNS, while grep /etc/hosts only reads the file. If a name doesn't resolve, getent hosts name reproduces exactly what a program will see.
  • getent hosts localhost may return ::1 instead of 127.0.0.1: this database looks for an IPv6 address first. Need IPv4 — use getent ahostsv4.
  • ahosts without -A hides addresses the system considers unsuitable (e.g. ::1 when the interfaces have no IPv6).
  • The member field in group doesn't include users for whom this is the primary group (GID in passwd). The full list of one user's groups comes from getent initgroups or id.
  • getent shadow and gshadow work only as root. For a regular user the output is empty.
  • Code 2, not 1, when a key isn't found. In conditions test for "non-zero", not specifically 1.

See also

useradd · id · dig · ping · ss · cut