Introduction
Before we discuss id command in Linux, let's briefly understand-What is id
Command ?
The id
command in Linux is used to retrieve information about the current user or specified user's identity and group memberships. It provides details such as User ID (UID), Group ID (GID), supplementary groups, and more. The id
command is helpful for checking user permissions and diagnosing access-related issues.
id
is a command-line tool that displays the current and valid user and group IDs.
In this tutorial, we will talk about the id
command. We will also address a few FAQs on id command in Linux.
Using the id Command
The id
command has the following syntax:
id [OPTIONS] [USERNAME]
The id
command displays information about the currently logged-in user if the username is not specified.
id
prints the real user ID (uid
), the user's real primary group ID (gid
), and the real IDs of the supplemental groups (groups
) the user belongs to when run without any options. Only when the effective user ID, group ID, and supplemental group IDs differ from the true ones are they printed.
id
uid=1000(vegastack) gid=1000(vegastack) groups=1000(vegastack),4(adm),27(sudo),998(docker)
id
additionally prints the user's security context (context
) if SELinux is enabled:
uid=1000(vegastack) gid=1000(vegastack) groups=1000(vegastack) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Pass the username or user ID as a parameter to the id
command to get information about another user:
id mark
Output
uid=1001(mark) gid=1001(mark) groups=1001(mark),998(docker)
If the system already has a user with the same name as the supplied ID, the name look-up takes precedence. To avoid the ID being misinterpreted as a name, prefix it with the +
sign when using it as an argument.
If you have a user with the name 1010
and another with the ID 1010
, for example, typing id 1010
will reveal information about the user with the name 1010
. Type id +1010
to get information about the user with ID 1010
.
id
Command Options
The id
command has a number of arguments that allow you to display only certain data. When utilizing id
in shell scripts, this is useful.
Use the -u
(--user
) option to print only the effective user ID:
id -u
Output
1000
With the -g
(--group
) option, id
will only print the effective group ID:
id -g
Output
1000 4 27 998
To print the effective IDs of all groups to which the user belongs, use the -G
(--groups
) option:
id -G
Output
1000
Use the -n
, --name
option to output names instead of numbers. This option can only be used in conjunction with the -u
, -g
, and -G
options.
id -un
Output
vegastack
Running the id
command with the -un
parameters produces the same results as running whoami
, and running id
-Gn
produces the same results as running groups.
The -r
, (--real
) option can be combined with the -u
, -g
, and -G
options to output real numbers rather than effective values:
id -ur
Use the -Z
(--context
) option to print only the process's security context, which is normally the user's security context:
id -Z
Output
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
id
sends an error message if SELinux is disabled:
id: --context (-Z) works only on an SELinux-enabled kernel
The -z
(--zero
) option instructs id
to use the NULL character instead of whitespace to delimit the output items:
id -znG
Output
vegastackadmsudodocker
When piping the output to a command that can parse newlines, this can be handy.
FAQs for id Command in Linux
What is the purpose of the id
command?
The id
command is used to display user and group identity information, including User ID (UID), Group ID (GID), and supplementary group memberships.
How do I use the id
command?
To use the id
command, open a terminal and type id
. It will display information about the current user or the user specified as an argument.
What information does the id
command display?
The id
command displays the User ID (UID), Group ID (GID), and supplementary group IDs of the current user or a specified user.
What is User ID (UID) in the id
command output?
User ID (UID) in the id
command output represents the unique identifier assigned to each user by the system. It is used to differentiate between different users.
What is Group ID (GID) in the id
command output?
Group ID (GID) in the id
command output represents the unique identifier assigned to each group by the system. It is used to associate multiple users to a specific group.
How can I determine the current user's UID and GID using the id
command?
By running the id
command without any arguments, it displays the User ID (UID) and Group ID (GID) of the current logged-in user.
Where can I find more information about the id
command?
You can refer to the id
command's manual page by typing man id
in the terminal. Additionally, online resources and Linux documentation provide further explanations and usage examples of the id
command.
Conclusion
If no username or ID is specified as an argument, the id
command publishes information about the currently logged-in user.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.