Skip to content

getent

Looks up entries in the system databases: users, groups, hosts and services, from local files and from directory services like LDAP.

getent output

Syntax

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

Options

Database Contents
passwd User accounts
group Groups and their members
shadow Password data (only as root)
hosts Host names and IP addresses
services Network services and their ports
Option Description
-s SOURCE, --service=SOURCE Only ask a specific source, e.g. files or ldap

Without a key, getent lists all entries of the database. The exit status is 0 if something was found and 2 if not.

Examples

Look up a user:

getent passwd nox
nox:x:1000:1000:Nox,,,:/home/nox:/bin/bash

Find the user for a UID:

getent passwd 33
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin

Show the members of a group:

getent group sudo
sudo:x:27:nox

Check whether a user exists:

getent passwd ghost
echo $?
2

Resolve a host name like the system does:

getent hosts tuxdex-srv
127.0.1.1       tuxdex-srv.example.com tuxdex-srv

Find the port of a service:

getent services ssh
ssh                   22/tcp

Show the password data of a user:

sudo getent shadow nox
nox:$y$j9T$ExampleSaltOnly$ExampleHashOnlyNotARealPasswordHash:20752:0:99999:7:::

The fields after the hash: last change (days since 1970), minimum and maximum age and warning period.

When do I need it?

  • To check in scripts whether a user or group exists, before creating it
  • On servers with LDAP, Active Directory or SSSD, where users are not in /etc/passwd: getent asks all configured sources
  • To test name resolution the way programs see it, including /etc/hosts

getent instead of grep /etc/passwd

grep nox /etc/passwd only finds local users and also matches nox inside other names. getent passwd nox searches exactly for this name in all sources.

Find the group for a GID

getent group 1001 shows which group has GID 1001, for example when ls -l shows a number instead of a group name.

groups ยท id