Introduction
In the world of Unix-type operating systems, knowing the user's identity under which a process is being implemented is essential for the management of permits, the resolution of security problems and the automation of tasks. The Linux id command provides a quick and direct way to get that information, showing the User Identifier (UID), the Group Identifier (GID) and the list of additional groups to which the user belongs. In this article we will explore its operation, its most useful options and some practical examples that you can immediately apply to your terminal.
What is command id
The id command belongs to the GNU Coreutils basic utility set and is present in virtually all Linux distributions. Its main purpose is to show the identity of the user who invokes it, but you can also consult the information of any other user specified as an argument. When running without parameters, go print the UID, the GID and the additional groups of the current user, all in a legible line. In addition, through different options, it is possible to obtain only the UID, only the GID, the names instead of the numbers, or even the real and effective ID in environments with setuid processes. This flexibility makes it an essential tool for both system administrators and developers who need to validate permissions within their scripts.
Basic syntax
The simplest syntax of the id command is: id [options] [user]. If the user argument is omitted, the command acts on the user who is running the shell. The options change which information is displayed and in which format. Some of the most common are -u to show only the UID, -g to show only the GID, -G to list all the GID (including the primary and the supplementary), -n to show names instead of numbers when combined with -u, -g or -G, and -r to print the real ID instead of cash in cases of processes with high privileges. It is important to remember that, without options, id shows a combined output that includes both the UID and the GID and the list of groups.
Most commonly used options
- -u: shows only the numerical UID. Example:
id -u→ 1000. - -g: shows only the primary numerical GID. Example:
id -g→ 1000. - -G: list all GID (primary and supplementary) separated by space. Example:
id -G→ 1000 24 27 30. - - n: when combined with -u, -g or -G, it shows names instead of numbers. Examples:
id -un→ username,id -gn→ name of primary group,id -Gn→ list of group names. - -r: shows the real ID instead of the cash. Useful with setuid processes. Example:
id -r -u→ Real UID,id -u→ UID effective.
They can be combined, by placing first -n or -r and then the desired ID option.
Practical examples
- Full information of the current user:
id
Typical output: uid = 1000 (user) gid = 1000 (user) groups = 1000 (user), 24 (cdrom), 27 (sudo), 30 (dip), 46 (plugdev) - Only the numerical UID:
id -u
Output: 1000 - Only the UID with name:
id -un
Output: user - Only the primary numerical GID:
id -g
Output: 1000 - List of all numerical DIGs:
id -G
Output: 1000 24 27 30 46 - List of all GID names:
id -Gn
Output: user cdrom sudo dip plugdev - Real UID in a setuid process:Suppose you run a program with the bit setuid root; then:
id -u→ 0 (effective UID)id -r -u→ 1000 (real UID) - Consult another user:
id -u nombredeusuario
This returns the specified user's UID, provided you have permission to read your information.
These examples illustrate how to combine options to get exactly the data you need, either to debug permissions, configure sudo or write automation scripts.
Cases of use in scripts
In practice, the id command is often used within shell scripts to make decisions based on the user's identity. For example, a script that requires root privileges can start with:
if [ $(id -u) -ne 0 ]; then
echo 'Este script debe ejecutarse como root' >&2
exit 1
fi
This checks that the effective UID is zero before continuing. Another typical case is to get the primary group name to assign permissions to files created by the script:
GROUP=$(id -gn)
chown -R :$GROUP /ruta/al/directorio
In addition,
id -Gnallows to generate a list of groups to which the user belongs, useful to validate whether the user is authorized to access certain resources by group membership checks.Conclusion
The id command is a simple but powerful tool that provides essential information about the user's identity in a Linux system. From showing the basic UID and GID to listing all groups and differentiating between real and effective IDs, your options allow you to adapt the output to any administrative or development needs. Dominating your use will help you write safer scripts, diagnose permission problems and manage users with greater confidence in the command line.


