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.
idid <user> — Print UID, GID, and groups for a specific user.
id www-dataid root — Show information for the root user. UID and GID should both be 0.
id rootNumeric IDs
id -u — Print only the UID (user ID) of the current user as a number.
id -uid -u <user> — Print only the UID of a specific user.
id -u www-dataid -g — Print only the primary GID (group ID) of the current user.
id -gid -g <user> — Print only the primary GID of a specific user.
id -g deployid -G — Print all group IDs (primary + supplementary) as space-separated numbers.
id -Gid -G <user> — Print all group IDs for a specific user.
id -G jpkNames Instead of Numbers
id -un — Print only the username (name instead of UID). Equivalent to whoami.
id -unid -gn — Print only the primary group name.
id -gnid -Gn — Print all group names (primary + supplementary) as space-separated names.
id -Gnid -Gn <user> — Print all group names for a specific user.
id -Gn jpkPractical 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 $USERstat -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.phpRelated Tools
whoami — Print only the current username. Equivalent to id -un.
whoamigroups — Print all group names the current user belongs to. Equivalent to id -Gn.
groupsgroups <user> — Print group memberships for a specific user.
groups www-datagetent passwd <user> — Show the full passwd entry for a user: UID, GID, home, shell.
getent passwd www-datagetent group <group> — Show group details and all members of a group.
getent group sudogrep <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
- Wikipedia: User identifier – background on UID, GID and real versus effective identifiers
- Ubuntu docs: Adding and deleting users – official guide to managing users, groups and IDs