Home Linux Linux List Users (Listing Users Based On Different Forms)

Linux List Users (Listing Users Based On Different Forms)

0

If you have ever wondered about “Linux list users”, you are in good company.

The reason may be due to the fact that it provides important info about users (all types). It may also be that you are a new system administrator and you want to list all your users.

Whatever your reason for wanting an answer to your “Linux list users” question, this article will explain exactly how it’s done in easy to follow steps below. 

I will first explain the types of users (and the source of the info about the list of users) before going into detail about listing users.

Types Of Users (And Where The List Of Users Info Can Be Found)

There are two different types of users. System Users and Regular Users. Sometimes Regular Users are referred to as Normal Users. 

System Users – created by default by the system itself.

Regular Users – System administrators create these users and give them access to login to the system.

File Path Of Where The List Of Users Are Stored

Knowing where the list of users info we need is stored is important. 

The list of users info is stored on the system in a file named “passwd“. This file’s directory is /etc. The file is in the text format.

Below is the file path to the file where every info about users is stored. 

$ /etc/passwd

Linux List Users (Listing Users Based On Different Forms)

Below, I will be explaining the various forms of commands that you can use to list users according to some sort of feature or identity. 

You will be able to segregate, arrange, rank, or specify users and a host of other things. 

List All Users

I thought we should start with the basic command of listing all users since that would probably be what you wanted to know when you asked the “Linux list users” question.

You can list all users easily by using the cat command. (There are other ways, but I am sticking to the way I find the easiest).

$ cat /etc/passwd

Linux list users

Explaining What You Will See When You Use The Command Above

When you use the cat command, what you will see is –

  • The first field you’ll see is for the user name of that user.
  • The next field is for the password. Not the actual password because that is encrypted but a representation of the password. Encrypted passwords are stored in the /etc/shadow file.
  • The next field after the one above is for the User ID or UID as it may be referred to.
  • The next field alludes to the group to which the user belongs to, the primary group.
  • The next field has information like emails and addresses. Basic User ID information.
  • The home directory of the User then comes next.
  • After the above, then the shell used by that specific user is shown.

So you see the tons of info you can get from the simple command of listing all users. From here on, I will now be explaining how to simplify this info even further and group users.

Listing Users By Name (Alphabetical Order)

This is basically the same command above but now the results will be sorted out alphabetically. 

You might find this useful if you want your info in an orderly fashion.

What you’ll need to do is input sort of the same command used above with minor changes and then add a sorting command at the end.

$ cut -d: -f1 /etc/passwd | sort

Linux List Users

Your command results should now be sorted alphabetically. 

Listing Users By Disk Space Usage

Now we’re getting to the more interesting part. 

Let’s say you have users that are using more disk space than others and you want to find out which users are doing that.

When you have a large directory, that can be a bit of a drag but you can just use the command below to rank the users based on their disk space usage. 

You will be able to immediately see the users who are guzzling disk space.

$ sudo du -smc /home/* | sort -n 

This command will rank the users based on disk usage in the /home directory. 

-n in the command is what makes the sorting based on the disk usage numbers.

Listing All Currently Logged In Users (Plus Other Important Info)

I will explain three commands you can use to list all currently logged in users. 

The commands are ranked from the most basic one to the one that gives you more info and then to the most advanced one.

$ users

When you use the command above, you will immediately get info about users who have open sessions in the system. 

You will only get a list of all the users’ names and nothing more. This is why this command is basic. It is good if that’s all you want, but if you want more info, try the ones below.

$ w

This command now shows you the users who have open sessions and then also shows you the time the session started. 

Info about the terminal session the user has available will also be shown.

There is another command that can show even more, plus it can be used by the entire Unix family.

$ who 

The command above shows the users currently logged in and it also shows info about the login. You can go further with this command though by adding -a.

$ who -a

This reveals all the info there is to know about the users that currently have open sessions on the system. 

Listing Users Without A Password

Many folks are searching “Linux list users” for this tip. It is important to know the users that don’t have a password.

It is quite easy, just follow the command below.

$ sudo getent shadow | grep -Po ‘ ^[^:]*(?=:.?:)’

Using the command above will help you see the list of users without a password. 

Listing Users By Recent Login History

Remember I already explained the command to use when finding out about users who are currently logged in. 

This one’s a bit different. This is for a list of users based on their recent login info. 

You will be able to see all the logins recently or even check out specific users and their recent logins. 

The command you’ll be using is the last command. This is for checking out all users’ recent login history.

$ last 

You should be able to see all the users’ recent logins history now. If you want to specify by users, you will have to add the username to the command like this below.

$ last username

Username in the above command stands for the username you want to see the recent login history of.

So, replace the username above with the actual username you’re trying to find info on.

You will be able to see when the sessions started for that particular user, you will also see how long the sessions took.

Listing Users With Their User ID

Every user has their own user identifier (ID) and you can use this to know which users are system users or normal users.

This is because system users have a range of User IDs of between 0 – 1000. 0 is basically for users who have root access on Unix systems. And yes, there can be more than one “0” user.

Normal or regular users have user IDs of more than 1000 and onward. 

The user ID is basically used to manage the different accounts in the OS. 

The command that does this is the awk command. Here’s how it’s done below.

$ awk -F: ‘{printf “%s:%s\n”,$1,$3}’ /etc/passwd

You should be able to see each user with their specific USID. You can now also know which one is a system user or a regular user from the user ID.

Listing Users Who Logged In On An Exact Date And Time

Maybe you’re asking the “Linux list users” question because you want to find out more info about logins on a particular day.

Then the command below will help you in doing that. 

$ last -t YYMMDDHHMMSS

The format above is used to indicate where you will put the following information

YY- Year

MM – Month

DD – Day

HH – Hour

MM – Minutes

SS – Seconds

This command is a precise command that lets you find out exactly who logged in at a particular time. 

Listing The Total Number Of Users

This is quite important because if you have a large directory, you might want to just get the exact number.

To get the exact number of users in the system, you will have to use the wc command below.

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

The total number of users will now be visible. There’s more to this that we can find out. The total users can now be separated into regular users and system users. 

Remember what the UID represents. I explained that 0 -1000 represented system users and numbers above 1000 represented regular users. 

Using the info above, the command below brings up the list of users based on what you input. I’m going to try and get a list of the regular users in the command below. 

awk -F: ‘$3 >= 1000 {print $1}’ /etc/passwd

This command should give you a list of all the regular users on the system.

You can then immediately assume that the other users not shown are the system users. 

Conclusion

Your “Linux list users” question has been answered using a variety of commands above. I have tried to explain different commands that you might need especially with the question in mind.

Information about users can be generalized as seen above but it can also be specialized (as seen above too). 

Use any of the commands to find the specific info you need about any user or a set of users. 

Please use the comments box below to share any other command you think folks who are asking the “Linux list users” question might need.