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]
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.
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.
Similar Readings
- The “time” Command in Linux [4 Practical Examples]
- The “tty” Command in Linux [4 Practical Examples]
- The “uptime” Command in Linux [5 Practical Examples]
- The “vmstat” Command in Linux [6 Practical Examples]
- The “uname” Command in Linux [11 Practical Examples]
- The “apt-get” Command in Linux [10 Practical Examples]
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”.
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”.
Similar Readings
- The “free” Command in Linux [8 Practical Examples]
- The “getent” Command in Linux [11 Practical Examples]
- The “groupadd” Command in Linux [7 Practical Examples]
- The “addgroup” Command in Linux [7 Practical Examples]
- The “groups” Command in Linux [6 Practical Examples]
- The “firewall-cmd” Command in Linux [7 Practical Examples]
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”.
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”.
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.
Similar Readings
- The “groupmod” Command in Linux [5+ Practical Examples]
- The “passwd” Command in Linux [7 Practical Examples]
- The “service” Command in Linux [6 Practical Examples]
- The “sestatus” Command in Linux [4 Practical Examples]
- The “shutdown” Command in Linux [7 Practical Examples]
- The “finger” Command in Linux [6 Practical Examples]
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.
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.
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.
Similar Readings
- The “dmesg” Command in Linux [7+ Practical Examples]
- The “install” Command in Linux [6+ Practical Examples]
- The “reboot” Command in Linux [3 Practical Examples]
- The “sync” Command in Linux [8 Practical Examples]
- The “chage” Command in Linux [7 Practical Examples]
- The “env ” Command in Linux [9 Practical Examples]
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
➌ 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.
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
- The “sudo” Command in Linux [8 Practical Examples]
- The “df” Command in Linux [11 Practical Examples]
- The “apt” Command in Linux [13+ Practical Examples]
- The “top” Command in Linux [8 Practical Examples]
- The “htop” Command in Linux [7 Practical Examples]
- The “enable” Command in Linux [6 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners