id Command
The id command in Linux is used to display the user and group identity information for the current user or a specified user. It provides details about the user ID (UID), primary group ID (GID), and all supplementary group IDs and names.
Syntax
Description
The id command is a fundamental utility for understanding user and group permissions in a Linux environment. It retrieves information from the system's user and group databases (typically /etc/passwd and /etc/group) and presents it in a human-readable format. This is invaluable for system administration, security auditing, and troubleshooting access issues.
Key information provided by id:
- User ID (UID): A unique numerical identifier for a user.
- Primary Group ID (GID): The main group associated with the user.
- Supplementary Group IDs: Any additional groups the user is a member of.
- Corresponding names for all IDs.
Common Options
| Option | Description |
|---|---|
-a |
Ignore, for compatibility with other versions. |
-g, --group |
Print only the effective group ID. |
-G, --groups |
Print all group IDs. |
-n, --name |
Print a name instead of a number for the user or group. |
-r, --real |
Print the real ID instead of the effective ID. |
-u, --user |
Print only the effective user ID. |
-z, --zero |
Delimit entries with NUL characters, not whitespace. |
Examples
Display current user's information
# Example Output: uid=1000(user) gid=1000(user) groups=1000(user),4(adm),24(cdrom),27(sudo)
Shows the UID, GID, and all groups for the currently logged-in user.
Display information for a specific user
# Example Output: uid=1001(john_doe) gid=1001(john_doe) groups=1001(john_doe),100(users),999(developers)
Displays the user and group information for the user john_doe.
Print only the effective user ID
# Example Output: 1000
Useful for scripting when only the numerical user ID is needed.
Print only all group names
# Example Output: user adm cdrom sudo
Combines -G (all groups) and -n (names) to list all group names.
Check if a user exists (using exit status)
echo $?
# Output: 1 (if user does not exist) # Output: 0 (if user exists)
A common scripting technique to check for user existence based on the command's exit status.