Linux Command Reference ยท Permissions and Users
Linux id command
Reference for the Linux id command: print real and effective user and group IDs, list group memberships, use -u -g -G -n options, and check for root in scripts.
id prints the identity of a user: the numeric user ID (UID), the primary group ID (GID) and all supplementary groups, each with its name when the system can resolve it. With no argument it describes the current process, which makes it the quickest way to answer "who am I and what groups do I belong to?" and the standard way for a script to check whether it runs as root (UID 0).
Linux decides access with numbers, not names. Names such as alice come from /etc/passwd and /etc/group (or LDAP and similar services), and id just translates.
id
id -u
id alice
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
id [OPTION]... [USER]...
| Option | Description |
|---|---|
-u, --user |
Print only the effective user ID. |
-g, --group |
Print only the effective primary group ID. |
-G, --groups |
Print all group IDs: the primary one first, then the supplementary groups. |
-n, --name |
Print names instead of numbers (with -u, -g or -G). |
-r, --real |
Print the real ID rather than the effective one. |
-z, --zero |
Separate entries with NUL instead of spaces or newlines. |
-Z, --context |
Print the SELinux security context (on systems with SELinux). |
USER |
Describe this user instead of the current process (name or numeric ID). |
Default output format: uid=1000(alice) gid=1000(alice) groups=1000(alice),27(sudo),999(docker). A name in parentheses is left out when the ID has no name, as on this site's runner.
Returns: 0 on success, 1 for an unknown user or an unreadable ID.
Errors: id: 'name': no such user; cannot find name for user ID N when -n is used for an ID that has no entry.
Usage notes
- The first group is the primary group, the one given to files you create. The others come from
/etc/group(or a directory service) and were fixed when you logged in. Afterusermod -aGyou must log in again. - Effective versus real. A setuid program runs with the effective ID of its owner and keeps the caller as the real ID.
idshows effective by default; use-rfor real. idof another user shows their configured groups, not the groups of their running processes.- For a script root check use
id -u, not$USERorwhoami, since$USERis just an environment variable that can be wrong (for example undersudo -Eor in cron). Bash also provides$UIDand$EUID. id -nGgives names for use in conditions such asid -nG | tr ' ' '\n' | grep -qx docker.- Inside containers the numbers matter: a bind-mounted file is owned by a UID, not a name, so check
id -uin both places when permissions look wrong. - Names need an entry. Containers and minimal systems often run with a UID that has no
/etc/passwdline; thenidprints just the numbers andwhoamifails. - Compare with
groups, which prints only names of the groups, andwho am i, which shows the login name from the terminal session.