The Linux system consists of multiple users and groups; there might be a large number of users in your system that can be easily managed by adding them to a specific group.
After adding them, you can easily assign different permissions and policies to that specific group of users and remove them when they are not required.
A good example is sudo. If you are not part of the sudo group, you will not be able to perform any system level changes similar to that.
You might be part of many other groups that you can list using the groups command.
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
|Discussed Tools in this Article||id, getent, cat|
Syntax of the Groups Command
The groups command takes one argument asking for a username, as shown.
$ groups [USERNAME]
Although you can specify multiple usernames by using the space as a separator.
Listing All Groups to Which a Specified User Belongs
Specify the “username” with the groups command to list all the groups to which the specified user belongs.
$ groups linuxtldr linuxtldr : linuxtldr sudo admin newadmin
If you do not specify the “username“, the groups command will show the result for the currently logged-in user.
Display the Current User’s UID, GID, and Groups to Which They Belong
The id command is used to display information about the current user’s ID (UID), group ID (GID), and groups to which they belong.
It is followed by the type with an “=” sign, then the user or group ID, and the username.
$ id uid=1003(linuxtldr) gid=1004(linuxtldr) groups=1004(linuxtldr),27(sudo),1007(admin),1008(newadmin)
Getting a Username Without a User ID
-n” and “
-G” flags can be used with the id command to display only the name instead of a number.
$ id -nG linuxtldr sudo admin newadmin
Listing All Groups in Your System
Whenever you create a new group or record for existing groups is recorded at “/etc/group” file that you can read using the cat command.
$ cat /etc/group root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:syslog,jr tty:x:5: disk:x:6: lp:x:7: mail:x:8: news:x:9: uucp:x:10: man:x:12: proxy:x:13: kmem:x:15: dialout:x:20: fax:x:21: voice:x:22: cdrom:x:24:jr floppy:x:25: tape:x:26: sudo:x:27:linuxtldr audio:x:29:pulse dip:x:30:jr
Alternatively, you can use the getent command to get the same output as above.
$ getent group root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:syslog,jr tty:x:5: disk:x:6: lp:x:7: mail:x:8: news:x:9: uucp:x:10: man:x:12: proxy:x:13: kmem:x:15: dialout:x:20: fax:x:21: voice:x:22: cdrom:x:24:jr floppy:x:25: tape:x:26: sudo:x:27:linuxtldr audio:x:29:pulse dip:x:30:jr
That was the end of the article.
If you have any thoughts, feel free to exchange them in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.