How to use the command 'id' (with examples)
The ‘id’ command is a Linux command that is used to display the current user and group identity. It can also be used to display the identity of any specific user.
Use case 1: Display current user’s ID (UID), group ID (GID) and groups to which they belong
Code:
id
Motivation: This use case is helpful when you want to know the current user’s identity, including their user ID, group ID, and the groups to which they belong. It is useful for system administrators and developers who need to verify the current user’s identity for troubleshooting or system administration tasks.
Explanation: The ‘id’ command without any options or arguments displays the current user’s identity. It lists the user’s UID (User ID), GID (Group ID), and the groups to which the user belongs.
Example output:
uid=1000(username) gid=1000(username) groups=1000(username),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare)
Use case 2: Display the current user identity as a number
Code:
id -u
Motivation: This use case is useful when you only need to retrieve the current user’s user ID without any extra information. It can be helpful for scripting purposes or when you need to pass the user ID as input to another command or script.
Explanation: The ‘-u’ option is used with the ‘id’ command to display the user ID (UID) of the current user. It prints only the numerical value of the user ID, without any additional information.
Example output:
1000
Use case 3: Display the current group identity as a number
Code:
id -g
Motivation: This use case is helpful when you want to retrieve the group ID (GID) of the current user without any other details. It can be useful for scripting purposes or when you need to pass the group ID as input to another command or script.
Explanation: The ‘-g’ option is used with the ‘id’ command to display the group ID (GID) of the current user. It prints only the numerical value of the group ID, without any additional information.
Example output:
1000
Use case 4: Display an arbitrary user’s ID (UID), group ID (GID) and groups to which they belong
Code:
id username
Motivation: This use case is useful when you want to retrieve the identity of a specific user, including their user ID, group ID, and the groups to which they belong. It is beneficial for system administrators who need to verify the identity of a specific user for troubleshooting or system administration tasks.
Explanation: By providing the username as an argument to the ‘id’ command, it displays the identity of the specified user. It lists the user’s UID (User ID), GID (Group ID), and the groups to which the user belongs.
Example output:
uid=1001(otheruser) gid=1001(otheruser) groups=1001(otheruser),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare)
Conclusion:
The ‘id’ command is a powerful tool for retrieving user and group identity information in Linux. It provides various options to display only specific details, such as user ID or group ID, making it useful for scripting purposes. Whether you need to check your own identity or verify the identity of another user, the ‘id’ command has you covered.