The id command in Linux: show the current user’s identity

Introduction

In the world of Unix-like operating systems, knowing the identity of the user under which a process is running is essential for permission management, troubleshooting security issues, and automating tasks. The Linux id command provides a quick and direct way to obtain that information, displaying the User Identifier (UID), Group Identifier (GID), and the list of supplementary groups to which the user belongs. In this article we will explore how it works, its most useful options, and some practical examples you can apply immediately in your terminal.

What is the id command

The id command belongs to the GNU Coreutils basic utilities set and is present in practically all Linux distributions. Its main purpose is to display the identity of the user invoking it, but it can also query information for any other user specified as an argument. When executed without parameters, id prints the UID, GID, and supplementary groups of the current user, all on a readable line. Furthermore, via various options, it is possible to obtain only the UID, only the GID, names instead of numbers, or even the real and effective ID in environments with setuid processes. This flexibility makes it an indispensable 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 running the shell. Options modify what information is displayed and in what format. Some of the most common are -u to show only the UID, -g to show only the GID, -G to list all GIDs (including the primary and supplementary ones), -n to show names instead of numbers when combined with -u, -g or -G, and -r to print the real ID instead of the effective one in cases of processes with elevated privileges. It is important to remember that, without options, id displays a combined output that includes both the UID and the GID and the list of groups.

Most used options

  • -u: shows only the numeric UID. Example: id -u → 1000.
  • -g: shows only the numeric primary GID. Example: id -g → 1000.
  • -G: lists all GIDs (primary and supplementary) separated by spaces. Example: id -G → 1000 24 27 30.
  • -n: when combined with -u, -g or -G, shows names instead of numbers. Examples: id -un → username, id -gn → primary group name, id -Gn → list of group names.
  • -r: shows the real ID instead of the effective one. Useful with setuid processes. Example: id -r -u → real UID, id -u → effective UID.

They can be combined, placing -n or -r first and then the desired ID option.

Practical examples

  1. 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)
  2. Only the numeric UID: id -u
    Output: 1000
  3. Only the UID with name: id -un

    This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .