Linux User and Group Management: How to List All Users with Their Groups and Vice Versa


2 views

In Linux systems, users can belong to multiple groups, and groups can contain multiple users. This relationship is crucial for permission management and system administration. Let's explore the commands to reveal these associations.

To get started, here are the fundamental commands:

# List all users
cat /etc/passwd | cut -d: -f1
# or alternatively
getent passwd | cut -d: -f1

# List all groups
cat /etc/group | cut -d: -f1
# or alternatively
getent group | cut -d: -f1

To see which groups a particular user belongs to:

# Syntax
groups username
id username

# Example
groups john
id john

# Output format:
# john : john adm cdrom sudo dip plugdev lpadmin sambashare

To list all users who are members of a particular group:

# Syntax
getent group groupname

# Example
getent group sudo

# Output format:
# sudo:x:27:john,mary,admin

For system administrators who need more detailed information:

# List all users with their UID and primary group
awk -F: '{print $1 " (UID:"$3") - Primary Group: " $4}' /etc/passwd

# List all groups with their GID and members
awk -F: '{print $1 " (GID:"$3") - Members: " $4}' /etc/group

Some systems have additional tools that might be available:

# Using lid command (common on some distributions)
lid -g groupname

# Using members command (if installed)
members groupname

For regular use, you might want to create a custom script:

#!/bin/bash
# list_users_with_groups.sh
echo "Listing all users with their groups:"
echo "-----------------------------------"

getent passwd | while IFS=: read -r username _ uid _ _ _ _; do
    echo -n "$username (UID:$uid): "
    groups $username | cut -d: -f2
done

Remember that Linux systems have both regular users and system users. Some commands might show all users while others might filter them differently. The /etc/login.defs file often contains UID/GID ranges that distinguish system accounts from regular users.


When managing Linux systems, it's common to need visibility into user accounts, groups, and their membership relationships. Here are the essential commands for this purpose:

To view all users on the system:

cat /etc/passwd | cut -d: -f1
# Or more cleanly:
getent passwd | cut -d: -f1

For a more detailed view including UIDs:

awk -F: '{ print $1 " (UID:" $3 ")" }' /etc/passwd

To display all groups:

cat /etc/group | cut -d: -f1
# Or:
getent group | cut -d: -f1

To see which groups a specific user belongs to:

groups username
# Alternative:
id username

Example output showing user 'john' belongs to groups 'john', 'sudo', and 'docker':

$ groups john
john : john sudo docker

To list all members of a particular group:

getent group groupname

Example checking the 'sudo' group:

$ getent group sudo
sudo:x:27:john,mary,admin

For more complex queries, you can combine these commands. For example, to find all users who belong to the 'docker' group:

getent group docker | cut -d: -f4 | tr ',' '\n'

Or to find all groups that have more than 5 members:

getent group | awk -F: '{if (split($4, a, ",") > 5) print $1}'

Here's a simple bash script to show all users and their groups:

#!/bin/bash
echo "User-Group Membership Report"
echo "==========================="

for user in $(getent passwd | cut -d: -f1); do
    echo -n "$user: "
    groups $user | cut -d: -f2
done

And another script to show all groups and their members:

#!/bin/bash
echo "Group Membership Report"
echo "======================"

for group in $(getent group | cut -d: -f1); do
    members=$(getent group $group | cut -d: -f4)
    echo "$group: $members"
done