The “id” Command in Linux [7+ Practical Examples]

The basic use of the id command in Linux includes finding out information about users’ IDs and names. Advanced options include printing the security context of a process. In this article, you will be able to learn about the basic usage of the id command with practical examples.

A. Description

The id command in Linux displays current user and group information according to the application of the command. You can modify the usage by passing a specific username or options.

B. Syntax

The syntax of the id command is described below. It contains option/s or username after the command itself.

id [OPTION]… [USER]
Note: In the above syntax OPTION enclosed by a square bracket and followed by 3 dots represents that multiple options can be utilized simultaneously.

C. Options

The id command in Linux takes a few numbers of options. Here I am describing the most useful options. You can find more on the man page by typing the following command.

man id

Useful Options

  • -g/–group: Display only the effective group ID.
  • -G/–groups: Display all the group IDs of the user.
  • -r/–real: Display real IDs.
  • -n/–name: Display names.
  • -u/–user: Display only the effective user ID.
  • -z/–zero: Delimit entries with the NULL character.
  • -Z/–context: Display the security context of the process.
Note: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of the “id” Command in Linux

There are various uses of the id command in Linux. You can view user information as well as security contexts. Here are some practical examples of the id command in Linux for your frequent use.

Example 1: Displaying Current User Information Using the “id” Command in Linux

You can use the id command without any argument. It will print out the UID and GIDs of the current user along with names. In this example, I will show you the UID and GIDs of my active user “anonnya”. To do the same you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

id

➌ Now, press the ENTER button.

Output >

In this image, you can see that I have printed out the active user’s information (UID and GID) using the id command in Linux.Showing active user’s information (UID and GID) using the id command in Linux


Similar Readings


Example 2: Finding a Specific User’s Effective ID Using the “id” Command in Linux

Using the id command in Linux with the option -u you can print out a user’s effective UID. In this example, I will display the effective UID of the current user “anonnya”. You can do the same by following the steps below.

Steps to Follow >

➊ At first, launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

id -u anonnya

➌ Press the ENTER button.

Output >

In the following image, you can see that I have printed the effective UID of the user “anonnya”.Dispalying specific user ID using the id command in Linux

Example 3: Finding a Specific User’s Effective GID Using the “id” Command in Linux

Using the id command with the option -g you can print out a user’s effective GID. In this example, I will display the effective GID of the current user group “anonnya”. You can do so by following the steps below.

Steps to Follow >

➊ At first go to the Ubuntu Terminal.

➋ Type the following command in the command prompt:

id -g anonnya

➌ Hit the ENTER button.

Output >

In the image below, you can see that I have printed the effective GID of the group “anonnya”.Displaying specific group ID using the id command in Linux.


Similar Readings


Example 4: Finding Real ID Using the “id” Command in Linux

You can find a specific user’s real ID using the id command with the option -r. You will be able to apply this option for displaying both user and group IDs. Follow the examples below to learn about the different applications.

Case A: Displaying a Specific Use’s Real ID

Follow the steps below to print a specific user’s real ID.

Steps to Follow >

➊ At first go to the Ubuntu Terminal.

➋ Type the following command in the command prompt:

id -r -u anonnya

➌ Hit the ENTER button.

Output >

In this image, you can see that I have printed the real UID of the user “anonnya”.Displaying users real ID using the id command in Linux.

Case B: Displaying a Specific Group’s Real ID

Follow the steps below to print a specific group’s real ID.

Steps to Follow >

➊ At first go to the Ubuntu Terminal.

➋ Type the following command in the command prompt:

id -r -g anonnya

➌ Hit the ENTER button.

Output >

In this snapshot, you can see that I have printed the real GID of the group “anonnya”.Displaying groups real ID using the id command in Linux.

Note: While working in Linux distribution you may temporarily need to use another ID apart from your current user ID. For example, sometimes you run commands as the root user. The root is not a real ID. In fact, it is an effective ID that gives you permission to perform changes on your system. On the other hand, the real ID is the one under which you own the process.

Example 5: Displaying All GIDs Belonging to the Current User Using the “id” Command in Linux

You can display all the group IDs of the current user using the id command in Linux. To do so you will have to use option -G. In this example, I will show you all the GIDs of my active user “anonnya”. You can follow the steps below to perform a similar task.

Steps to Follow >

➊ Open the Ubuntu Terminal.

➋ Type the following command in the command line:

id -G

➌ Press the ENTER button.

Output >

In the given image, you can see that I have displayed all the group IDs of the current user using the id command in Linux.Sowing all available group IDs using the id command in Linux.


Similar Readings


Example 6: Displaying Only Names Using the “id” Command in Linux

Using the id command you can find out the names of the users and groups. Typing the command with the option -n results in names. You can find the name of a group or a user or all the groups according to other associated options. Below are the cases of using the -n option.

Case A: Displaying Only Current Username

Go through the given process to print the name of the current user.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

id -nu

➌ Now, press the ENTER button.

Output >

In this 1st case, you can see that I have displayed the current user name.Displaying current user name using the id command in Linux.

Case B: Displaying Current Group Name

Follow the given process to print the name of the current group of the current user.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

id -ng

➌ Now, press the ENTER button.

Output >

In this 2nd case, you can see that I have displayed the current group name.Displaying current group name using the id command in Linux.

Case C: Displaying All Group Names

You can print all the group names by following the given steps.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

id -nG

➌ Now, press the ENTER button

Output >

In this 3rd case, you can see that I have printed all the available group names.Displaying all group names using the id command in Linux.


Similar Readings


Example 7: Delimiterizing Entries with Null Character Using the “id” Command in Linux

You can delimit the output with a NULL character using the id command with the option -z. In this example, I will print all the available group names and use NULL as a separator. Using a NULL character will merge the names.

Steps to Follow >

➊ At first launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

id -nzG
EXPLANATION
Here, the -z option is combined with the -n and -G options. The -G returns all the group users and -n extracts only the names of the group user.

➌ Press the ENTER button.

Output >

In the following image, you can see that I have printed all the group names and there are NULL characters between each name.Using NULL character as delimiter with entries using the id command in Linux.

Conclusion

In this article, I have tried to show you the frequent uses of the id command in Linux. You can always combine one or more options demonstrated here to get a full experience. I hope these practical examples will help you through your journey with the LINUX distribution.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment