← Full reference

id, groups, whoami

Who am I and which groups am I in — UID, GID, the group list of the current process or any user

id prints the numeric user (UID) and group (GID) identifiers along with their names. Without an argument — for the current process; with a user name — based on /etc/passwd and /etc/group. groups and whoami are short versions for the frequent questions "which groups am I in" and "who am I running as".

Syntax

id [options] [user…]
groups [user…]
whoami

id options

OptionWhat it prints
(no options)uid=1001(alice) gid=1001(alice) groups=1001(alice),1002(devs)
-uonly the (effective) UID, e.g. 1001
-gonly the primary GID
-Gall GIDs separated by spaces: the primary group and supplementary ones
-nnames instead of numbers; works only together with -u, -g or -G
-rthe real ID instead of the effective one (with -u, -g, -G); they differ for setuid programs
-zseparate values with a null byte (only with -u/-g/-G)
-ZSELinux context; an error on a kernel without SELinux

Common combinations: id -un — the name (same as whoami), id -gn — the primary group's name, id -Gn — names of all groups (same as groups).

Reading the output

FieldMeaning
uid=1001(alice)the user's number and name; root is always 0
gid=1001(alice)the primary group: new files are created with it
groups=…all groups, including the primary one; file access permissions depend on them

groups alice prints in the format alice : alice devs, and groups without an argument — just alice devs.

Exit codes

CodeWhen
0all is well
1no such user: id: 'bob': no such user

Examples

# who am I running as
whoami
id -un

# full information about a user
id alice

# just the number — handy in scripts
id -u alice

# check that a script is run as root
if [ "$(id -u)" -ne 0 ]; then echo "root required" >&2; exit 1; fi

# is alice in the devs group
id -nG alice | grep -qw devs && echo "yes"

# does the user exist
id bob >/dev/null 2>&1 || echo "no such user"

# groups of the current process and groups from the database — compare after usermod -aG
id -Gn
id -Gn "$(whoami)"

Pitfalls

  • id without an argument and id name may differ. Without an argument you see the groups of the already running process; with a name — what's currently recorded in /etc/group. After usermod -aG devs alice, id alice shows devs right away, but alice's old session won't have the group until she logs in again (or runs newgrp devs, su - alice).
  • -n without -u/-g/-G is an error: id -n prints "cannot print only names or real IDs in default format".
  • whoami shows the effective user, not the one who logged in. After sudo -i or su it's root. Who originally logged in is shown by who or logname (without a terminal logname prints "no login name").
  • $USER is just a variable; it may be missing (in cron, in a container) or overridden. In scripts id -un or bash's $EUID is more reliable.
  • The name in parentheses is printed only if the number exists in the user database. Files from another system or an archive show bare numbers in ls -l too.

See also

useradd · su · sudo · getent · chown · who-last