Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I see how many people are logged on to a Linux machine? I know the 'users' command shows all the people logged in but I need a number. Is there a switch for users that I am missing in the man page? I thought of using the grep -c command, but there must be something that is the same in each username for this to work. Is there an easier way?

share|improve this question

12 Answers 12

up vote 13 down vote accepted

You are looking for the wc (word count) command.

Try this:

users | wc -w
share|improve this answer
note that this approach (an who/w solutions) show only users logged in, not necessarily active users (i.e. users that start a process and then logout leaving the running process behind). Just to give a heads up about the difference. – estani Mar 18 '13 at 13:45

Classically, the command is 'who' rather than 'users', but 'who' gives you more information. Looking back at the original Unix articles (mid-70s), the example would have been:

who | wc -l

Using 'wc -l' counts lines of output - it works with both 'users' and 'who'. Using '-w' only works reliably when there is one word per user (as with 'users' but not with 'who').

You could use 'grep -c' to count the lines. Since you are only interested in non-blank user names, you could do:

who | grep -c .

There's always at least one character on each line.

As noted in the comments by John T, the users command differs from who in a number of respects. The most important one is that instead of giving one name per line, it spreads the names out several per line — I don't have a machine with enough different users logged in to test what happens when the number of users becomes large. The other difference is that 'who' reports on terminal connections in use. With multiple terminal windows open, it will show multiple lines for a single user, whereas 'users' seems to list a logged in user just once.

As a consequence of this difference, the 'grep -c .' formulation won't work with the 'users' command; 'wc -w' is necessary.

share|improve this answer
small nitpick - the users command does not print 1 line per user, it just prints id's sequentially, so grep -c . would not work in this case. Smart thinking though. – John T Jul 26 '09 at 3:59
this also means that wc -l will not work with the users command, as shown here: – John T Jul 26 '09 at 4:11
@John T: oh - well, given screen shot, you're correct. I tested 'users' on MacOS X - but with just one user logged in. When I double checked with a second user logged in, I see the 'all on one line' behaviour. – Jonathan Leffler Jul 26 '09 at 5:52
to simulate more users you can SSH into your own box :) – John T Jul 26 '09 at 5:59
Thank you for this valuable information, although I wanted to stick with the "users" command. Also upvoted you, thanks. – Anonymous Aug 1 '09 at 23:54

Open a shell and type:

who -q

The last line will give you a count.


(sigh) I misunderstood the question. Here's a somewhat brute-force approach:

To see unique user names:

who | awk '{ print $1 }' | sort | uniq

To see a count of unique users:

who | awk '{ print $1 }' | sort | uniq | wc -l
share|improve this answer
that counts all logins of the same user in the total. – hayalci Jul 26 '09 at 18:56
check the re-edit, I think you'll find that the new answers address that. – Avery Payne Jul 27 '09 at 15:33
This still doesn't work, uniq only removes duplicate successive lines, you need to sort the output of who first. – theotherreceive Jul 27 '09 at 18:10
that's what I get for posting answers at 1am. (sigh) fixed. – Avery Payne Jul 27 '09 at 19:37

Do you want to see the number of sessions, or the number of actual users?

$ who
andrew   tty1         2009-07-26 15:31 (:0)
andrew   pts/0        2009-07-27 00:11 (:0.0)
andrew   pts/1        2009-07-27 01:58 (:0.0)

That's on my laptop, so i'm the only user, but i'm logged on three times.

$ who | wc -l
$ users | wc -w

It is fairly easy to filter out these duplicates though to get the number of actual users.

$ users | tr ' ' '\n' | sort -u 
$ users | tr ' ' '\n' | sort -u | wc -l
share|improve this answer

Here's a bash version of tink's great awk post:

set $(users)
declare -A user
for u ; do ((user[$u]++)) ; done
for key in "${!user[@]}" ; do echo "$key: ${user[$key]}" ; done | column -t | sort -nk 2

Ok, it's a little bit longer, but was worth finding this one ... :).

While testing, do before next attempt:

shift $#                # clear positional parameters
unset user              # remove associative array variable
share|improve this answer

number of the users currently logged in:

who |cut -c 1-9 |sort -u |wc -l

the above buta with their account name:

who |cut -c 1-9 |sort -u |tee /dev/tty |wc -l
share|improve this answer
who | cut --delimiter=' ' -f 1 | sort -u | wc -l

Who prints out the list, cut removes everything but the first row, sort -u sort it and removes duplicates and wc -l counts the lines. Works fine for me on ubuntu/bash :)

share|improve this answer

You can simply use w (/usr/bin/w on my Red Hat based system) or uptime, they show the actual number of logged in users.


 22:40:38 up 3 days, 22 min,  1 user,  load average: 0.02, 0.01, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
manuel   pts/0    pc-manuel        09:35    0.00s  0.07s  0.00s /usr/bin/screen -xRR


 22:39:18 up 3 days, 21 min,  1 user,  load average: 0.08, 0.02, 0.01
share|improve this answer

And a method that uses only one pipe...

users | awk '{for(i=1;i<=NF;i++){a[$i]++}}END{for(i in a){print i"\t"a[i]}}'


share|improve this answer

"who" prints information about users who are currently logged on. Synopsis:

who [option] [file] [am i]

share|improve this answer

You could always download the free tool for unix called TOP. It produces a list of the users and also what they are doing on the system at the time and will continue to update as long as it is running.

It is located at

It has many command line switches so you should be able to extract the information you are looking for.

share|improve this answer
I've yet to find a linux distro that doesn't include top by default, let alone not have an option to install it from packages. – theotherreceive Jul 27 '09 at 1:32

If you are looking for the total number of users logged in and logged off in a proper sequence, the best command to run is

cat -n /etc/passwd
share|improve this answer
No, that's not what that command does. cat -n just prints all lines in the file, numbering each one. You'll get a list of users that exist on that system, but you won't get much login info. – cpast Feb 21 '13 at 5:01

You must log in to answer this question.