Linux id Command Examples

The 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.

Example 1: Display Current User Information

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.

Example 2: Display Information for a Specific User

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.

Example 3: Display Only User ID (UID)

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.

Example 4: Display Only Group ID (GID)

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.

Example 5: Display Only Group Names

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.

Example 6: Display Only Group IDs

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.

Example 7: Display Real IDs Instead of Effective IDs

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).

Note: Effective IDs are used when a process runs with temporary privileges (e.g., via sudo).

Example 8: Display User Name Only

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.

Example 9: Check Group Membership for a Specific User

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.

Example 10: Combine with Other Commands

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:

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.