The id
command in Linux is used to display the user and group information for the currently logged-in user or a specified user. It provides details such as user ID (UID), group ID (GID), and the groups that the user belongs to.
Syntax
id [OPTION] [USER]
OPTION
: Optional flags to modify the output format.USER
: The username or user ID whose information you want to retrieve. If no username is provided, it defaults to the current user.
Basic Usage
- Display current user’s information:
id
Example output:
uid=1000(username) gid=1000(username) groups=1000(username),27(sudo),24(cdrom)
This shows:
uid=1000(username)
: The user’s unique identifier (UID).gid=1000(username)
: The primary group ID (GID) and group name.groups=1000(username),27(sudo),24(cdrom)
: The list of groups the user belongs to, including group IDs and group names.
Options for id
Command
-u
(user ID): Display the user ID (UID) of the specified user or the current user if no user is specified.id -u
Example:
1000
-g
(group ID): Display the group ID (GID) of the specified user or the current user’s primary group if no user is specified.id -g
Example:
1000
-G
(all group IDs): Display all group IDs (GIDs) the user belongs to.id -G
Example:
1000 27 24
-n
(name instead of numeric ID): Use this option to display the group name or user name instead of the numeric UID or GID.id -un
Example:
username
For groups:
id -gn
Example:
username
-a
: Display all available information in a more readable format.id -a
-r
(real IDs): This option shows the real UID and GID (not the effective ones). It’s especially useful for users with sudo privileges.id -r
Examples
- Show information for a specific user:
id username
Example output:
uid=1001(username) gid=1001(username) groups=1001(username),27(sudo)
- Show only the UID of the current user:
id -u
Example output:
1000
- Show the list of all groups a user belongs to:
id -G
Example output:
1000 27 24
- Show the user name instead of UID:
id -un
Example output:
username
- Show the group name instead of GID:
id -gn
Example output:
username
Common Use Cases
- Checking User Information:
Use theid
command to quickly check the UID, GID, and groups associated with any user, either by specifying the user or by default for the current user. - Determining Group Membership:
Theid -G
option is particularly useful to see which groups a user belongs to, which is often used for troubleshooting permissions or verifying group access. - Automation and Scripts:
In bash scripts or automation, you can useid
to check whether the user belongs to a specific group or to extract a user’s UID/GID for further processing.
Conclusion
The id
command is a simple yet powerful tool in Linux for retrieving user and group information. It’s particularly useful for administrators and developers to quickly verify user and group details, troubleshoot access issues, or gather user information for automation.