DevOps

How to get Username and Group in Linux

April 6, 2023

Every user in Linux has a username and a group. These are used to identify and manage users and their permissions. In this quick guide, we will learn how to get your personal username and group, as well as the username and group of a specific user.

Personal Username and Group

To get the username and group of the currently logged in user, you can run the following command:

id

The output will look like this:

uid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare)

Here is the explanation of the output:

  • uid=1000(ubuntu) — The user ID i.e. 1000 and the username i.e. ubuntu. The user ID is a unique number that identifies the user and the username is the name of the user.
  • gid=1000(ubuntu) — The group ID i.e. 1000 and the group name i.e. ubuntu.
  • groups=1000(ubuntu),4(adm).. — The supplementary groups of the user. The supplementary groups are the groups that the user is also a member of.

You can also use different options to select the output format. For example, to get the username only, you can run:

id -un # ubuntu

To get the group only, you can run:

id -gn # ubuntu

You can also use the id command to get the details of a specific user.

Details of a Specific User

To get the username and group of a specific user, you can run the following command:

id <options> <username>

For example to get the group name of a user named root, you can run:

id root

To only get the username and the group name, you can run:

id -un root # root
id -gn root # wheel

Conclusion

In this quick guide, we learned how to get the username and group of the currently logged in user and a specific user. We also learned how to use the id command to get the username and group of a user.

© All rights reserved — cs.fyi