Introduction to the Linux id Command
In this tutorial, we'll delve into the Linux id
command, a vital tool for any systemadmin. This utility provides essential information about users on a Linux system. We will explore its function and applications, focusing on identifying user and group details, and customizing its output.
The id
command reveals a user's User ID (UID), Group ID (GID), and supplementary group memberships. It can retrieve this information for the current user or any other user. Furthermore, the id
command can display the effective UID and GID, which are crucial for understanding permission handling during command execution.
Understanding the Functionality and Application of the id Command
This section focuses on the purpose and practical usage of the id
command within a Linux environment. The id
command is an invaluable tool for system administrators, offering quick access to user-related information.
The fundamental syntax for the id
command is:
$ id
uid=1000(labex) gid=1000(labex) groups=1000(labex),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare)
This output presents the UID, GID, and the supplementary groups associated with the current user.
To obtain the same information for a different user, simply include their username as an argument:
$ id alice
uid=1001(alice) gid=1001(alice) groups=1001(alice),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare)
This command displays the user and group details for the user alice
.
The id
command is also capable of displaying the effective UID and GID, which govern permission checks during command execution. The -e
option is used for this purpose:
$ id -e
uid=1000(labex) gid=1000(labex)
Example output:
uid=1000(labex) gid=1000(labex) groups=1000(labex),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare)
Identifying User and Group Information with the id Command
In this part, we will learn how to leverage the id
command to pinpoint user and group details for both the current user and other users within the system.
Firstly, let's confirm the user and group information for the current user:
$ id
uid=1000(labex) gid=1000(labex) groups=1000(labex),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare)
This output showcases the UID, GID, and the supplementary groups associated with the current user, labex
.
To retrieve user and group details for other users, specify their username. For example, to get the information for user alice
:
$ id alice
uid=1001(alice) gid=1001(alice) groups=1001(alice),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare)
This command will present the UID, GID, and supplementary groups for the user alice
.
Furthermore, the -u
and -g
options can isolate the UID and GID, respectively:
$ id -u
1000
$ id -g
1000
Example output:
uid=1000(labex) gid=1000(labex) groups=1000(labex),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare)
Exploring Advanced Options and Customizations of the id Command
In this final section, we'll explore the more advanced options and customization possibilities offered by the id
command. These features enable system admins to tailor the output to meet specific needs.
A particularly useful option is the -n
flag, which displays names instead of numeric IDs. For example:
$ id -nu
labex
$ id -ng
labex
This will show the username and group name instead of the numeric IDs. This is especially helpful when working with scripts or configurations that require human-readable output.
To view all available options for the id
command, use the --help
option:
$ id --help
Usage: id [OPTION]... [USER]
Print user and group information for the specified USER,
or (when USER omitted) for the current user.
-a print all information in a readable format
-Z, --context print only the security context of the process
-g, --group print only the effective group ID
-G, --groups print all group IDs
-n, --name print a name instead of a number, for -ugG
-r, --real print the real ID instead of the effective ID, with -ugG
-u, --user print only the effective user ID
-z, --zero delimit entries with NUL character, not whitespace;
not permitted in default format
--help display this help and exit
--version output version information and exit
Examples:
id
id username
id -a
id -u
id -g
This output provides a comprehensive overview of all available options for customizing the id
command's output.
Combining multiple options allows for highly specific information retrieval. For example, to display the username and group name for the current user:
$ id -un
labex
$ id -gn
labex
Example output:
$ id -un
labex
$ id -gn
labex
Summary
This tutorial has equipped you with a thorough understanding of the id
command in Linux. You've learned how to use it to display information about users, including the current user. You can now retrieve the UID, GID, and supplementary groups a user belongs to. Furthermore, you understand the significance of the -e
option for displaying effective UIDs and GIDs, crucial for permission management. You can also target specific users by providing their usernames as arguments to the command. Mastering the id
command is a valuable asset for any Linux systemadmin.