id
The id command in Linux is used to display user identity information. It prints real and effective user IDs (UIDs) and group IDs (GIDs), along with all the groups the user belongs to. It's a quick way to check group memberships and user privilege levels.
Basic Syntax
id [OPTION]... [USER]
If USER is omitted, id displays information for the current user.
Common Flags/Options
| Option | Description |
|---|---|
-u, --user | Print only the effective user ID (UID). |
-g, --group | Print only the effective group ID (GID). |
-G, --groups | Print all group IDs (GIDs) the user belongs to. |
-n, --name | Print the names instead of the IDs (used with -u, -g, or -G). |
-r, --real | Print the real ID instead of the effective ID, with -u, -g, or -G. |
Real-world Examples
1. View current user's IDs and groups
Running id without any arguments shows detailed information for your current session.
id
Output:
uid=1000(john) gid=1000(john) groups=1000(john),4(adm),27(sudo),120(docker)
2. View another user's information
Pass a username as an argument to see their UID, GID, and group memberships.
id alice
Output:
uid=1001(alice) gid=1001(alice) groups=1001(alice),27(sudo)
3. Get only the current User ID
This is often used in shell scripting to check for specific UIDs (e.g., UID 0 for root).
id -u
Output:
1000
4. Get the names of all groups a user belongs to
To list the group names rather than their numerical IDs, combine -G and -n.
id -Gn john
Output:
john adm sudo docker