id Command in Linux with Examples (Print real and effective user and group IDs)

Today you will learn How to use the id command in Linux. A very small command, but most useful.

id stands for Identity.

As you can understand by the name itself, you can use the id command to print User and Group information for the specified User, OR for the current User.

In short id command prints real and effective user and group IDs.

Let’s try to understand what is the real and effective user.

Real User:

As you know, an authorized user has to enter a valid username and password to login in to the Linux system whether it logs graphically or in command line mode.

The user can check who originally logged in to the system by running the logname command.

Like in my case I have logged into the system with a user named helpdesk.

~$ logname 
helpdesk

In this case, helpdesk is the real user.

Effective User:

During our login session, we may have to switch to another user to perform certain tasks.

For example, we may have to switch to the root user to perform some administrative tasks for which we use the su command and we can use the whoami command to verify.

~$ su - root
Password: 

~$ whoami
root

Let’s run the logname command now and see what the output is.

~$ su - root
Password: 

~$ whoami
root

~$ logname
helpdesk

As you can see the logname command is still displaying that we have logged the system from the helpdesk user.

Hence in this case root is the effective user.

I think now the concept of your real and effective user must have cleared.

Suggested Read:

Before discussing this topic further, let’s understand some technical terms.

In Linux by default, each user is assigned a unique numeric value called UID(User Identifier).

Similarly, each group is assigned a unique numeric value called GID (Group Identifier).

In this article, I will tell you how you can easily check the user IDs and group IDs, but you also need to know where all this information actually resides.

All this information is stored in the /etc/passwd file. You can display the contents of this file using the cat command.

~$ cat /etc/passwd
User and Group Information

Key features of id command:

  • Print User’s User ID(UID) and Group ID(GID)
  • Print only the effective User and Group ID
  • You can print User/Group names instead of ID numbers
  • Print all Group ID’s(Primary and Supplementary)
  • Display real ID instead of the effective ID’s
  • Print security context of the process
  • You can delimit output entries with NUL characters

First of all, let’s focus on some of the most important options that we can use with the id.

OptionsExplanation
-aIgnore, for compatibility with other versions.
-Z, --contextPrint only the security context of the process.
-g, --groupPrint only the effective group ID
-G, --groupsPrint all group IDs
-n, --namePrint a name instead of a number, for -ugG
-r, --realPrint the real ID instead of the effective ID, with -ugG
-u, --userPrint only the effective user ID
-z, --zeroDelimit entries with NUL characters, not whitespace
--helpDisplay help page of id Command
--versionCheck the version of id command

Syntax:

You must follow the syntax given below to use the id command.

id [OPTION]... [USER]

1. Print current user’s UID and GID

Running the id command without any options will print the current user details.

I logged into the system with a user named helpdesk. Now by running the id command we will get the following information:

  • UID : User ID – uid=1000(helpdesk)
  • GID : Group ID which is also called as Primary Group – gid=1000(helpdesk)
  • Supplementary group or Seconday group details – groups=1000(helpdesk),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare)
$ id
uid=1000(helpdesk) gid=1000(helpdesk) groups=1000(helpdesk),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare)
User Details

2. Print Specific user’s UID and GID

To display the details of a specific user, pass that user’s name as an argument in the id command.

In this example, I am printing the details of the user named ayush.

~$ id ayush
uid=1007(ayush) gid=1011(ayush) groups=1011(ayush)

3. Print only the effective User ID

To print, only the effective user ID (UID) of a user pass the -u option to id command. Let’s take some examples.

Type the following command to print the user ID of the user you are just logged in with.

~$ id -u
1000

If you want to print the User ID of a specific user then run the following command. Here I am printing the user id of a user named ayush.

~$ id -u ayush
1001

4. Print only the effective Group ID

To print, only the effective Group ID (GID) of a user pass the -g option to id command. Let’s take some examples.

Type the following command to print the Group ID of the user you are just logged in with.

~$ id -g
1000

If you want to print the Group ID of a specific user then run the following command. Here I am printing the Group ID of a user named ayush.

~$ id -g ayush
1011

Read Also:

5. Print the Username instead of a number(UID)

You can print the user’s name instead of the UID number. to do so pass the -n option to id command but the -n option has to be used in conjunction with one of these three options (-u, -g or -G).

~$ id -un
helpdesk

Note: You can get the same output using the whoami command.

~$ whoami 
helpdesk

6. Print the Group name instead of a number(GID)

You can print the group’s name instead of the GID number. to do so pass the -n option to id command but the -n option has to be used in conjunction with one of these three options (-u, -g or -G).

~$ id -gn
helpdesk

7. Print all group IDs

Print all group IDs (GID) who have a member named user1.

~$ id -G user1
1012 1004 1005 1006 1007 1008

Type the following command to print all group IDs of the current user.

~$ id -G
1000 4 24 27 30 46 116 126

Print all group names instead of GID numbers who have a member named user1.

~$ id -nG user1
user1 group1 group2 group3 group4 group5

Note: You can get the same output using the groups command.

$ groups
user1 group1 group2 group3 group4 group5

8. Print the real ID instead of the effective ID

To print real ID instead of the effective ID pass the -r option to id command but the -r option has to be used in conjunction with one of these three options (-u, -g, or -G).

Let’s take some examples:

# Print real UID :

~$ id -ur
1000

# Print real GID :

~$ id -gr
1000

# Print real GID's :

~$ id -Gr
1000 4 24 27 30 46 116 126

You can print the Names instead of the number IDs (UID and GID) by combining the -n option.

Let’s take some examples:

# Print real Username :

~$ id -unr
helpdesk

# Print real Group name :

~$ id -gnr
helpdesk

# Print real Group names :

~$ id -Gnr
helpdesk adm cdrom sudo dip plugdev lpadmin sambashare

9. Print security context of the process

To print only the security context of the process pass the -Z option to id command but remember that this only works on a SELinux-enabled kernel.

Otherwise, you will get the following error.

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

And if SELinux is enabled in your operating system, you will get the following output.

~$ id -Z
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

10. Delimit output entries with NUL characters

Usually, when we run the id command we get the output entries separated by Whitespace.

Output entries separated by Whitespace

You can delimit output entries with NUL characters, not whitespace by passing the -z option to id command.

~$ id -nGz user1
user1group1group2group3group4group5

11. Check the version of id command

Check the id command version using the following command.

~$ id --version
id (GNU coreutils) 8.28

12. Help/Manual page access

Use the following commands to access the Manual Page/Help Page of id command.

~$ id --help
~$ man id

You can visit at following websites to get more information on id command.

Conclusion

I hope you have learned something from this article.

I have tried my best to include all the features of id command in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

Leave a comment below.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment