How to use id command in Linux?

The id command in Linux is used to print the real and effective user and group ids. By using the Id command you can display the given information.

  • The real user-id of current or a specific user
  • Display the user id and all the groups associated with it
  • List out all groups a user belongs to
  • You can display the security context of the current user if SELinux is enabled

In this article, I will discuss the usage of the id command along with some examples.

The syntax of the id command

The syntax of how to use the id command in Linux is given below.

id [Options] [Username]

Where you can use the following options with the id command.

-g, --group – Print only the effective group ids
-G, --groups – Display all group ids
-n, --name – Print name instead of a number, for -ugG
-r, --real – Display the real id instead of the effective id
-u,--user – Print only the effective user-id
-z, --zero – This will delimit entries with NUL characters, not whitespace; not permitted in the default format
--help – Use to display the help
--version – This will print the version of the id utility and exit

For more information, you could visit the id command’s man page.

Usage of the id command in Linux

Some examples of how to use the Linux id command for different purposes are given below.

Using id command without any options

When you use the id command without any options or arguments it will print the real user ID (uid), the user’s real primary group ID (gid), and real IDs of the supplemental groups (groups)  the user belongs to, of the current user.

id

You can see the output in the given image.

Display information of another user

To display the information of another user that includes real user id, real primary group id, supplementary group ids, etc you can use the given command.

id username

For example –

id root

You can see the output in the given image.

Print only user id of a specific user

Let’s say we want to print only the real user id of the root user on a system then use the id command with the option -u.

id -u root

The output will look like this-

Print only primary group id of a specific user

Similarly, if you want to print the primary group id of root on a system then use the id command with option -g.

id -g root

This will display the given output.

Display all the groups a user belongs to

By using the option -G with the id command, you can display all the groups a user belongs to.

id -G lalit

This will display only group ids.

To display the name instead of numbers using –

id -nG lalit

Now the output will look like this:

For more information, you can use the given command in your terminal.

man id

Conclusion

Ok, now you know how to use the id command in Linux. Now if you have a query then write us in the comments below.

Leave a Comment