In a Linux operating system, every user account is assigned a unique numerical User ID (UID), and every group is assigned a Group ID (GID). When troubleshooting file permission errors or auditing system security, you often need to verify exactly what numerical IDs and group memberships are currently assigned to an account. Instead of manually parsing the complex /etc/passwd and /etc/group text files, you can use the id command to instantly print a user’s complete identity profile.
How to Use the id Command
To view the identity information for your own currently logged-in account, simply run the command with no arguments:
id
The terminal will instantly output a string of data that looks something like this:
uid=1000(charlie) gid=1000(charlie) groups=1000(charlie),27(sudo),46(plugdev)
This output tells you exactly who the operating system thinks you are.
- uid: This is your User ID number, followed by your username in parentheses. A standard user typically has a UID of 1000 or higher. The
rootadministrator always has a UID of 0. - gid: This is your primary Group ID. When you create a new file, it will automatically be assigned this group ownership.
- groups: This is a comma-separated list of every secondary group you belong to. In the example above, the user belongs to the
sudogroup, meaning they have administrative execution privileges.
How to Check Other Users
If you are a system administrator and you need to check the group memberships of a different employee, you do not need to log into their account. You can simply pass their username as an argument to the id command.
id sarah
The terminal will fetch and display Sarah’s UID, GID, and a complete list of all her group memberships. This is incredibly useful for verifying that you successfully added a user to a specific restricted group before you grant them access to a sensitive directory.
Extracting Specific IDs for Scripts
While the default output is great for human reading, it is difficult to parse in an automated bash script. If your script needs to verify that the person running it is the root user (UID 0), you can use the -u flag to strip away all the text and output only the raw User ID number.
id -u
If the user is root, this command will simply output 0. If they are a standard user, it will output a number like 1000. You can easily capture this single number in a bash variable and use an if statement to terminate the script if the user does not have the necessary privileges.