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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

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

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

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.

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.

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

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

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).

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.

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.

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.