Skip to content

id Command Cheat Sheet

The id command displays the real and effective User ID (UID), Group ID (GID), and group memberships for a user. It is the fundamental tool for verifying "who am I" in the system context.


Synopsis

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

Basic Usage

Current User

id
Output format: uid=1000(john) gid=1000(john) groups=1000(john),4(adm),27(sudo)

Specific User

id root
# Output: uid=0(root) gid=0(root) groups=0(root)

Specific Data Extraction

Useful for scripting when you only need a number.

id -u
# Output: 1000

Prints the primary group ID.

id -g

Prints all group IDs (primary + supplementary).

id -G
# Output: 1000 4 27

Display Names instead of Numbers (-n)

Must be used with -u, -g, or -G.

id -un   # Equivalent to `whoami`
id -gn   # Primary group name
id -Gn   # All group names

Real vs Effective ID

Files are accessed based on Effective IDs. Usually, Real and Effective IDs are the same. They differ when running setuid programs (like passwd or sudo).

  • -r: Print Real ID.
id -u -r

Comparison: id vs whoami vs groups

Command Output Use Case
whoami Username only Simple script checks
groups Group names Quick membership check
id Everything (IDs + Names) Detailed diagnostics

Scripting Examples

Check if Root

Preferred way to check for root privileges in scripts (checks UID 0).

if [ "$(id -u)" -eq 0 ]; then
    echo "Running as root"
else
    echo "Please run as root"
    exit 1
fi

Check Group Membership

if id -nG "$USER" | grep -qw "docker"; then
    echo "User found in docker group"
fi

Security Context (-Z)

On systems with SELinux (like Fedora, CentOS, RHEL), id can show the security context.

id -Z
# Output: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

Exit Status

Code Meaning
0 Success
1 User does not exist

Notes

  • The UID 0 is always reserved for root.
  • UIDs 1-999 are usually System Users (daemons).
  • UIDs 1000+ are regular users.