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
| Database | What's in it | Key | Line format |
|---|---|---|---|
passwd | users | name or UID | name:x:UID:GID:comment:home_dir:shell |
group | groups | name or GID | name:x:GID:member1,member2 |
shadow | password hashes (root only) | name | name:hash:last_change:min:max:warn::: |
gshadow | group passwords (root only) | name | name:hash:admins:members |
initgroups | all groups of a user | name | name GID1 GID2 … |
hosts | name ↔ address (gethostbyname) | name or IP | address name aliases |
ahosts | addresses via getaddrinfo | name | address STREAM|DGRAM|RAW name |
ahostsv4 / ahostsv6 | same, IPv4 / IPv6 only | name | like ahosts |
services | service ports (/etc/services) | name, port or port/protocol | name port/protocol aliases |
protocols | IP protocol numbers | name or number | tcp 6 TCP |
networks | network names | name | link-local 169.254.0.0 |
ethers, rpc, aliases, netgroup | less commonly needed databases |
getent --help prints the list of databases your system supports at the end.
Options
| Option | What it does |
|---|---|
-s source | query only the given NSS source, e.g. -s files — local files only |
-A | for ahosts*: don't drop addresses of a family not present on the interfaces (by default ::1 may disappear) |
-i | don't convert IDN names (internationalized domains) |
Exit codes
| Code | Meaning |
|---|---|
0 | everything found |
1 | unknown database or missing arguments |
2 | at least one key not found |
3 | the 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/passwddoesn't see everyone. Users from LDAP/SSSD or systemd aren't in the file, butgetent passwdshows them. To check whether a user exists, usegetent, notgrep.getent hostsalso asks DNS, whilegrep /etc/hostsonly reads the file. If a name doesn't resolve,getent hosts namereproduces exactly what a program will see.getent hosts localhostmay return::1instead of127.0.0.1: this database looks for an IPv6 address first. Need IPv4 — usegetent ahostsv4.ahostswithout-Ahides addresses the system considers unsuitable (e.g.::1when the interfaces have no IPv6).- The member field in
groupdoesn't include users for whom this is the primary group (GID inpasswd). The full list of one user's groups comes fromgetent initgroupsorid. getent shadowandgshadowwork only as root. For a regular user the output is empty.- Code
2, not1, when a key isn't found. In conditions test for "non-zero", not specifically1.