7 methods to list user groups in Linux? [SOLVED] | GoLinuxCloud

Advertisement

In operating systems, applications add their own users and groups to the system. From an administrative point of view, this makes it easier for users. Adding users to the application group is the easiest way to edit privileges. As a matter of fact, systems such as LDAP and Active Directory are also built on this method.

There are many methods of listing groups in Linux. In some methods, group information is accessed from the user, while in some methods, users are accessed from group information. We will tell you some of the most used methods with examples.

 

Method-1: Using groups command

When you run the groups command without any parameters, it lists the group information of the user who opened the terminal:

foc@fedora:~$ groups

foc wheel

If you type a username after the group command, the groups belonging to that user are listed:

foc@fedora:~$ groups golinux

golinux : golinux

In this method, groups are listed with user information.

 

Method-2: Using id command

Like the group command, the id command, when executed without parameters, lists the active user’s groups. But this time group id are also displayed:

foc@fedora:~$ id

uid=1000(foc) gid=1000(foc) groups=1000(foc),10(wheel)

By typing the username after the id command, the groups belonging to that user are listed with their ids:

foc@fedora:~$ id golinux

uid=1001(golinux) gid=1001(golinux) groups=1001(golinux)

As the user’s group information increases, the information displayed on the screen may not be understood. With the parameters of the ID command, the output can be made more understandable. For example, to list all group ids and names:

Advertisement

foc@fedora:~$ id -Gn golinux 

golinux

You can get help from the --help page for all its parameters:

foc@fedora:~$ id --help
Usage: id [OPTION]... [USER]...
Print user and group information for each specified USER,
or (when USER omitted) for the current user.

  -a             ignore, for compatibility with other versions
  -Z, --context  print only the security context of the process
  -g, --group    print only the effective group ID
  -G, --groups   print all group IDs
  -n, --name     print a name instead of a number, for -ugG
  -r, --real     print the real ID instead of the effective ID, with -ugG
  -u, --user     print only the effective user ID

Again in this method, groups are listed with their user information.

 

Method-3: Using getent command

The getent command pulls information from the group database. If there is no central system such as LDAP, Active Directory, it will pull from the local database.

You can pull groups by typing group after getent command:

foc@fedora:~$ getent group
root:x:0:
bin:x:1:
...
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:foc
cdrom:x:11:
mail:x:12:

To list users in a group, you must type the group name:

foc@fedora:~$ getent group wheel

wheel:x:10:foc

To list all groups in the system without details:

foc@fedora:~$ getent group | cut -d: -f1

root bin disk lp mem kmem wheel cdrom mail

This method lists both groups and users in that group.

 

Method-4: Using /etc/group file

On Linux the group information is in the /etc/group file. If a user is added or removed from the group, this file changes.

When you view this file with file view commands like cat , it gives a complex output. To list group information, you can write it like this:

foc@fedora:~$ cut -d: -f1 /etc/group

root bin ... lp mem kmem wheel ... tape video ftp

For the total number of groups:

foc@fedora:~$ cat /etc/group | grep -c ""

82

Using awk command we can extract the group names from the /etc/group file using the colon (:) delimiter.

awk -F: 

'{ print $1 }'

/etc/group

 

Method-5: Using compgen command

Another command you can use to list groups in Linux is compgen. You can list the groups in the system with the -g parameter:

Advertisement

[foc@rocky9 ~]$ compgen -g

root bin wheel ftp lock audio users nobody foc

 

Method-6: Using lid command

This command displays information about the specified group, including the GID, group password (if any), and members.

# lid -g nagios

nagios(uid=1001) apache(uid=48) snmptt(uid=974)

 

Method-7: Using dscl command (On MacOS)

Using the dscl command on macOS. This command displays information about the specified group on macOS.

dscl . -read /Groups/groupname

 

Bonus Tip

If you want to list the groups of users logged into the system, you can use the following for loop:

[foc@rocky9 ~]$ for user in $(cat /etc/passwd | grep bash | awk -F: '{print $1}');do groups $user; done

root : root foc : foc wheel

Note: Bash was chosen as the default shell. If a different shell(zsh,sh etc) is used, it can be written after the grep command.

 

What is NEXT?

 

Summary

There is always an alternative on Linux. We have explained different ways to list groups in Linux for you. The commands and methods used may vary according to habits. You can use whichever method is faster and easier for you. Of course the choice is yours.

You can get help with the -h/–help parameter for each command. For more detailed information, you can also access the man page of the commands as in the example:

foc@fedora:~$ man id

NAME
       id - print real and effective user and group IDs

SYNOPSIS
       id [OPTION]... [USER]...
...

 

References

unix.stackexchange.com – How to list groups with gid in redhat?
stackoverflow.com – Is there a command to list all Unix group names?

 

Alternate Text Gọi ngay