Linux id Command: Inspect UID, GID & Group Memberships

Overview

Understanding identity boundaries in Linux requires inspecting user identifiers (UIDs), primary group identifiers (GIDs), and supplementary group memberships. This hands-on tutorial walks through practical applications of the id utility, breaking down default terminal output, isolating specific attributes with targeted command-line switches, and implementing robust privilege verification routines inside production shell scripts.

Inspecting Active Session Identity with the id Command

Before dissecting account details, establish the identity of the current terminal session using whoami.
As displayed below, the shell immediately reports that operations are being executed under the root account.

Verifying current session username with whoami

Invoking id without extra parameters produces a complete overview of the active session’s security credentials, displaying its user identifier (UID), primary group identifier (GID), and all associated supplementary groups simultaneously.
The output snippet uid=0(root) denotes the superuser boundary, whereas gid=0(root) alongside groups=0(root) maps out both the primary group and supplemental memberships tied to this administrative shell.
⚙️ [Core Options]

-u : Prints only the numeric effective user ID (UID).
-g : Outputs solely the numeric effective primary group ID (GID).
-G : Enumerates all supplementary and primary group IDs as space-delimited values.
-n : Displays human-readable names instead of numeric identifiers (paired with -u, -g, or -G).
-r : Discloses the real ID rather than the effective ID (paired with -u, -g, or -G).
-z : Delimits output fields using NUL characters rather than standard whitespace.

Default id command output overview

Extracting Specific UID, GID, and Group Attributes via Flags

Passing the -u flag isolates the effective user ID, returning only the raw integer.
Here, the command yields 0, immediately signaling that the current process context possesses root-level administrative authority.

Retrieving raw UID with id -u

Combining the name flag with the user switch via id -un transforms the numeric UID into its plain-text username.
The terminal prints root, demonstrating an efficient technique for capturing the active username directly into shell automation variables.

Extracting username as string with id -un

When running id -g, the utility filters out extraneous metadata and outputs solely the active user’s primary group identifier (GID).
Observing 0 confirms that the primary group matches the administrative group index.

Displaying primary GID using id -g

Supplying id -gn resolves the primary numeric GID back to its textual alias.
The console prints root, making it straightforward to confirm the primary group label assigned to the account.

Printing primary group name using id -gn

The id -G command aggregates all group associations (primary and supplementary alike) into a clean, space-separated sequence of numeric IDs.
In this minimal administrative context, only group 0 appears in the list.

Listing all group IDs with id -G

Appending the name modifier as id -Gn converts every associated group ID into its corresponding group name separated by spaces.
The resulting entry root confirms all group memberships in a readable format.

Listing group names using id -Gn

Auditing System Accounts and Tracking Supplementary Groups

Targeting an explicit account name with id root extracts credential data for that specific user rather than the active session.
The shell prints uid=0(root), gid=0(root), and groups=0(root), illustrating how administrators can inspect user privileges across the system on demand.

Querying root user credentials with id root

Executing id nobody inspects the environment’s standard low-privilege unassigned account.
Outputting uid=65534(nobody) along with gid=65534(nogroup) highlights how Linux isolates unprivileged daemon processes using elevated numerical ranges.

Inspecting nobody user attributes with id nobody

To demonstrate group inheritance, provision a sample user equipped with a custom home directory and login shell via useradd -m -s /bin/bash testuser.

Creating test user with useradd

Next, establish a supplementary organizational container by invoking groupadd testgroup.

Creating secondary test group with groupadd

Append the newly minted account into the supplementary group by applying usermod -aG testgroup testuser.

Assigning user to supplementary group with usermod

Auditing the account with id testuser reveals the updated group topology.
Beyond the primary association gid=1001(testuser), the supplementary descriptor 1002(testgroup) now registers cleanly under the groups mapping.

Verifying updated account mappings with id testuser

Running id -Gn testuser formats all active group assignments into human-readable text.
Both the primary identifier testuser and the secondary grouping testgroup display side by side, separated by a single space.

Checking group names for testuser with id -Gn

Once verification is complete, purge the sandbox account along with its home directory via userdel -r testuser 2>/dev/null || true.

Cleaning up test user with userdel

Finally, strip out the temporary group asset using groupdel testgroup 2>/dev/null || true.

Removing test group with groupdel

Practical Bash Scripting: Enforcing Root Privileges via id

In administrative shell scripts, programmatically capturing the current user ID is critical for privilege verification; assign the numeric output to a variable using CURRENT_UID=$(id -u).

Assigning current UID to shell variable

Confirming the stored value with echo "Current User ID: $CURRENT_UID" verifies the capture process.
The terminal prints Current User ID: 0, validating that the superuser identifier has been successfully held in memory.

Printing captured UID value to stdout

The inline conditional [ "$CURRENT_UID" -eq 0 ] && echo "Running as root user" || echo "Running as standard user" evaluates whether the UID equals zero, branching execution flow based on administrative status.
Because the evaluation resolves affirmatively, Running as root user prints directly to the console, demonstrating an airtight pattern for gating privileged script execution.

Evaluating root privilege conditional branch

This walkthrough highlighted how the Linux id command provides immediate visibility into UIDs, GIDs, and supplementary memberships across users and scripts alike.
Mastering these flags ensures accurate permission auditing and strengthens privilege validation throughout your Linux administration workflows.