id — Show User and Group IDs

id prints the UID, GID and group memberships of a user – ideal for permission troubleshooting, root checks in scripts and debugging access on Linux.

id answers "who am I, and what am I allowed to do?" in a single line: it prints the UID (user ID), the primary GID (group ID) and every group membership, plus the SELinux security context where applicable. That makes it the go-to tool when file permissions misbehave, a service runs under the wrong account, or a script needs to know whether it is running as root. This guide walks you through the options for humans and machines alike – from the full overview to machine-readable single values for your scripts.

Basic Usage

id — Print UID, GID, and all group memberships of the current user.

id

id <user> — Print UID, GID, and groups for a specific user.

id www-data

id root — Show information for the root user. UID and GID should both be 0.

id root

Numeric IDs

id -u — Print only the UID (user ID) of the current user as a number.

id -u

id -u <user> — Print only the UID of a specific user.

id -u www-data

id -g — Print only the primary GID (group ID) of the current user.

id -g

id -g <user> — Print only the primary GID of a specific user.

id -g deploy

id -G — Print all group IDs (primary + supplementary) as space-separated numbers.

id -G

id -G <user> — Print all group IDs for a specific user.

id -G jpk

Names Instead of Numbers

id -un — Print only the username (name instead of UID). Equivalent to whoami.

id -un

id -gn — Print only the primary group name.

id -gn

id -Gn — Print all group names (primary + supplementary) as space-separated names.

id -Gn

id -Gn <user> — Print all group names for a specific user.

id -Gn jpk

Practical Examples & Scripts

[ $(id -u) -eq 0 ] && echo root — Check if the current user is root (UID 0). Common pattern in install scripts.

[ $(id -u) -eq 0 ] && echo 'Running as root' || echo 'Not root'

id -nG | grep -qw docker && echo 'in docker group' — Check if the current user is a member of the docker group.

id -nG | grep -qw docker && echo 'docker access OK'

id -u $USER — Print the UID of the user stored in the $USER environment variable.

id -u $USER

stat -c '%u %g' <file> — Show the UID and GID of a file's owner. Useful to compare with id output.

stat -c '%u %g' /var/www/html/index.php

whoami — Print only the current username. Equivalent to id -un.

whoami

groups — Print all group names the current user belongs to. Equivalent to id -Gn.

groups

groups <user> — Print group memberships for a specific user.

groups www-data

getent passwd <user> — Show the full passwd entry for a user: UID, GID, home, shell.

getent passwd www-data

getent group <group> — Show group details and all members of a group.

getent group sudo

grep <user> /etc/passwd — Show the raw passwd entry for a user directly from the system file.

grep www-data /etc/passwd

Conclusion

id is a read-only command with no destructive risk, making it the fastest answer to any permission question. For humans the bare call gives the full overview; for scripts the machine-readable variants are invaluable: id -u for the raw UID, id -g for the primary group and id -nG for a space-separated list of group names. The classic [ "$(id -u)" -eq 0 ] root check belongs in every serious install script. One detail worth knowing: for setuid programs the real and effective UID differ – id reports both, so you can see which privileges a process actually runs with.

Further Reading

  • useradd – create new user accounts with their UID and groups
  • su – switch to another user or to root
  • sudo – run individual commands with elevated privileges