Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

10 Answers 10

up vote 8 down vote accepted

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

Try this:

users | wc -w
share|improve this answer
    
what i was looking for thanks –  Anonymous Aug 1 '09 at 23:53
    
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 line (as with 'users' but not with 'who'), and the '-w' option is relatively new (though obviously present on Linux and required by the current POSIX standard - it was not always present, though it has been standard since at least 1997).

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. 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: i26.tinypic.com/4pw0vd.png –  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
1  
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.

EDIT:

(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
3
$ users | wc -w
3

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

$ users | tr ' ' '\n' | sort -u 
andrew
$ users | tr ' ' '\n' | sort -u | wc -l
1
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.

w:

                              v
 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

uptime:

                              v
 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

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 http://www.unixtop.org/

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
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.