Linux Command to Check User Groups (original) (raw)

Last Updated : 14 May, 2026

Linux is a multi-user operating system that allows multiple users to work on the same system securely and efficiently. User groups in Linux make it easier to manage permissions by applying access rules to multiple users at once.

**Example: Create a user named demoUser3 to demonstrate how user groups can be checked in Linux.

**Commands:

sudo groupadd demo_group
sudo useradd demo_user
sudo groupmod -a -U demo_user demo_group
groups demo_user

When a new user is created, a new group with the same name is created and the user is added to it. This group is called the primary group of the user.

**Output:

Creating a group, a user, and adding the user to the group.

Creating a group, a user, and adding the user to the group.

Linux Methods to Check User Groups

Method 1: The "groups" command

The groups command is the simplest and most commonly used way to list all groups (both primary and secondary) that a user belongs to.

You can start using it immediately from the terminal.

**Command:

groups --version

**Output:

1

This command prints version information, it confirms that groups is installed and working correctly.

**Syntax:

groups []

**Example 1: Check groups for a specific user

It is used to display all the groups that the user liveuser belongs to.

**Command:

groups liveuser

**Output:

Groups to which user liveuser belongs.

Groups to which the user liveuser belongs.

This means:

**Example 2: Check groups for another user

**Command:

groups demoUser1

This shows that demoUser1 belongs to multiple groups, which is common in real systems.

**Output:

Using groups command with username, example 2

Using groups command with username, example 2

**Example 3: Check groups for the current user

**Command:

groups

**Output:

Using groups command without username.

Using groups command without a username.

Method 2: The "id" command

The id command is another built-in Linux command that shows user identity information, including user ID (UID), group ID (GID), and group memberships. It is especially useful when you want structured output or are writing scripts.

**Syntax:

id -G -n username

**Example:

id -G -n demoUser1

This confirms all groups associated with the user.

**Output:

Using the id command to retrieve the groups a user belongs to.

Using the id command to retrieve the groups a user belongs to.

Method 3: Using the /etc/group File

The /etc/group file stores all group information on the system. Every group has an entry in this file.

**1. Entry format in /etc/group file

group_name:x:group_id:user1,user2,user3

**2. Manually searching /etc/group file

less /etc/group
cat /etc/group
more /etc/group

Contents of /etc/group file.

Contents of /etc/group file.

However, manual searching is not recommended on production systems because the file can be very large.

Better approach: Use grep to filter results

Instead of scrolling, you can search only the lines that contain a specific username.

**Syntax:

grep -w username /etc/group

**Example: Using the grep command for searching

Even the grep command is pre-installed hence no installation is required. grep command is used for pattern matching in strings. We use it here to print only those lines from the /etc/group file where our concerned username appears.

**Command:

grep -w demoUser1 /etc/group

This is used to see all groups the user belongs to.

**Output:

Using the grep command to conveniently retrieve group information from the/etc/group file.

Method 4: The "getent" command

The getent command retrieves information from system databases such as:

This makes it very useful in enterprise environments where users and groups may not be stored locally.

**Basic command:

getent group

This prints all group entries, which can be large and hard to read.

**Recommended approach: Combine getent with grep

**Syntax:

getent group | grep -w username

**Example:

getent group|grep -w demoUser1

This displays only the groups where demoUser1 is a member.

Using intent and grep commands together to retrieve the groups to which the user belongs.

Using intent and grep commands together to retrieve the groups to which the user belongs.