opscape

System and users

id

Print the user and group IDs of the current user or of a named user.

id [OPTION...] [USER...]

10 of 10 examples run in Debian 13.7 on 2026-10-31 · coreutils 9.7-3 · manual: id(1)

id shows the identity the kernel uses for permission checks: the user ID (uid), the primary group ID (gid) and the supplementary groups. With no argument it reports the current process; with a user name it looks that account up in the user database (/etc/passwd, /etc/group, or LDAP and friends through NSS).

The single-value options make it the standard tool for scripts: -u for the user ID, -g for the primary group, -G for all groups, and -n to print names instead of numbers. whoami is just id -un and groups is just id -Gn.

One subtlety matters in practice: group membership is read once, at login. After someone adds you to a group, id USER (a database lookup) shows the new group immediately while plain id (your running session) does not until you log in again.

Examples

Show your user and groups

id
explain
output
uid=1000(dev) gid=1000(dev) groups=1000(dev)

uid, primary gid, and the supplementary groups, each as number and name. The sandbox user dev belongs only to its own group.

Print just the numeric user ID

id -u
explain
output
1000

-u alone prints the effective UID; 1000 is the first regular account on Debian.

Print the user name

id -un
explain
output
dev

-n turns any of -u, -g or -G from numbers into names. This is exactly what whoami prints.

Print the primary group name

id -gn
explain
output
dev

The primary group is the group that owns the files you create (unless the directory is setgid).

List all group names, space-separated

id -Gn root
explain
output
root

-G lists every group ID, primary first; with -n, their names. Given a user name, id looks the account up instead of describing the current process.

Look up another account

id www-data
explain
output
uid=33(www-data) gid=33(www-data) groups=33(www-data)

www-data is the Debian account web servers run as, with UID and GID 33. Any user in the database can be inspected without being that user.

Run as root

id
explain
output
uid=0(root) gid=0(root) groups=0(root)

Root has UID 0 and GID 0. Permission checks are skipped for UID 0, whatever the name is.

Check whether an account exists

if id deploy >/dev/null 2>&1; then echo "deploy exists"; else echo "no deploy user"; fi
id deploy
outputexit 1
no deploy user
id: ‘deploy’: no such user

For an unknown name id prints no such user and exits 1, so the exit status doubles as an existence test. The second line shows the error message the first one hides.

Test whether the current user is in a group

for g in dev sudo; do
  if id -nG | grep -qw "$g"; then echo "in $g"; else echo "not in $g"; fi
done
output
in dev
not in sudo

grep -w matches whole words, so being in devops would not count as being in dev. The check uses the running session's groups, the ones that actually apply to file access.

Use the UID in a root check

[ "$(id -u)" -eq 0 ] && echo "root" || echo "not root (uid $(id -u))"
explain
output
not root (uid 1000)

Checking for UID 0 is more robust than comparing names. In bash, $EUID holds the same number without running a command.

Common pitfalls

  • Expecting a new group to work right after usermod -aG docker alice. Running processes keep their old group list: log out and back in, or start a shell with newgrp docker. id alice shows the new group sooner than id does, which is a common source of confusion.

  • Parsing the default output with cut or sed. Use the single-value options (-u, -gn, -Gn) instead.

  • id -n on its own is an error: it only modifies -u, -g or -G.

GNU vs BSD and macOS

The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).

  • The short options -u -g -G -n -r work on GNU and BSD/macOS. GNU adds -z (NUL-separated) and the SELinux context -Z; macOS adds -p (human-readable) and -P (passwd-file line).

Options worth knowing

A selection, described in our own words. For the full list, see id(1) or id --help.

OptionWhat it does
-u, --userPrint only the effective user ID.
-g, --groupPrint only the effective (primary) group ID.
-G, --groupsPrint all group IDs.
-n, --namePrint names instead of numbers; use with -u, -g or -G.
-r, --realPrint the real ID instead of the effective one; use with -u, -g or -G.
-z, --zeroSeparate entries with NUL instead of spaces.
-Z, --contextPrint only the SELinux security context.
-aIgnored; accepted for compatibility.

Related