Seeing all users in linux

This post and this website contains affiliate links. See my disclosure about affiliate links.

linux: how to list users?…show all users or just local users

Linux doesn’t provide a straight forward command to list all users in the system. You can list users who are currently logged in, or you can find groups that the user belongs to, but really no simple way to list users or to get a comprehensive list of all users in the system or a list of users in a specific group.

However, this being Linux there are several different commands that you can use to list users, if you are willing to use the command line to stitch some commands together. First, you might be better off by understanding how and where the user names and the associated user data is stored in Linux to understand how and why the commands detailed below will work.

/etc/passwd

The /etc/passwd file holds all the necessary information about the local users such as the user id, password info, login, primary group and home directory. But this file also lists the local users to the system including pseudo users such as adm, mail, news, apache etc etc. These are pseudo users that the system uses to run some applications or services and are not real world users with login privileges.

This file however does not include users that are remote to the system but can log in to the system, such as users in external databases like NIS or LDAP.

/etc/group

The /etc/group file holds the information pertaining to the user groups in the system. There is a field in the file that lists the logins of users who belong to that particular group. This field can be useful when listing the users.

Now lets’ see how you might be able to list users in the system.

list all local linux users

First let’s see how we can list all the local users in the system. You can list all the local users by doing a simple cat of the passwd (/etc/passwd) file.

This prints out all the information on the file. If you are just interested in the login names of the users, then you can cut out just the pertinent information. The login name is the first field in each line separated by the colon (:) delimiter.

You can format your output so that it prints out more than just the login names. In order to print out the full name and home directory along with the login, you can use the awk command…

You can substitute the variables ($1, $5 and $6 in the above example command) to print out more (or less) information as needed. You can add more fields to the list as well.

Another command in Linux that essentially does the same is the passwd command. You can get essentially the same information as detailed above by using the passwd command as in the example below.

Читайте также:  Linux create dev files

To print out some of other user information such as password change date, minimum age, max age and inactivity period etc, you will need to use the passwd command instead of the passwd file directly.

list users in linux

list only “real” linux users

The above example lists users irrespective of their status or privileges which means it will also contain the pseudo users as described above. If you like to list only “real world” users with login privileges and a home directory which is probably what you meant by “list users“, then you will need to filter the above command output to weed out these pseudo users.

Let’s assume that the real users on the system have a home directory at /home.

$ cat /etc/passwd | grep ‘/home’ | cut -d: -f1

If you happen to have users on multiple partitions such as /h1, /h2 and /h3 ….then you can modify the grep command to filter all of them out using some regular expressions such as in the example below.

$ cat /etc/passwd | grep -E ‘:/h1|:/h2|:/h3’ | cut -d: -f1

Remember that you will need to filter the users using grep before using cut to print only the login names. The above examples will list users that have a home directory, under the assumption that all “real” users will have a home directory.

list all users in linux

The above lists only the local users. If you like to get a list all the users that have access to the system across many database such as NIS, LDAP etc, then the command you can use is getent. You can use the cut, grep and awk commands to modify and format the output as described the previous commands.

If you would like to list only logged in users, then you will need to use other linux commands such as who, w or finger.

list all linux users in a group

Again there is no single and simple command to do this. The /etc/group file contains all the group names and the users who are in that group, but may or may not have the users who have the particular group as their primary group. That information is stored in the /etc/passwd file and it uses the group id rather than the group name.

$ grep ^usb /etc/groups | cut -d: -f4

The above command will list users who are in the user group usb, but may not (sometimes it does though, depending on your linux distro or configuration!) include the users who happens to have usb as their primary group. We can find all the users whose primary group is usb by looking at the /etc/passwd file.

We need to get the group id first, from the group file for the group usb, for that we will have to grep the /etc/group and get the third field which is the group id.

$ grep ^usb /etc/group | cut -d: -f3

Now, we can plug this into another grep command that will search the /etc/passwd file. This will print out the users that are in the passwd file with the primary group usb.

$ grep :`grep ^usb /etc/group | cut -d: -f3`: /etc/passwd

Now, merging all of the above commands into a single line command….

$ grep ^usb /etc/group |cut -d: -f4 && grep :`grep ^usb /etc/group | cut -d: -f3`: /etc/passwd | cut -d: -f1

Читайте также:  Linux if one liner

Ok, that is definitely a long command to type out if you do this often. You do have the option of converting this into an alias or a shell script so that you can use it easily and repeatedly without having to retype.

This still might list some users twice if they exist in both the group as well as the passwd files, and the output is comma separated and in multiple lines. You might also want to convert this into a shell script that accepts the group as an argument. I will leave that as an assignment for you.

Источник

How to List All Users in a Linux System?

In the Linux system, it is a critical administrative task to manage users, add them, remove users, or assign new user privileges. In Linux, numerous users can simultaneously work on the same system. But security measures must be made to stop breaching other users’ private data. Information related to the local users is stored in the path “/etc/passwd”. In which, every row indicates the data of a single user that may contain the name of the user, user Id, directory of the user, and login details. When it comes to the listing of the user in Linux, there are multiple ways to list them, we will discuss some of these in this tutorial.

Prerequisites

To list users, we must have Linux running on our system and also access to the terminal to perform this task.

Method 1: Using the Cat Command

Cat command is the short form of concatenation, it is used to read the data of the file without opening it. In this, we will use the cat command to list all users in Linux. Additionally, it is used to make a new file and put data in it.

Syntax:

The given data below is the syntax to use the “cat” command where file_name represents the name of the file that has to be read.

To list the user using the “cat” command, we will first launch the terminal.

After that, we will run the command:

In the command above, we have used the “cat” command along with the file name “etc/passwd” which is the one in which all of the user’s data is stored. After running this command and by pressing enter, we get the output as shown in the snippet below which includes the list of the users that are stored in the file. As we discussed above, each row indicates the details of the single user.

If we only want to see the number of users that are allowed to use the system, we can just write the following command:

In the command listed above, we used the “wc” command along with the “cat” command. “wc” is the one that is used to count the line, word, or bytes of the file. In our case, we are going to list the number of rows that are storing the single user data. That is why we have passed “-l” which means the lines are counted. After running the command above, we obtained the number of users whose data is stored in the file that in our case is “47”.

Method 2: Using “less” or “more”

The other method to list the users from any file is “less” or “more”. Less and more are the terminal pagers commands that allow us to read the files line by line or to read them page by page.

Читайте также:  Linux policy password policy

Syntax:

Below is the syntax to use the “less” command to list the users from any file.

In the snippet, we have successfully executed the list of the users that are stored in the /etc/passwd file. The less command can also be used to read the list of users. It will display the users until it reaches the end of the terminal by scrolling the terminal using the down button, we can display the remaining data of the file.

Using the “more” command to list the users, below is the syntax to use the “more” command.

This command has some limited functionalities. It will display some of the percentages of the file like in the snippet below. It displayed about 47% of the data of the file by pressing the “enter” key we can display the data to some percentage, so we have to press the “enter” key until the 100% of it is not displayed.

Text Description automatically generated

Method 3: awk Command

In this, we will discuss another method to list the users of the system which is an “awk” command. The awk command is useful only when we have to display the name of the user neglecting the other details that are stored in the file related to any user.

Syntax:

Below is the syntax to use the awk command to enlist the user’s name.

In the syntax, colon “:” is used to separate the input that is given by the awk. After that, it prints the first value of each row where –F is a file along with the argument which is responsible for reading the file and then displaying it as an output. The /etc/passwd is the file name that we want to read.

Text Description automatically generated

If you have multiple pages in a single file and you want to read the file page by page by writing the following command, you can perform this task.

In the above syntax, less will enable us to read the file page by page if multiple pages exist.

Method 4: Using the getent Command

The “getent” command is most similar to the “cat” command as we can display the complete details of the users along with the technical details. The “getent” is also responsible for displaying the complete details of the users.

Syntax:

The syntax for writing the getent command is given below. In this syntax, we just passed the name of the file along with the “getent” command.

After running the command, the whole file data is executed as shown in the below screenshot.

Text Description automatically generated

Conclusion

In this guide, we introduced the listing of the users that are using the same system at the same time. It is an administrative task to manage multiple users at a time. We also examined some of the techniques that are employed to enlist the users on Ubuntu 20.04. As we know it is necessary to manage the users but first, it is important to know the users who are using the system so it would be made secure for others to prevent data breaches between all of the users.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.

Источник

Оцените статью
Adblock
detector