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
| Option | What it prints |
|---|---|
| (no options) | uid=1001(alice) gid=1001(alice) groups=1001(alice),1002(devs) |
-u | only the (effective) UID, e.g. 1001 |
-g | only the primary GID |
-G | all GIDs separated by spaces: the primary group and supplementary ones |
-n | names instead of numbers; works only together with -u, -g or -G |
-r | the real ID instead of the effective one (with -u, -g, -G); they differ for setuid programs |
-z | separate values with a null byte (only with -u/-g/-G) |
-Z | SELinux 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
| Field | Meaning |
|---|---|
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
| Code | When |
|---|---|
0 | all is well |
1 | no 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
idwithout an argument andid namemay differ. Without an argument you see the groups of the already running process; with a name — what's currently recorded in/etc/group. Afterusermod -aG devs alice,id aliceshowsdevsright away, but alice's old session won't have the group until she logs in again (or runsnewgrp devs,su - alice).-nwithout-u/-g/-Gis an error:id -nprints "cannot print only names or real IDs in default format".whoamishows the effective user, not the one who logged in. Aftersudo -iorsuit'sroot. Who originally logged in is shown bywhoorlogname(without a terminallognameprints "no login name").$USERis just a variable; it may be missing (in cron, in a container) or overridden. In scriptsid -unor bash's$EUIDis 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 -ltoo.