Linux commands
Browse topics

Checking users and groups with id

Version
unversioned
Review
Draft

Summary#

id prints the user ID, primary group and supplementary groups of the current process or of a named account.

Syntax#

sh
id
id -u
id -un
id -nG alice
whoami
getent passwd alice
who

-u selects the user ID, -g the primary group, -G all groups and -n prints names. getent passwd reads the account from the configured databases, including network directories. who lists login sessions.

Example#

sh
(
  set -eu
  test "$(whoami)" = "$(id -un)" && echo 'whoami matches id -un'
  id -u root
  getent passwd root | cut -d: -f1,3
  id dl-no-such-user 2>/dev/null || echo "status $?"
)

Expected output#

Text
whoami matches id -un
0
root:0
status 1

Common mistakes#

A running session keeps the groups it started with. After adding a user to a group, id name shows the new group while id in the old session does not; log in again. Check for root with [ "$(id -u)" -eq 0 ] rather than the user name or $USER, which can be changed. id reports no such user for an unknown account. Accounts from a directory service may be missing from /etc/passwd.

Version applicability#

This entry describes id from GNU Coreutils and getent from the GNU C Library.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0