How to list all users and groups depends on how authentication is configured.
The most basic are the /etc/passwd
and /etc/group
files used for local authentication.
Using those files is insufficient to get a complete listing when user/group data is stored centrally, for instance in LDAP, NIS, Hesiod, etc.
Central authentication is usually configured in addition to the /etc/passwd
and /etc/group
files to allow the system to be booted and viable in single user mode by means of the Name Service Switch configuration file /etc/nsswitch.conf
If configured with NSS:
getent passwd
will show all user accounts: both those from /etc/passwd
and your central user directory.
getent group
will show all groups.
By monitoring the exit status of the getent
command you will also know if you did indeed list all users/groups (exit code 0
) or that you're missing some (exit code 3
indicates a database that does not support enumeration and you'll need to use appropriate tools for that specific database...).