id - Linux


Overview

The id command in Linux is used to display a user’s UID (user ID), GID (group ID), and groups they belong to. This command is helpful in system administration, script writing, and security management to confirm user permissions and group memberships. It’s particularly useful when managing permissions and access controls on multi-user systems.

Syntax

The general syntax to use id is:

id [options] [username]
  • If no username is specified, id will display information about the current user.
  • If username is provided, id displays the identity of the specified user.

Options/Flags

  • -u, --user: Display only the UID.
  • -g, --group: Show only the GID (primary group ID).
  • -G, --groups: List all group IDs.
  • -n, --name: Display the name instead of the numeric ID; used with -u, -g, -G.
  • -r, --real: Print the real ID instead of the effective ID, with -u, -g, -G.
  • -z, --zero: Delimit entries with a NUL character instead of whitespace; mainly used in scripting for safer output parsing.

Examples

  1. Basic usage:

    id
    

    Displays detailed information of the current user including UID, GID, and groups.

  2. Display only the UID of the current user:

    id -u
    
  3. Display the group information of a specific user:

    id -Gn username
    

    Outputs a space-separated list of group names the user belongs to.

  4. Check the real and effective UID and GID of a user:

    id -r -u username
    id -r -g username
    

Common Issues

  • Misinterpretation of effective vs. real IDs: Users sometimes confuse the real ID with the effective ID. Real IDs are the true identifiers, while effective IDs are used for permission checks during operations.
  • Permission Denied: Avoid running id with options that require elevated permissions without proper access rights.

Integration

id can be combined with other commands for enhanced system management. For example:

  1. Script to check membership in a specific group:

    if id -Gn | grep -qw "sudo"; then
        echo "User has sudo privileges."
    else
        echo "User does not have sudo privileges."
    fi
    
  2. In combination with xargs for file ownership changes:

    find /path/to/dir -type f | xargs -I {} chown $(id -u):$(id -g) {}
    
  • whoami: Shows the username of the current effective user.
  • groups: Lists the groups a user is a member of.
  • usermod: Used for modifying user accounts.

For further exploration of user management commands and their usage, consult the official Linux documentation or access manual pages using the man command, such as man id.