Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

I'm looking for a simple way to determine the number of currently logged in users, using only the filesystem and basic bash functions (instead of using binaries).

The uptime binary appears to be reading from /var/run/utmp according the its source code, but /var/run/utmp doesn't seem to update in real time (it shows tty7 and pts/1-5 for my user, but I'm only on tty7 and pts/5 at the moment). However, the uptime binary correctly shows "2 users".

Note: Instead of relying on uptime or getutent() in C, I'm trying to better understand how the underlying system works.

What's the best place to find a current, accurate number of users logged into the system?

share|improve this question
The utmp man page contains some background information. In my opinion, it is not possible (or at least really painful) to interpret utmp files with only basic shell functions. There is also who which shows the current allocated terminals. – jofel Feb 7 '13 at 9:34
I'm actually writing some Python to decode utmp, but it's still very painful. I'm trying to avoid relying on subprocess calls to existing binaries, and force myself to use the actual Linux filesystem contents to gather data in order to learn more about Linux/procfs/utmp. – wroberts Feb 7 '13 at 22:44

You can see how many pty's are allocated by looking at /proc/sys/kernel/pty/nr, however that won't necessarily be how many people are logged in.

For instance, on one of my servers I am currently the only person logged in but there are 7 pty's in use because of users running screen.

share|improve this answer
This is very helpful - I'll play with it tonight. Thanks! – wroberts Feb 7 '13 at 22:43

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.