The id command prints user and group identity for any account on the system. It shows the real user ID, primary group ID, and all supplemental group memberships — the quickest way to confirm what permissions an account holds.
The syntax is:
bashid [OPTIONS] [USERNAME]
Run without arguments to see the current user:
bashid
Output: uid=1000(linuxize) gid=1000(linuxize) groups=1000(linuxize),4(adm),27(sudo),998(docker)
Each field shows the numeric ID followed by the name in parentheses. uid = real user ID; gid = real primary group ID; groups = primary group plus all supplemental groups. Effective IDs appear in the output only when they differ from the real ones.
If SELinux is enabled, a context= field is appended automatically.
Query another user by name or UID:
bashid markid 1001
Name lookup takes precedence over numeric lookup. If a user named 1010 exists alongside a user with UID 1010, id 1010 returns the user whose name is 1010. Prefix with + to force numeric UID lookup:
bashid +1010
These options extract a single field — especially useful in scripts where you need one value rather than the full output:
| Option | Returns |
|---|---|
-u | Effective user ID |
-g | Effective primary group ID |
-G | All group IDs (space-separated) |
-n | Names instead of numbers (combine with -u, -g, or -G) |
-r | Real ID instead of effective (combine with -u, -g, or -G) |
-Z | SELinux security context |
-z | NUL-delimit output (for xargs -0) |
-n and -r are modifiers — they do not work alone. You must combine them with -u, -g, or -G:
bashid -un # effective username (same output as whoami)id -Gn # all group names (same output as the groups command)id -ur # real user ID as a number
Real vs effective IDs. The real user ID is the account that started the process. The effective user ID is what the kernel checks for permission decisions. They are usually identical. They diverge when a setuid binary runs — for example, passwd temporarily elevates privileges to write to /etc/shadow.
The -Z option prints the SELinux security context. If SELinux is not enabled, id returns: id: --context (-Z) works only on an SELinux-enabled kernel.
Check if running as root:
bashif [ "$(id -u)" -eq 0 ]; then echo "Running as root"else echo "Root privileges required" exit 1fi
Comparing to 0 is more reliable than checking for the string “root” — UID 0 is always root, regardless of how the account is named.
Check group membership:
bashif id -Gn | grep -qw docker; then echo "User is in the docker group"fi
-q makes grep exit silently with a status code. -w enforces a whole-word match, so a group named “sudocker” does not trigger a match for “docker”.
Store the current username for logging:
bashCURRENT_USER="$(id -un)"echo "Script started by $CURRENT_USER"
Use id to confirm your current identity, id -Gn to check group memberships, and id -u in scripts to test for root. Leave a comment below if you run into any issues.