id (print user and group IDs)
The id command displays the real and effective user and group identifiers for the specified user or the current user if no user is specified. It is a quick way to verify a user's identity and group memberships. This command is crucial for system administrators to manage user permissions and troubleshoot access-related issues.
Example 1: Display current user's ID
# Display the user ID and group ID of the currently logged-in user
id
Explanation
This command, when executed without any arguments, provides the user ID (uid), primary group ID (gid), and all the groups the current user belongs to. It's a fundamental command for self-checking user context.
Example 2: Display a specific user's ID
# Show the user and group information for the user 'student'
id student
Explanation
By providing a username as an argument, the id command fetches and displays the UID, GID, and group memberships for that specific user. This is useful for administrators to check the details of any user account on the system.
Example 3: Show only the user ID
# Print only the user ID (uid) of the current user
id -u
Explanation
The -u option filters the output to show only the numerical user ID. This is particularly useful in shell scripts where you might need to check if a user has a specific UID, for instance, checking for the root user (UID 0).
Example 4: Show only the primary group ID
# Display only the group ID (gid) of the user 'student'
id -g student
Explanation
Using the -g option, the command outputs only the numerical primary group ID of the specified user. This is often used in scripts to verify a user's primary group before performing certain actions.
Example 5: Display all group IDs
# List all group IDs the current user is a member of
id -G
Explanation
The -G option prints all the numerical group IDs (both primary and supplementary) that the user belongs to. This is helpful for understanding the full scope of a user's group-based permissions.
Example 6: Print names instead of numbers
# Show the user name instead of the user ID for the current user
id -un
Explanation
Combining -u with -n (name), this command displays the username associated with the user ID. It provides a more human-readable output, which can be beneficial in scripts that log user activity. In RHEL, -un is equivalent to whoami.
Example 7: Print primary group name
# Display the primary group name for the user 'student'
id -gn student
Explanation
Similar to the previous example, -gn will output the name of the primary group for the specified user instead of its numerical ID. This is useful for scripts that need to work with group names rather than GIDs.
Example 8: Print all group names
# List the names of all groups the current user belongs to
id -Gn
Explanation
The -Gn options together will list the names of all groups a user is a member of. This provides a clear, readable list of a user's group affiliations, making it easy to audit permissions.
UNIX Variants
In most UNIX-like systems, including Solaris and FreeBSD, the id command and its options are largely the same as in RHEL.