Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

getent

Look up entries in the system databases (users, groups, hosts, services …) the same way programs do.

Level
RHCSA exam
Group
Users and groups
Needs root
No
Package
glibc-common

What it does

getent asks the name service switch (/etc/nsswitch.conf). So it finds users from /etc/passwd and from LDAP, IdM or Active Directory (through SSSD), and host names from /etc/hosts and DNS — exactly as the system sees them. cat /etc/passwd only shows local users.

Common databases: passwd, group, shadow, gshadow, hosts, ahosts, ahostsv4, ahostsv6, services, protocols, networks, netgroup, initgroups, aliases, ethers, rpc.

How to write it

getent [OPTION]... DATABASE [KEY]...

Main parts

passwd
User accounts.
group
Groups and members.
shadow
Password data (root only).
hosts
Host names (files and DNS, by nsswitch order).
ahosts
Host names with getaddrinfo (all addresses, IPv4 and IPv6).
services
Port numbers of services (/etc/services).
initgroups
All groups of a user, also from LDAP/AD.
netgroup
NIS netgroups.

Options 6

Most used

-s, --service SERVICE
Use this source only (like files or sss).
-s, --service DATABASE:SERVICE
Use this source only for one database.
-i, --no-idn
Do not use IDN encoding for host lookups.
-?, --help
Show help.
--usage
Short usage.
-V, --version
Show the version.

Examples 12

$ run as a normal user# run as root (or with sudo)

  1. 1Show a user

    getent passwd lisa
    lisa:x:1001:1001:Lisa Hassan:/home/lisa:/bin/bash

    Works for local and directory (LDAP/AD) users.

  2. 2Show a group and its members

    getent group wheel
    wheel:x:10:student,devops1

    The last field lists the members.

  3. 3Resolve a host name like programs do

    getent hosts server2
    192.168.122.20  server2

    Uses /etc/hosts first, then DNS (nsswitch order). nslookup/dig ask DNS only.

  4. 4All addresses of a name

    getent ahosts www.redhat.com

    IPv4 and IPv6, like a browser sees them.

  5. 5Find the port of a service

    getent services ssh https
    ssh                   22/tcp
    https                 443/tcp

    Reads /etc/services.

  6. 6Check a directory user from AD/IdM

    getent passwd anna@corp.example.com

    If it shows nothing, check SSSD (systemctl status sssd).

  7. 7All groups of a user, also from AD

    getent initgroups anna

    Fast check when sudo rules use AD groups.

  8. 8Look only in local files

    getent -s files passwd lisa

    Tells you if a user is local or from the directory.

  9. 9List all normal users

    getent passwd | awk -F: '$3 >= 1000 && $3 < 60000 {print $1}'

    nobody (65534) is not in this range.

  10. 10Read the password data (root)

    getent shadow lisa

    Like the line in /etc/shadow.

  11. 11Test if a user exists in a script

    getent passwd lisa >/dev/null && echo exists || echo missing

    Exit code 2 = not found.

  12. 12Show the order of the sources

    grep -E '^(passwd|group|hosts):' /etc/nsswitch.conf
    passwd:     files sss systemd
    group:      files sss systemd
    hosts:      files dns myhostname

    RHEL manages this file with authselect.

Watch out

  • getent passwd without a key may not list all directory users (SSSD does not enumerate by default).

Files

/etc/nsswitch.conf
Which sources each database uses, in which order.

Exit codes

0 :: Found. 1 :: Wrong arguments or unknown database. 2 :: One or more keys not found. 3 :: The database cannot list all entries.