Linux - Liste des utilisateurs

how to list local users?

I. Presentation

In this tutorial, we will see how to obtain an exhaustive list of users present on a Linux machine, whether on a Debian distribution, Ubuntu, Rocky Linux, etc… Because, despite the differences between some distributions, there are still many commonalities. It’s an easy manipulation can be useful in different cases: for example, if you want to analyze your machine in search of a suspicious account or check the presence of an account.

II. The /etc/passwd file

In Linux, local user account information is stored in a system file, which is as follows:

/etc/passwd

Each line of this file represents information for one and only one user. There are native users, such as “root”, “daemon” or even “bin” and users specific to the local machine, in particular your account, or the account of certain services. For example, if an Apache server is set up on the machine, there will be an account for this service: usually “www data” but the name may vary between distributions.

To be more precise, each line will contain the user name, the encrypted password (when it is indicated “x”, it is that it is necessary to refer to the file /etc/shadow), the user’s UID, full name, home path, and default login shell.

Now let’s see how to list the users of the Linux machine.

III. List users in Linux

By looking at the contents of the “/etc/passwd” file, we can get the list of users, but it’s somehow polluted with all the additional information. Which gives, for example:

cat /etc/passwd
less /etc/passwd

Output :

Contents of the passwd file in Linux

To get only the list of users by selecting only the name, to have a clean output, we can rely on commands like “cut” and “awk” to retrieve only the first value.

With the cut command, this gives:

cut -d: -f1 /etc/passwd

With the awk command, this gives:

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

I give you the two possibilities, so you choose the one you prefer according to your habits.

In both cases, we get a similar output:

List Linux users with cut

To find out if a user exists, by searching for it by name, you can add a “grep” in addition to fine-tune the output:

cut -d: -f1 /etc/passwd | grep "flo"

In case the user exists, the name is displayed otherwise the command returns an empty output.

As I said before, each user has a UID. This is important information to differentiate between standard user accounts and other accounts. For example, my user “flo” has UID “1000” and if I create another one, it will have UID “1001”.

As a result, you can use the getent command which allows you to obtain information from different files, including passwd, to list users who have a UID between 1000 and 60000. For example:

getent passwd {1000..60000}

Which give :

Getend Command - List Users

IV. Count the number of users in Linux

Bonus before you leave, how to count the number of local users present on a Linux machine? Well, simply by using the “wc” command corresponding to “Word Count” and not to anything else…!

Here is an example :

cut -d: -f1 /etc/passwd | wc -l

On my test Linux machine, the value 37 is returned! It’s not surprising even though I mainly use one account in my name, there are all the accounts associated with the system and the services.

V. Conclusion

Thanks to this tutorial, you are able to list the users of your workstation or server under Linux using various commands! Remember that these commands are valid on different Linux distributions.

Commandes et Système,Linux,Astuce,

#list #local #users

Leave a Comment

Your email address will not be published. Required fields are marked *