How to list User Groups on Ubuntu Linux with examples

This brief tutorial shows students and new users how to list groups on Ubuntu 18.04 | 16.04 Linux systems.

Listing user groups on Ubuntu Linux is essential for system administration tasks. It allows you to see which users can access specific resources and helps you manage permissions and access control for your system.

As a beginner, learning how to list user groups is an excellent introduction to Linux user management, and it will help you understand Linux file permissions and ownership.

Overall, understanding user groups is a fundamental part of Linux system administration and a skill that every Linux user should know.

Linux Groups:

There are two types of groups users can be assigned to. One is a primary, and the other is a second group that grants privileges to users to access specific resources.

Below is how a typical Linux user account is added and assigned group memberships:


User A user with an account must belong to one primary group. The user’s primary group is typically named after the user account name.
Primary Group — The primary group is created simultaneously when the user account is created, and the user is automatically added to it. The file created by the user automatically belongs to the user group.
Secondary Group — This group is not required and is only there to give users access to other resources they don’t already have access to. Users can belong to one or as many secondary groups as possible.


The primary user’s group is stored in the /etc/passwd file, and the supplementary groups, if any, are listed in the /etc/group file.

List User Groups using the group’s command

Now that you know the types of groups for users, you can use the groups command to find the groups to which a user belongs. Running the groups command without arguments will list all the groups to which the user belongs.

Example:

groups

Should output all the groups the account richard belongs to. The primary group is the first group with the same name as the user account name.

Ouput:
richard adm cdrom sudo dip plugdev lpadmin sambashare

To list all the groups a user belongs, add the username to the group’s command

example:

groups richard

This should output the same as above

Output
richard : richard adm cdrom sudo dip plugdev lpadmin sambashare

List User Groups using the id command

One can also use the id command to list group information about the specified user. It prints user and group information for the specified USER,

The command will show the username (uid), the user’s primary group (gid), and the user’s secondary groups (groups)

id richard

Should output the line below:

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

List Group Membership using the getent command

If you want to know a particular group’s members, use the getent command. This command gets entries from the administrative database.

To get a membership of the cdrom group, run the command below

getent group cdrom

This should output all the users who have access to the cdrom group.

Listing All Groups

To list the entire groups on Ubuntu, run the command below

less /etc/group

That should output all the groups on each line

root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,richard
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:richard
floppy:x:25:
tape:x:26:
sudo:x:27:richard
audio:x:29:pulse
dip:x:30:richard
.

That’s it

Congratulations! You have learned how to list groups on Ubuntu Linux.

Frequently Asked Questions

How do I list all user groups in Ubuntu?

You can list all user groups in Ubuntu by using the 'groups' command followed by the username. For example, 'groups richard' will show all groups that the user 'richard' belongs to.

What is the difference between primary and secondary groups in Linux?

In Linux, a primary group is the main group assigned to a user, typically created when the user account is established. Secondary groups are additional groups that provide users access to resources beyond their primary group.

How can I find out which groups a user belongs to?

You can find out which groups a user belongs to by using the 'id' command followed by the username. For example, 'id richard' will display the user's UID, primary GID, and all secondary groups.

What command can I use to see members of a specific group?

To see the members of a specific group, you can use the 'getent' command followed by 'group' and the group name. For example, 'getent group sudo' will list all users in the 'sudo' group.

Why is it important to manage user groups in Linux?

Managing user groups in Linux is crucial for controlling access to resources and permissions. It helps system administrators ensure that users have the appropriate access rights while maintaining system security.

Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *