Monday, January 20, 2025
HomeProgrammingWhat Is Linux id Command?

What Is Linux id Command?

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

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

  1. -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
    
  2. -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
    
  3. -G (all group IDs): Display all group IDs (GIDs) the user belongs to.
    id -G
    

    Example:

    1000 27 24
    
  4. -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
    
  5. -a: Display all available information in a more readable format.
    id -a
    
  6. -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

  1. Show information for a specific user:
    id username
    

    Example output:

    uid=1001(username) gid=1001(username) groups=1001(username),27(sudo)
    
  2. Show only the UID of the current user:
    id -u
    

    Example output:

    1000
    
  3. Show the list of all groups a user belongs to:
    id -G
    

    Example output:

    1000 27 24
    
  4. Show the user name instead of UID:
    id -un
    

    Example output:

    username
    
  5. Show the group name instead of GID:
    id -gn
    

    Example output:

    username
    

Common Use Cases

  1. Checking User Information:
    Use the id 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.
  2. Determining Group Membership:
    The id -G option is particularly useful to see which groups a user belongs to, which is often used for troubleshooting permissions or verifying group access.
  3. Automation and Scripts:
    In bash scripts or automation, you can use id to check whether the user belongs to a specific group or to extract a user’s UID/GID for further processing.
See also  Java Database Connectivity (JDBC) in 5 Steps

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.

RELATED ARTICLES

Banking Application in Java

Java PrintWriter Class

What Is CSS Hover?

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x