list user in ubuntu

Ubuntu, like most Linux distributions, stores a list of users on the system. These users can include regular users, system users, and service accounts. If you’re an administrator or just curious about the users on your system, there are multiple ways to list them. In this blog post, we’ll walk through various methods to list users in Ubuntu, ranging from simple command-line utilities to more advanced commands.

1. Using the cat Command

One of the easiest ways to list users on Ubuntu is by checking the /etc/passwd file. This file contains basic information about each user, including usernames, home directories, and default shells. To list all users in Ubuntu using the cat command:

cat /etc/passwd

This will show all the details, but you might not want the extra information. The first field in each line represents the username, and the rest of the information is separated by colons. It might look something like this:

root:x:0:0:root:/root:/bin/bash

Here, “root” is the username. Each entry represents a different user account.

2. Using the cut Command

If you’re only interested in listing the usernames and not the full details, you can filter the output using the cut command. The cut command can extract specific fields from the /etc/passwd file, in this case, the usernames:

cut -d: -f1 /etc/passwd

This command will display only the usernames, without any additional information. For example, the output might look like:

root
user1
user2
...

This method is clean and quick if you need just the list of usernames.

3. Using the getent Command

Another reliable command to list users is getent. The getent command fetches entries from system databases, including user information from /etc/passwd as well as from external sources like LDAP if configured. To list users with getent, run:

getent passwd

Similar to the cat /etc/passwd command, this will output detailed information for each user. But unlike cat, getent will also show users from any external sources configured on your system. The output will look something like:

root:x:0:0:root:/root:/bin/bash
user1:x:1001:1001:User One:/home/user1:/bin/bash

4. Using the compgen Command

If you want to see all users (including system users), you can use the compgen command with the -u flag. The compgen command generates a list of all users on your system, whether they are regular users or system accounts:

compgen -u

This will list all the usernames in the system, including the system users, and will look something like this:

root
user1
user2
sys
daemon
...

While this is helpful to see every account on the system, it can also include system users that may not be of interest if you’re looking for regular human users.

5. Using the users Command

If you’re only interested in seeing which users are currently logged in, you can use the users command. This command will return a list of usernames for all users currently logged into the system:

users

For example, if two users are logged in, the output will look something like:

user1 user2

This method is useful when you need to know who is currently using the system, but it doesn’t show all users, only the active ones.

6. Using the w Command

For a more detailed view of the currently logged-in users, including their IP address, the terminal they’re using, and their activity, you can use the w command. This provides more detailed information compared to users:

w

The output will show who is logged in, where they are logged in from, and what they are doing, for example:

 09:30:01 up 2 days,  3:15,  2 users,  load average: 0.05, 0.01, 0.02
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
user1    pts/0    192.168.0.5     09:00    2.00s  0.10s  0.10s -bash
user2    pts/1    192.168.0.6     09:15    1.00s  0.05s  0.05s -bash

This is useful if you want to check the activities of users on your system.

Conclusion

Listing users in Ubuntu can be done in many ways, depending on what information you’re looking for. Whether you need a simple list of usernames or a detailed view of currently logged-in users, Ubuntu provides a variety of commands to help. The most commonly used commands are cat, cut, getent, compgen, users, and w. Choose the one that best suits your needs.