You are not logged in.

#1 2013-12-29 19:26:01

Ambrevar
Trusted User (TU)
Registered: 2011-08-14
Posts: 158
Website

[SOLVED] Finding out connected users in shell

Hi there!

This may be a simple question at first glance: who is connected and how many sessions per user are opened?  I thought a 'who' or 'w' would do it.

Sadly, it will only work with TTY sessions. Any session started from a login manager for X will not be taken into account by any of the aforementioned commands.

'loginctl list-... ' does the trick for systemd-based systems. But I'm looking for a more portable solution (BSD, other GNU, etc.).

Any thoughts?

Last edited by Ambrevar (2014-01-11 08:51:00)

Offline

#2 2013-12-29 19:32:16

karol
Archivist
Registered: 2009-05-06
Posts: 25,432

Re: [SOLVED] Finding out connected users in shell

I'm not using a login manager, so I can't test, but what about 'users'?

Offline

#3 2013-12-29 19:33:32

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 13,414

Re: [SOLVED] Finding out connected users in shell

Does who -a work?


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
You assume people are rational and influenced by evidence.  You must not work with the public much. -- Trilby
----
How to Ask Questions the Smart Way

Offline

#4 2013-12-29 21:59:55

Ambrevar
Trusted User (TU)
Registered: 2011-08-14
Posts: 158
Website

Re: [SOLVED] Finding out connected users in shell

Nope, 'who -a' does not see the loginctl sessions. It will print available TTY when only loginctl has been used:

INLOGGNING   tty1         2013-12-29 19:54               228 id=tty1
INLOGGNING   tty2         2013-12-29 19:54               230 id=tty2

Offline

#5 2013-12-29 22:02:40

Ambrevar
Trusted User (TU)
Registered: 2011-08-14
Posts: 158
Website

Re: [SOLVED] Finding out connected users in shell

'users' looked promising... But same thing: only TTY are taken into account.

Offline

#6 2014-01-01 11:20:17

durazelI
Member
Registered: 2013-12-18
Posts: 9

Re: [SOLVED] Finding out connected users in shell

who for tty's.
ls -l /dev/pts for X consoles

Offline

#7 2014-01-03 15:48:40

Ambrevar
Trusted User (TU)
Registered: 2011-08-14
Posts: 158
Website

Re: [SOLVED] Finding out connected users in shell

ls -l /dev/pts lists the virtual terminals running on every X sessions. This is not exactly what I want. A user can be connected without having any terminal running. On the other hand a user can be connected only once and use several terminals on the same session. In both cases, the result will be wrong.

Last edited by Ambrevar (2014-01-03 15:49:33)

Offline

#8 2014-01-10 12:28:56

snakeroot
Member
Registered: 2012-10-06
Posts: 104

Re: [SOLVED] Finding out connected users in shell

loginctl

EDIT: Apologies for not reading the first post carefully enough

Last edited by snakeroot (2014-01-10 14:21:36)

Offline

#9 2014-01-10 12:35:52

karol
Archivist
Registered: 2009-05-06
Posts: 25,432

Re: [SOLVED] Finding out connected users in shell

snakeroot wrote:

loginctl

From the first post:

Ambrevar wrote:

'loginctl list-... ' does the trick for systemd-based systems. But I'm looking for a more portable solution (BSD, other GNU, etc.).

Offline

#10 2014-01-10 12:50:38

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,192
Website

Re: [SOLVED] Finding out connected users in shell

How about something like

ps -eo user,tty --no-headers | sort -u

(edit: added --no-headers)

Last edited by Trilby (2014-01-10 12:52:46)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Online

#11 2014-01-11 08:50:37

Ambrevar
Trusted User (TU)
Registered: 2011-08-14
Posts: 158
Website

Re: [SOLVED] Finding out connected users in shell

Thanks, Trilby, this seems to be a good lead!

I've been playing with 'ps' a little to see what I could get. First I think the following command is equivalent and more portable:

$ ps -ax -o user='' -o tty='' | sort -u

'-e' is GNU-specific, and --no-header can be avoided by setting all header entries to the null string.

This will display all 'users', even 'root', 'polkitd', 'dbus', etc. Every X terminal will add another superfluous entry, but we can get rid of it easily:

$ ps -ax -o user='' -o tty='' | grep -v ' pts/' | sort -u

To display a list of all users with the number of started sessions:

$ ps -ax -o user='' -o tty=''| sort -u | awk '$0 !~ / pts\// {x[$1]++} END{for(i in x) print i, x[i]}'
doe 2
polkitd 1
dbus 1
root 3

To display the number of started session for the specified user:

$ ps -o user='' -o tty='' -u $USER | sort -u | grep -cv ' pts/'

Do not use '-U $USER', since it will display one root entry per X session (X euid is $USER but X ruid is root).

I've not tested the above commands on BSD systems, but according to the 'ps' man page from FreeBSD, this should work well. But it may fail if the TTY are not reported the same way. It also relies on the pts names.

Don't know if anyone can come up with a better suggestion, but for now I'm quite satisfied with the result, so I'll mark it as a 'solved'. Thanks again!

Last edited by Ambrevar (2014-01-11 08:53:36)

Offline

Board footer

Powered by FluxBB