How to List All Users or User Groups in Linux

How to List All User Groups in Linux

Knowing how to list all user groups on Linux is a significant stride towards sustaining ownership and permissions. Since you can use several commands to list user groups, the command you choose depends on the depth of details you want to see.

For example, use the compgen command if you are only interested in names. On the other hand, you can customize the getent command to view extensive information about user groups.

This tutorial explains user management, listing users and their groups using several commands.

These commands should apply to any Linux distribution, including Ubuntu, CentOS, Debian, Linux Mint, and others.

How Linux Manages Users

First, you should understand the basics of user management, such as creation, privileges, and deletion.

Creation

You can create a user using either useradd command

sudo useradd <username>

For example:

sudo useradd edxd

OR

adduser command to collect more information about the user, such as password, room number, and phone number.

sudo adduser <username>

For example:

sudo adduser edxd

Adding a user to the system appends the user details to the /etc/passwd file.

A user that can log into the system is referred to as a normal user. Otherwise, the created user is a system user whose accounts are for automation tasks.

Supply the -r option to create a systems user.

sudo useradd -r <username>

For example:

sudo useradd -r sysuseredxd

Privileges

Another point worth remembering is Linux creates a user when you first install it. That user is a superuser and has all the privileges on systems management. The user is often referred to as the root or superuser and is assigned a special directory and group called the root.

Other users created after that are assigned the home directory from where they can store personal data such as media, docs, and videos. The normal users have limited permissions than the root user.

Each of the user’s default group is their username. For example, doe we created above belongs to the doe group. Linux keeps the groups’ data in the /etc/group file. You can find the default configuration details in the /etc/default/useradd file.

Deletion

You can delete a user using the userdel command.

sudo userdel <username>

Now you should find out how to list all users and their groups.

List all users

Show Names Only

Use the compgen command with the -u option to list all users on Linux.

compgen -u

OR

Run the cut command on the /etc/passwd file.

cut -d ":" -f 1 /etc/passwd
Output
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
...
rpcuser
saslauth
avahi
gnome-initial-setup
tcpdump
edxd

Show Details

The most straightforward way to list all users is to cat the /etc/passwd file.

cat /etc/passwd

You can use the less command to view a shorter portion of the output page or more for longer.

less /etc/passwd
more /etc/passwd
Output
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
sssd:x:997:994:User for sssd:/:/sbin/nologin
chrony:x:996:993::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pipewire:x:995:992:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin

Use the top/bottom arrow keys or the enter key on your keyboard to navigate the page and the q key to quit the navigation.

Alternatively, you can use the getent command to list all users.

getent passwd
Output
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
...
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
libstoragemgmt:x:978:978:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
saslauth:x:977:76:Saslauthd user:/run/saslauthd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
gnome-initial-setup:x:976:976::/run/gnome-initial-setup/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
edxd:x:1000:1000:edxd:/home/edxd:/bin/bash

OR

Reveal a specific user’s details by specifying their username.

getent passwd <username>

For example:

getent passwd edxd
Output
edxd:x:1000:1000:edxd:/home/edxd:/bin/bash

Here is what the colon separated values mean:

The first part, doe, is the uid (user id). x is a representation of the user’s hashed password. The first 1002 denotes that doe is a normal user. A system user would have a reading of less than 1000. The second 1002 is the gid (group id).

The next part, /home/doe, shows the user’s home directory. The last part, /bin/bash, denotes the shell assigned to the user.

Now that you understand how Linux manages users, groups and how to list users, you know how to list the user groups.

List All Groups

Display Group Names Only

Use the compgen command with the -g option to list all group names.

compgen -g
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem
wheel
...
rpcuser
saslauth
libvirt
avahi
gnome-initial-setup
tcpdump
slocate
edxd

Alternatively, you can sieve the names from the result of the getent command using the awk or cut command.

awk
getent group | awk -F: '{ print $1}'
cut
getent group | cut -d: -f1

Show Group Details

You can list all user groups on Linux by navigating the /etc/group using the cat, more, or less commands.

cat /etc/group

OR

more /etc/group

OR

less /etc/group
Output of more /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:edxd
cdrom:x:11:
mail:x:12:
man:x:15:
dialout:x:18:
floppy:x:19:
games:x:20:
tape:x:33:
video:x:39:
ftp:x:50:
lock:x:54:
audio:x:63:
users:x:100:

Alternatively, you can use the getent command with the group option.

getent group

Assume you want to count the groups. You can do that by piping the getent‘s result into the wc command.

getent group | wc -l
Output
69

Say you want to see the user groups created with the users in section 1 of this tutorial. Since they occupy the last part of the page, you can pipe the result of getent group into the tail command.

getent group | tail
Output
rpc:x:32:
libstoragemgmt:x:978:
rpcuser:x:29:
saslauth:x:76:
libvirt:x:977:
avahi:x:70:
gnome-initial-setup:x:976:
tcpdump:x:72:
slocate:x:21:
edxd:x:1000:

Lastly, you may want to list a particular group’s members. All you do is append the group name to the getent command.

getent group <group name>

For example:

getent group wheel
Output
wheel:x:10:edxd

List a Specific User’s Groups

Apart from listing user groups of all users, you could narrow it down to groups belonging to a particular user. The groups command plays a massive role in listing a user’s groups.

By default, Linux creates a user with the same uid as gid. The resulting group is called the primary group and can be reassigned.

Run the groups command to list all the groups of the logged-in user.

groups

OR

Use the groups command with the username to list the current user’s groups, whether they are not logged into them.

groups <username>

For example:

groups edxd
Output
edxd wheel

Alternatively, you can list a particular user’s groups using the id command.

Show group names and numbers
id <username>

For example:

groups edxd
Output
edxd : edxd wheel

OR

id -nG

Conclusion

The primary commands to list all user groups are compgen and getent. Alternatively, you can use the cat, more, less, id, and groups commands, as illustrated in this tutorial.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Rm Command in Linux
Read More

Rm Command in Linux

The rm command is a very important command used to remove files and directories in Linux. The rm…