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?
14 Answers
You are looking for the wc
(word count) command.
Try this:
users | wc -w
-
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.– estaniMar 18, 2013 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.
-
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.– user1931Jul 26, 2009 at 3:59
-
this also means that wc -l will not work with the users command, as shown here: i26.tinypic.com/4pw0vd.png– user1931Jul 26, 2009 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. Jul 26, 2009 at 5:52
-
1to simulate more users you can SSH into your own box :)– user1931Jul 26, 2009 at 5:59
-
Thank you for this valuable information, although I wanted to stick with the "users" command. Also upvoted you, thanks.– AnonymousAug 1, 2009 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
-
-
check the re-edit, I think you'll find that the new answers address that. Jul 27, 2009 at 15:33
-
This still doesn't work, uniq only removes duplicate successive lines, you need to sort the output of who first. Jul 27, 2009 at 18:10
-
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
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
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
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 :)
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
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]}}'
:}
http://www.gnu.org/software/coreutils/manual/html_node/who-invocation.html
"who" prints information about users who are currently logged on. Synopsis:
who [option] [file] [am i]
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.
-
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. Jul 27, 2009 at 1:32
who | cut -d ' ' -f1 | uniq | wc -l
-
2This seems like a minor variation of Kim's answer, and it contains no explanation. It might be more appropriate as a comment on that answer (which requires a little more rep). The intention is that each answer provide a solution that is substantively different from what has already been contributed. Feb 1, 2019 at 7:20
Just a small modification to Avery's answer. Type the following command at the terminal. Here -u option is used in sort filter to avoid the use of uniq
filter.
who | awk '{ print $1 }' | sort -u | wc -l
-
Welcome to Super User! Please take the tour and read through the help center center. Here is a guide on How to Ask and How to Answer. Once you have sufficient reputation you will be able to comment on any post. Feb 23 at 6:49
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
-
2No, 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.– cpastFeb 21, 2013 at 5:01