Users

The getent command

Queries users, groups and hosts as the system resolves them, including network sources.

Syntax

getent passwd|group|hosts [nombre]Try it in the terminal →

Most useful options

-s, --service <servicio>Changes the name service for all databases. With database:service, only for one
--usagePrints a short usage summary and exits
-?, --helpPrints a usage summary and exits
-V, --versionPrints the version, license and warranty disclaimer

Copy-ready examples

$ getent passwd alumnothe account data for alumno, read from every configured source.

Example output

alumno:x:1000:1000:Alumno Aula:/home/alumno:/bin/bash
$ getent group sudothe sudo group and its members, at the end of the line.

Example output

sudo:x:27:alumno
$ getent passwd 1000looks up by numeric UID instead of by name.

Example output

alumno:x:1000:1000:Alumno Aula:/home/alumno:/bin/bash
$ getent passwd nadie; echo $?if the key does not exist it prints nothing and the exit code is 2.

Example output

2
$ getent hosts localhostresolves a host name following the order in /etc/nsswitch.conf.

Example output

127.0.0.1       localhost

You can try them in the browser terminal: it does not touch your computer.

Typical mistakes

  • getent also queries network sources (LDAP, SSSD or others listed in /etc/nsswitch.conf). An account can exist even if it is not in /etc/passwd.
  • Without a key, getent prints the whole database: in a script, always pass the name or number you are looking for.
  • getent shadow needs root permissions, because /etc/shadow is not readable by ordinary users.

Differences from similar commands

  • id: shows a user's UID, GID and groups. To check whether an account exists, getent is more direct.
  • groups: lists only the groups of a user or of the session, without the other database fields.

Related commands

FAQ

Which exit code means the key does not exist?

Code 2. Running getent passwd nadie; echo $? prints 2, and in a script you only need to check that value.

Why is it better than grep /etc/passwd?

Because it queries every system source, not only the local file.

Can I use getent to see passwords?

getent passwd only shows an x: the hash lives in /etc/shadow. getent shadow displays it, but only with root permissions.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course