Ubuntu like any operating system consists of users. On Linux-based Operating Systems users are parts of a group that segregates certain permissions to do certain things. There may be cases where you would want to list a user’s groups in Ubuntu.
Here is a breakdown of the users and groups in Ubuntu.
- User: A user who has an account must belong to one primary group. Usually, the user’s primary group is also named after the user account name.
- Primary Group: This group is created at the same time the user account is created and the user is automatically added to it. By default, files created by the user automatically belong to the user group.
- Secondary Group: These are other groups that are there to give users access to other resources that they don’t already have access to. A user can belong to zero or more secondary groups as needed.
Generally, the primary user’s group is stored in the /etc/passwd file while the secondary groups, if any, are listed in the /etc/group file.
We can get started by listing all the groups the current user belongs to using the following command:
This should output all the groups that the user belongs to.
john sudo www-data lxd
You can list a specific user’s groups in Ubuntu by specifying the user as shown here:
$ groups john
Here is the Output:
john : john sudo www-data lxd
List User’s Groups Ubuntu Using id Command
You can get more detailed group information about a specific user using the id command. The id command will show the username (uid), the user’s primary group (gid), and the user’s secondary groups (groups).
$ id john
Here is the expected output.
uid=1000(john) gid=1000(john) groups=1000(richard),27(sudo),33(www-data),110(lxd)
You can also list a specific group’s membership using the following command:
$ getent group www-data
Here is the expected output
Listing All Groups
You can list all the groups on Ubuntu using the following command:
$ less /etc/group
This would be the expected output:
Output root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:syslog 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: floppy:x:25: tape:x:26: sudo:x:27:john audio:x:29: dip:x:30: www-data:x:33:john,user2,user3,user4 ...
That’s all for now, you should now know how to list a user’s groups on Ubuntu 20.04, 18.04 and older versions.