How to List Users and Groups on Ubuntu 24.04

In Linux, users and groups are fundamental for managing access and security. They help identify the person or user having system access and what resources they can use. Grouping users with similar needs simplifies assigning and managing file and directory permissions. Linux users and groups can help you solve file ownership or access issues by identifying the root cause using this list of groups.

This brief article will cover different ways of listing users and groups in Ubuntu 24.04.

Table of Contents

1. What are Users in Ubuntu?

In Ubuntu, users describe the unique individual system accounts. All users have their unique username and password to log in. This account grants them specific permissions and determines what resources they can access on the system. User accounts can be for people or even programs.

User accounts for a particular user define their identity with properties like a username, a unique user ID (UID), a home directory to store their files, and a default shell program for interacting with the system.

2. Types of Users in Ubuntu

There are two main types of users in the Ubuntu system:

  • Regular Users: These are the accounts created by administrators for everyday tasks. They have limited privileges to ensure system stability. Regular user UIDs start from 1000 and above. This is the command range for user accounts created for people who log in and use the system.
  • System Users: These users are created during installation. These accounts run background services and applications critical for the system’s function. The most powerful system users are root users. It has complete administrative control of the system. The system user’s UID’s range starts from 0 to 999 and is reserved for system accounts and services.

3. What is the /etc/passwd File in Ubuntu?

The /etc/passwd file in Ubuntu is a plain-text database that contains fundamental user information. Each line is a user account, with different fields separated by colons (:). 

username:x:UID:GID:comment:home directory:shell

These fields include:

  • Username: The name of the user.
  • Encrypted Password: This shows either the actual password or one encrypted with an x character for more security. The x character is a password saved inside the /etc/shadow file. 
  • UID (User ID): A unique numeric identifier for the user.
  • GID (Group ID): The numeric identifier for the user’s primary group.
  • Comment or Description: A descriptive name for the user.
  • Home Directory: The path for the user’s home directory.
  • Default Shell: The default shell /bin/bash or command interpreter for the user.

4. How to List Users in Ubuntu?

To list the users in the Ubuntu system, you must simply list the content of the /etc/passwd file. As explained above, this file contains all the information related to system users, and by listing its contents, you can get the user’s information. To list the contents, you can use several commands, like the cat, awk, less, and getent commands.

Let’s cover all these commands to list users in Ubuntu.

4.1. Using cat Command

The cat command can list the entire /etc/passwd file content and display all users on the terminal. For example, to list all users using the cat command run this:

cat /etc/passwd
List Users And Groups On Ubuntu 24.04 A

The above output contains a separate line for each of the users.

To get the users list with line number, run the cat command with the -n flag:

cat -n /etc/passwd
List Users And Groups On Ubuntu 24.04 B

If you only need to list the details of a particular user, run the cat command and piped it with the grep command along with the username:

cat /etc/passwd | grep ilf
List Users And Groups On Ubuntu 24.04 C

To count the number of users, pipe the output to the wc command:

cat /etc/passwd | wc -l

The number of lines corresponds to the total number of users.

List Users And Groups On Ubuntu 24.04 D

4.2. Using less or more Command

If your system has many users, you can limit the output displayed at once using terminal pagers. For this, you can use the less command.

To open the /etc/passwd file using less, enter:

less /etc/passwd

Now you can navigate through the file using the keyboard and get all the information related to your system users. It gives you a preview of all users without listing them on the terminal.

List Users And Groups On Ubuntu 24.04 E

You can also use more commands for similar results:

more /etc/passwd

This will open up the details of each user one by one on the terminal without cluttering it.

List Users And Groups On Ubuntu 24.04 F

One addition to the more command compared to the less command is that it displays the percentage of content currently displayed on the screen. For example, 38% of the content in the above image is currently displayed, and the remaining 62% can be viewed upon scrolling.

4.3. Using awk Command

Another way of extracting the users from the passwd file is to use the awk command. The awk command can extract and display specific information from a file. You can use different flags with the awk command to obtain the required output.

For example, to list the usernames only, run this command:

awk -F':' '{print $1}' /etc/passwd

The above command prints the first field inside the passwd file, which denotes the usernames, for this purpose the {print $1} flag is used here. The -F flag tells the awk command to consider colon (:) as a field separator when parsing each line in the file.

List Users And Groups On Ubuntu 24.04 G

If you want to get other information with the username, like UID, you can define different numbers for the {print $1} flag. For example, you must replace the $3 in the above command for UID, which describes the third field in the passwd file.

4.4. Using getent Command

The getent is another Linux command that retrieves entries from various system databases, such as passwd. All these databases store important information like usernames, hostname, and services. Using the getent command with the passwd file, you can extract all the user’s information:

getent passwd
List Users And Groups On Ubuntu 24.04 H

The getent command can also list users with respect to their UIDs. For example, to list all regular users, run this command: 

getent passwd {1000..60000}
List Users And Groups On Ubuntu 24.04 I

Similarly, you can also list a particular user by defining its username or UID along with the getent command:

getent passwd [username or UID]
List Users And Groups On Ubuntu 24.04 J

4.5: Using cut Command

The cut command is another command-line utility for extracting, or in other words, cutting, some information from a file and displaying it on the terminal.

For example, to cut the list of users, you can use the cut command like this:

cut -d: -f1 /etc/passwd

Here, the cut command extracts the usernames described by the f1 flag from the /etc/passwd/ file. The -d (delimiter) option specifies the colon.

List Users And Groups On Ubuntu 24.04 K

4.6: Using compgen Command

The compgen is a bash built-in command used to list various data. We can use this command for listing users in a system using its -u flag:

compgen -u
List Users And Groups On Ubuntu 24.04 L

4.7. How to Separately List Regular and System Users

If you want to list regular and system users separately, try these commands.

You can run this command to list only the regular users. Keep in mind the UIDs that start from 1000 and above for regular users:

getent passwd {1000..60000} | cut -d: -f1
List Users And Groups On Ubuntu 24.04 M

Similarly, like the cut command, this can also be done with the awk command:

getent passwd {1000..60000} | awk -F':' '{print $1}'
List Users And Groups On Ubuntu 24.04 N

To list all the system users define their UIDs from 0 to 999:

getent passwd {0..999} | cut -d: -f1
List Users And Groups On Ubuntu 24.04 O

4.8. How to List Currently Logged-in Users on Ubuntu

To list currency, logged-in users run:

users
List Users And Groups On Ubuntu 24.04 P

Similarly, you can also try who command for the same purpose:

who
List Users And Groups On Ubuntu 24.04 Q

5. What are Linux Groups?

In Linux, groups represent user collections having the same permissions. Groups help Linux administrators easily assign permission to files and directories by defining groups. There is no need to set permissions for each user individually; you can create a group of users with the same permissions. All members of that group will inherit those permissions.

Like users, we have also two groups in Linux systems:

  • Primary or Login Group: Each Linux user belongs to a primary group. This group is created with the user account and shares the same name as the username. The user’s primary group defines the default permissions for files and directories.
  • Secondary or Supplementary Group: Users can be members of one or more supplementary groups in addition to a primary group. Depending on their roles, you can set extra permissions.

Now, let’s cover different ways of listing groups in Linux.

6. How to List All Groups in Linux?

Like the passwd file, which stores all the user information, the Linux groups are listed inside the /etc/group/ file. To read the details of groups, one can use the same command we used earlier for user listing.

Let’s cover all those commands here for listing groups.

6.1. Using groups Command

In Ubuntu, you can use the groups command to list the groups of your current users. It will list all those groups your current user is a part of.

groups
List Users And Groups On Ubuntu 24.04 R

6.2. Using /etc/group File

The /etc/group/ file contains the necessary details of Linux groups. Different commands, such as cat, less, more, and awk, are used to list its content.

To list the groups using the cat command, run:

cat /etc/group
List Users And Groups On Ubuntu 24.04 S

Similarly, to list the groups in a more precise format run the less or more command:

less /etc/groupmore /etc/group
List Users And Groups On Ubuntu 24.04 T

If you only want to list the names of groups, run the following:

cat /etc/group | cut -d: -f1
List Users And Groups On Ubuntu 24.04 U

Similarly, this can also be done using the awk command:

cat /etc/group | awk -F: '{print $1}'
List Users And Groups On Ubuntu 24.04 V

If you want to check the group of a specific user, then run this:

cat /etc/group | grep <group>
List Users And Groups On Ubuntu 24.04 W

6.3. Using cut Command

The cut is another useful text processing command to extract group information from the /etc/group file:

cut -d: -f1 /etc/group
List Users And Groups On Ubuntu 24.04 X

6.4. Using getent Command

The getent command retrieves information from system databases, such as user information from the passwd file, using the Name Service Switch configuration (/etc/nsswitch.conf).

To get the information of all groups, run:

getent group
List Users And Groups On Ubuntu 24.04 Y

Similarly, to get the group detail of a specific user, define the name of the user with the getent command:

getent group sudo
List Users And Groups On Ubuntu 24.04 Z

Conclusion

In Linux, system users and groups help the administrator manage file and content permissions. This article covered various methods for listing users and groups in Linux. We covered commands like cat, getent, and groups to display user and group details. All user details can be found in the /etc/passwd file, while group details can be found in the /etc/group file.