id Command ExamplesThe id command in Linux displays user and group information for the current user or a specified user, including user ID (UID), group ID (GID), and supplementary group memberships. It is useful for checking user privileges and debugging permission issues. Below are 10 examples demonstrating various uses of the id command with detailed explanations.
id
Displays the user ID (UID), primary group ID (GID), and supplementary groups for the current user.
Sample Output:
uid=1000(john) gid=1000(john) groups=1000(john),4(adm),24(cdrom),27(sudo)
Explanation: Shows UID 1000 for user john, primary GID 1000, and membership in groups adm, cdrom, and sudo.
id alice
Shows user and group information for the specified user alice.
Sample Output:
uid=1001(alice) gid=1001(alice) groups=1001(alice),27(sudo),100(users)
Explanation: Displays UID, GID, and groups for alice. Useful for verifying another user’s group memberships.
id -u
Prints only the UID of the current user.
Sample Output:
1000
Explanation: Outputs just the numeric UID, useful for scripts needing only the user ID.
id -g
Prints only the primary GID of the current user.
Sample Output:
1000
Explanation: Shows the numeric primary group ID, often used in automation scripts.
id -Gn
Lists the names of all groups the current user belongs to, including the primary and supplementary groups.
Sample Output:
john adm cdrom sudo
Explanation: Outputs group names without IDs, useful for checking group memberships in a readable format.
id -G
Lists the numeric IDs of all groups (primary and supplementary) the current user belongs to.
Sample Output:
1000 4 24 27
Explanation: Shows numeric GIDs, useful for scripts or permission checks.
id -r
Displays the real UID and GID instead of effective IDs, useful when running commands with sudo or su.
Sample Output: (If run under sudo)
uid=1000(john) gid=1000(john) groups=1000(john),4(adm),24(cdrom),27(sudo)
Explanation: Shows the real user’s IDs, not the effective IDs (e.g., root when using sudo).
sudo).id -un
Prints only the username of the current user.
Sample Output:
john
Explanation: Outputs the username without the UID, useful for scripts needing the username string.
id -Gn alice
Lists the names of all groups the user alice belongs to.
Sample Output:
alice sudo users
Explanation: Shows alice’s group memberships, useful for verifying access to specific resources.
id -u | xargs -I {} grep "^{}:" /etc/passwd
Uses the UID from id -u to search for the user’s entry in /etc/passwd, retrieving additional user details.
Sample Output:
1000:john:x:1000:1000:John Doe:/home/john:/bin/bash
Explanation: Combines id with grep to display the user’s /etc/passwd entry, showing details like home directory and shell.
Additional Tips:
id -u or id -g in scripts to check user or group IDs programmatically.id to verify group memberships when troubleshooting file access issues.id as another user with sudo -u username id to check their IDs.id nobody) to understand their permissions.Note: The id command retrieves information from /etc/passwd and /etc/group. Output may vary slightly depending on the system’s user and group configuration. Check the man page (man id) for additional options and details specific to your system.