id Command in Linux: Display User and Group Information

By 

Updated on

5 min read

Linux id command

The id command prints real and effective user and group IDs. It is one of the quickest ways to check which user account you are operating as, what primary group you belong to, and which supplemental groups grant you additional permissions.

This article covers the id command syntax, its options, and practical examples of using it in everyday administration and shell scripts.

Using the id Command

The syntax for the id command is as follows:

txt
id [OPTIONS] [USERNAME]

If the username is omitted, id displays information about the currently logged-in user.

When invoked without any option, id prints the real user ID (uid), the user’s real primary group ID (gid), and real IDs of the supplemental groups (groups) the user belongs to. Effective IDs are printed only when they differ from the real ones.

Terminal
id
output
uid=1000(linuxize) gid=1000(linuxize) groups=1000(linuxize),4(adm),27(sudo),998(docker)

If SELinux is enabled, id also prints the user’s security context (context):

output
uid=1000(linuxize) gid=1000(linuxize) groups=1000(linuxize) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

To obtain information about another user, pass the username or user ID as an argument:

Terminal
id mark
output
uid=1001(mark) gid=1001(mark) groups=1001(mark),998(docker)

When a user with a name identical to the supplied numeric ID exists on the system, the name lookup takes precedence. Prefix the ID with the + symbol to force numeric lookup.

For example, if you have a user with name 1010 and another user with ID 1010, running id 1010 will display information about the user whose name is 1010. To query the user with UID 1010 instead, run id +1010.

id Command Options

The id command accepts several options that limit output to a specific field. These are especially useful in shell scripts where you need a single value rather than the full line.

The -u (--user) option prints only the effective user ID:

Terminal
id -u
output
1000

The -g (--group) option prints only the effective primary group ID:

Terminal
id -g
output
1000

The -G (--groups) option prints the IDs of every group the user belongs to, including the primary group and all supplemental groups :

Terminal
id -G
output
1000 4 27 998

The -n (--name) option prints names instead of numeric IDs. It must be combined with -u, -g, or -G:

Terminal
id -un
output
linuxize

Running id -un produces the same output as the whoami command, and id -Gn is equivalent to the groups command.

The -r (--real) option prints real IDs instead of effective ones. Like -n, it must be combined with -u, -g, or -G:

Terminal
id -ur

In most situations the real and effective IDs are the same. They diverge when a process runs under a setuid or setgid binary.

The -Z (--context) option prints the security context of the current process:

Terminal
id -Z
output
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

If SELinux is disabled , id prints an error message:

output
id: --context (-Z) works only on an SELinux-enabled kernel

NUL-Delimited Output

The -z (--zero) option separates output items with a NUL character instead of whitespace, which is useful when piping into tools like xargs -0:

Terminal
id -zG

Using id in Scripts

The id command is commonly used in shell scripts to verify permissions before running privileged operations.

Check whether the current user is root:

sh
if [ "$(id -u)" -eq 0 ]; then
  echo "Running as root"
else
  echo "This script requires root privileges"
  exit 1
fi

Check whether the current user belongs to the docker group:

sh
if id -Gn | grep -qw docker; then
  echo "User is in the docker group"
else
  echo "User is not in the docker group"
fi

Store the current username in a variable for logging:

sh
CURRENT_USER="$(id -un)"
echo "Script started by $CURRENT_USER"

Quick Reference

OptionDescription
-u (--user)Print effective user ID
-g (--group)Print effective primary group ID
-G (--groups)Print all group IDs
-n (--name)Print names instead of numbers (combine with -u, -g, or -G)
-r (--real)Print real IDs instead of effective (combine with -u, -g, or -G)
-Z (--context)Print SELinux security context
-z (--zero)NUL-delimit output instead of whitespace

FAQ

What is the difference between real and effective user ID? The real user ID is the account that started the process. The effective user ID is the identity the kernel uses for permission checks. They are usually the same, but they can differ when a setuid binary (such as passwd or sudo) temporarily elevates privileges.

How do I check if a user belongs to a specific group? Run id -Gn username to list all group names, then pipe the output through grep -qw groupname. For example: id -Gn mark | grep -qw docker.

What is the difference between id and whoami? The whoami command prints only the effective username, which is equivalent to id -un. The id command provides more detail, including numeric IDs and all group memberships.

Conclusion

The id command displays user and group identity for any account on the system. For related tasks, see how to create users , manage groups , or list group memberships .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page