3 ways to list all currently logged in users in linux

Many a time in multi-user environments like Linux, you want to find all the logged in sessions or list all logged in users. This will allow you to know if there has been any 'defunct' sessions which has hanging around for long. This might even be necessary from a security perspective. It could also help you to figure out if there is any particular user and process that is hogging the system resources.

The list of all logged in users will be a subset of all the users that are in the system. If you like to see the list of all users in a Linux system, then you will need to use different set of commands than the ones mentioned in this post.

w command

The w command is a combination of some of the commonly used Linux commands, such as who, ps and uptime and provides a comprehensive output of the logged in users.

bash$ w

The w command, without any command line options will show you a list of all the users who are currently logged in to system and the information about all the processes that are currently running. By default it should show you the user name, the login time, the CPU resources consumed by each process, the terminal (or tty) and the executing command.

bash$ w <username>

Specifying a particular username to the w command will let you see all the sessions of that particular user and just the processes run by that user. Substitute the username field in the example above with the actual login name of the user.

who command

Another command that you can use is the who command, which is closely related to the w command above. Compared to the w command, the who will print out much more concise information. It usually skips the detailed process information by default and shows you the user name and the tty information.

There are some useful command line options available with the who command…

-b (or –boot) : this will print out the time of the last system boot
-d (or –dead): prints out some information about processes that are dead or defunct
-a (or –all): this enables a bunch of useful options such as -b, -d, -l, -p etc.

bash$ who -a

This will list all logged in users along with detailed information about each session and terminal.

users command

Sometimes, you just want to know the logins of the users who are currently logged in. The users command will print out just the user or login names with out any of the other information such as login times and process information.

bash$ users

This will list all logged in users. However, if a user has multiple sessions then it will print out the user name for each session. The previously mentioned commands w and who also outputs the user name for each session. It shows looks redundant or repetitive in this case as there is no additional information about the session.

Output Customization

Sometimes, the purpose of listing the currently logged in users is to feed it as input to another script or another process. You might be interested in just the username listed just once and uniquely.

You can use several other Linux utilities to do this, such as cut, sort, awk, tr and uniq. We will use some of them below to demonstrate how it works.

bash$ who | cut -d' ' -f1 | sort | uniq

You can use the above output formatting with the w command as well. This will list  all logged in users sorted by username and each will be listed just once. Below is the similar command but this time using the w command.

bash$ w -h | cut -d' ' -f1 | sort | uniq

Some of the other very generic examples using other commands such as users and who are…

bash$ users | tr ' ' '\n' | sort -u
bash$ who | awk '{ print $1 }' | sort | uniq

Depending on your requirement, you can further play around with the options of the cut utility to show more fields, such as

bash$ who | cut -d' ' -f1,5 | sort | uniq

Another command that is probably worth mentioning here is last. The last command will display a (usually long) list of all users that were logged-in in the past. This list will contain both the users that are currently logged-in and active as well as users who are not currently logged-in. Use the command line argument -n to limit the list.

bash$ last -n 100

The above command will show the last 100 users who have logged into the system.