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?
|
You are looking for the Try this:
|
|||||
|
|
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:
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:
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 ' |
|||||||||||||
|
|
Open a shell and type:
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:
To see a count of unique users:
|
|||||||||
|
|
Do you want to see the number of sessions, or the number of actual users?
That's on my laptop, so i'm the only user, but i'm logged on three times.
It is fairly easy to filter out these duplicates though to get the number of actual users.
|
|||
|
|
|
number of the users currently logged in:
the above buta with their account name:
|
|||
|
|
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 :) |
|||
|
|
|
You can simply use w:
uptime:
|
|||
|
|
|
And a method that uses only one pipe...
:} |
|||
|
|
|
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. |
|||
|
|
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
|
|||||
|
