How to Print Real and Effective User and Group IDs in Linux Using the id Command

In Linux security and process management, access control decisions rely entirely on numerical identifiers: the User ID (UID) and Group ID (GID). When troubleshooting permission denials, testing sudo access, or auditing user memberships, system administrators must inspect both real and effective identities. The standard, native core utility for inspecting user credentials across all Linux distributions is the id command. Understanding how to use the id command allows you to rapidly audit permissions, verify group memberships, and write robust shell scripts.

What Is the id Command in Linux?

The id utility is part of the standard GNU Core Utilities (coreutils) suite. When executed without arguments, it prints information about the current logged-in user. You can also specify any user on the system to inspect their credentials:

id [options] [username]

A typical output from running id displays three key attributes:

uid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev)
  • uid: The user ID number followed by the account name in parentheses.
  • gid: The primary group ID number and primary group name.
  • groups: A comma-separated list of all supplementary groups to which the user belongs, including system administrative groups such as sudo or wheel.

Understanding Real vs. Effective IDs

In Unix-like systems, a process has both a real ID and an effective ID:

  • Real ID (RUID/RGID): Identifies the user who originally launched the process or logged into the session.
  • Effective ID (EUID/EGID): Determines the actual access rights and file-system permissions granted to the process during execution.

When a process executes a SUID (Set User ID) binary or runs under sudo, the effective UID switches to root (0) while the real UID remains tied to the originating user account. The id command automatically highlights discrepancies between real and effective identities when they diverge.

Useful Options and Flags for the id Command

While the default output provides an all-in-one summary, the id command includes several practical flags for filtering specific parameters, particularly in automation scripts:

1. Print Only the Numerical User ID (-u)

To extract solely the effective User ID, use the -u flag:

id -u

In shell scripts, this command is widely used to verify whether a script is executing with root privileges:

if [ $(id -u) -ne 0 ]; then
    echo "Error: This script must be run as root." >&2
    exit 1
fi

2. Print the Real User ID Instead of Effective (-r)

To inspect the real identity when running under privilege elevation, combine -r with -u or -g:

id -ru

3. Print the Numerical Primary Group ID (-g)

To extract the primary group ID assigned to the current session:

id -g

4. Print Group Names Instead of Numbers (-n)

By default, specific query flags return numbers. Add the -n option to display the human-readable string name instead:

id -un
id -gn

5. Print All Supplementary Groups (-G)

To list all supplementary group IDs assigned to an account, use -G. Combine it with -n to print group names separated by spaces:

id -Gn

This is ideal for quickly verifying whether a user has been granted membership in administrative groups like docker or lxd.

Auditing Another User Account

You do not need root privileges to inspect the identity and memberships of other system accounts. Simply append the target username to the command:

id www-data

Output:

uid=33(www-data) gid=33(www-data) groups=33(www-data)

Displaying Security Context (SELinux / AppArmor)

On security-hardened Linux systems running SELinux (such as Red Hat, Rocky Linux, or Fedora), passing the -Z flag prints the current process security context:

id -Z

This outputs the user, role, type, and MLS level (e.g., unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023) to assist in policy debugging.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.