How to Use the id Command in Linux

How to Use the id Command in Linux

In this tutorial, we will show you how to use the id command in Linux and other Unix-based systems.

One of the most useful commands is Linux is the id command. This command allows you to find the user ID, or UID and group ID or GUID of a specific username in the Linux system.

Knowing the IDs of a user can help you diagnose problems and find processes access various resources in the system. We can also use the IDs to determine in which group a user belongs.

id Command Syntax

The command follows are relatively simple and straightforward syntax. Luckily, you can use the id without any arguments.

id [OPTION] [USER]

The command accepts two main arguments as shown:

  • OPTION - specifies the command option to alter how the command behaves.
  • USER - specifies the username whose UID and GID you wish to determine. Luckily, you can specify a single or multiple usernames.

id Command Options

The following are the accepted command options:

     -A      Display the process audit user ID and other process audit
             properties, which requires privilege.

     -F      Display the full name of the user.

     -G      Display the different group IDs (effective, real and supplementary)
             as white-space separated numbers, in no particular order.

     -P      Display the id as a password file entry.

     -a      Ignored for compatibility with other id implementations.

     -g      Display the effective group ID as a number.

     -n      Display the name of the user or group ID for the -G, -g and -u
             options instead of the number. 

     -p      Make the output human-readable.  

     -r      Display the real ID for the -g and -u options instead of the
             effective ID.

     -u      Display the effective user ID as a number.
     --version  Display command version

Basic Command Usage

As stated, the id command is easy to use. If you run the id command without any arguments, it will display the information about the current user.

For example:

id

Running the command should return information as shown:

uid=1000(debian) gid=1000(debian) groups=1000(debian),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),109(netdev),113(scanner),116(lpadmin)

Among the displayed information includes the UID, GID and other supplementary information such as the group names.

Keep in mind that if the system has SELinux Enabled, it will display security context.

Get Group ID using the id command.

The simplest way to fetch the group ID of specific user with the id command is using the -g parameter.

An example is as shown:

id -g

Output:

1000

Get All Groups using the id command

We can fetch all the groups attached to a specific user using the -G or --groups option.

Example:

id -G

Output:

1000 24 25 27 29 30 44 46 109 113 116

To show the group name instead of the ID, we can use the -Gn option as:

id -Gn

Output:

debian cdrom floppy sudo audio dip video plugdev netdev scanner lpadmin

You can also use the -gn to show the group of user.

id -gn

Output:

debian

Showing Real Group ID

By default, the id command displays the effective ID instead of the real ID. To display the real ID, we can use the -r or --real option.

Examples:

id -gr

Output:

1000

Show User ID

We can print the effective User ID with the -u option:

id -u

Output:

1000

You can show the usernames instead of the User ID with the -n flag.

id -un 0

Output:

root

Show Security Context

We can also show the security context of a specific process using the -Z options.

id -Z

Output:

id: --context (-Z) works only on an SELinux-enabled kernel

Conclusion

In this article, we learned how to use the id command to get the user information about a given user such as the username, User ID and Group ID.

If you enjoyed this tutorial, feel free to share and leave us a comment below.

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.