id Command in Linux
The id command in Linux displays the system identity of a specific user. It also displays the user and group name along with their IDs. If the user is not specified, the id command will display the details of the current user. It is a handy tool for system administrators to retrieve user information.
Table of Contents
Here is a comprehensive guide to the options available with the id command −
Syntax of id Command
The syntax of the Linux id command is as follows −
id [options] [username]
The [options] field is used to specify the various id command options. The [username] field is used to specify the name of the user whose IDs need to be displayed.
Options of id Command
The options for the id command are listed below −
Flags | Options | Description |
---|---|---|
-a | It is used for compatibility with other Unix-like system versions (ignored in Linux) | |
-Z | --context | To display the security context of the current process |
-g | --group | To display the group ID |
-G | --groups | To display all group IDs |
-n | --name | To display the name corresponding to the ID instead of numerical value (use with -u, -g, and -G) |
-r | --real | To display the real ID instead of effective ID when used with -u, -g, and -G |
-u | --user | To display the effective user ID |
-z | --zero | To use null (\0) instead of whitespace to delimit the entries (for scripting) |
--help | To display the help about command | |
--version | To display the version of the command |
The real ID identifies the user or group who launched the process, essentially tracking who owns or started it. However, the effective ID determines what the process is allowed to do. If a process's effective ID is different from the real ID, it means the process can act with the permissions of another user, for example, using the setuid to run a command with root privileges.
Examples of id Command in Linux
This section demonstrates the usage of the id command in Linux with examples −
Displaying the User and Group IDs
To print the user and group IDs of the current user, use the id command without any option −
id

Displaying the User and Group IDs of a Specific User
To display the user and group ID of a specific user, use the id command with the userâs name −
id alex

Displaying the Security Context
A security context refers to the security attributes assigned to processes, files, or users in systems like SELinux (Security-Enhanced Linux) and AppArmor. It helps the system control what actions can be performed, ensuring that only allowed operations are carried out to protect the system.
To display the security context of the current process, use the -Z or --context option with the id command −
id -Z

Displaying the Effective Group IDs
To display the effective group ID of the current user, use the -g or --group option with the id command −
id -g

To display the effective group ID of the specific user, use the id command in the following manner −
id -g alex

Displaying All the Group IDs
Use the -G or --groups option to display all the group IDs to which the current or specific user belongs. For the current user, use the following command −
id -G
For a specific user, use −
id -G alex

Displaying Names Instead of Numeric IDs
To display the names instead of numeric IDs, use the -n or --name option. It will only be used with -u, -g, and -G options.
To display the username of the current user, use the following command −
id -un
To display the group name, use −
id -gn
To display all the groups the current user belongs, use −
id -Gn

For a specific user, use the userâs name −
id -un alex id -gn alex id -Gn alex
Displaying the Real IDs Instead of Effective IDs
By default, the id command displays the effective IDs. To display the real user and real group ID, use the -r or --real option. It is also used with -u, -g, and -G options.
To display the real ID of the current user, use the following command −
id -ur
To display the real group ID, use the command given below −
id -gr
To display the real group IDs, use the command given below −
id -Gr

For a specific user, use the userâs name −
id -ur alex id -gr alex id -Gr alex
Displaying only the Effective User ID
To display the effective user ID, use the -u or --user option. For the current user, use the following command −
id -u

To display the effective user ID of a specific user, use −
id -u alex

Delimiting the Output with NULL
To delimit the output with NULL, use the -z or --zero option. It is also used with -u, -g and -G options.
For the current user, use the following commands −
id -uz id -gz id -Gu

For a specific user, use the id command in the following way −
id -uz alex id -gz alex id -Gu alex
Conclusion
The id command in Linux is used to display the userâs ID, group ID, and all the group IDs the user belongs. It is a powerful user-management tool for Linux system administrators. It is available by default in all modern Linux distributions.
In this tutorial, we explained the id command, its syntax, options, and usage in Linux with examples.