How to return the currently active user/session on a graphical Linux desktop session?
On many current distributions, login sessions (graphical and non-graphical) are managed by logind
. You can list sessions using
loginctl list-sessions
and then display each session’s properties using
loginctl show-session ${SESSIONID}
or
loginctl session-status ${SESSIONID}
(replacing ${SESSIONID}
as appropriate); the difference between the two variants is that show-session
is designed to be easily parsed, session-status
is designed for human consumption. Active sessions are identified by their state; you can query that directly using
loginctl show-session -p State ${SESSIONID}
which will output
State=active
for the active session(s). The full show-session
output will tell you which user is connected, which TTY is being used, whether it’s a remote session, whether it’s a graphical session etc.
Note that logind
can have multiple active sessions, if the system is configured with multiple seats, or if there are remote sessions.
Putting this all together,
for sessionid in $(loginctl list-sessions --no-legend | awk '{ print $1 }')
do loginctl show-session -p Id -p Name -p User -p State -p Type -p Remote $sessionid
done
will give all the information you need to determine which sessions are active and who is using them, and
for sessionid in $(loginctl list-sessions --no-legend | awk '{ print $1 }')
do loginctl show-session -p Id -p Name -p User -p State -p Type -p Remote $sessionid | sort
done |
awk -F= '/Name/ { name = $2 } /User/ { user = $2 } /State/ { state = $2 } /Type/ { type = $2 } /Remote/ { remote = $2 } /User/ && remote == "no" && state == "active" && (type == "x11" || type == "wayland") { print user, name }'
will print the identifiers and logins of all active users with graphical sessions.
The LockedHint
property now indicates whether a given session is locked, so
for sessionid in $(loginctl list-sessions --no-legend | awk '{ print $1 }')
do loginctl show-session -p Id -p Name -p User -p State -p Type -p Remote -p LockedHint $sessionid | sort
done |
awk -F= '/Name/ { name = $2 } /User/ { user = $2 } /State/ { state = $2 } /Type/ { type = $2 } /Remote/ { remote = $2 } /LockedHint/ { locked = $2 } /User/ && remote == "no" && state == "active" && (type == "x11" || type == "wayland") { print user, name, locked == "yes" ? "locked" : "unlocked" }'
will also indicate whether the active session is locked or not.
Related videos on Youtube
rugk
Apparently, this user prefers to keep an air of mystery about them. … I mean, yes I do. There is also not a lot to tell… You can follow me on Mastodon or check out my GitHub or GitLab profile.
Updated on September 18, 2022Comments
-
rugk over 1 year
My question is how to get the user name on the shell, who is currently using the Linux desktop (on a "normal" desktop system, where you usually only have one active user, i.e. no server system here, but just your usual Laptop etc.). If you really want to imagine a server system, I would be fine with listing all active users.
So take e.g. the case that a script is running as root as a cron job (or similar) and I want to get the/all currently active users on the system.
I know I could use
w
orwho
orusers
to get the currently logged in users. That's fine, but that user are logged in does not mean that they are actually currently using the desktop, because in all desktop environments I know, users can switch to another user after they have logged in.
I could also uselast
to get the user who last logged in, but this is also no guarantee that this user is still the active one.So how can one do this? It is fine to provide specific solutions for different desktops environments (GNOME, KDE, …), but, of course, a cross-compatible solution is preferred.
-
roaima over 6 yearsMy Linux-based laptop can often have two users logged in on it simultaneously. It's also possible that I'm actually using a vTerminal session outside the GUI (Ctrl+Alt+F1). In your terms, which one's active?
-
wvxvw over 6 yearsIf, for example, your Linux uses
systemd
andlightdm
(a setup typical for many modern stock Linuxes), you could examine the output ofsystemctl status lightdm
and look for the line"session opened for user <user name> by <user id>"
. -
rsm over 6 yearswhat do you mean by active user? on linux you can have any number of active users.
-
rugk over 6 years@roaima Personally I only care about the graphical sessions (so no terminal) and I only care about the user, whose session is currently active, i.e. using it. As I said when using the device as a server, of course multiple users could be active, but do not imagine this case. Or, if you want, list all active users… My use case would be a simple Laptop/PC Linux installation, where usually only one user can be active…
-
rugk over 6 years@wvxvw Thanks, but this only works with lightdm. gdm e.g. does not show me this information.
-
roaima over 6 yearsIf you have a particular definition for "active", as in your comment above, please add it to your question for everyone to see. That's how this site works.
-
roaima over 6 yearsOut of interest, what are you hoping to do with the knowledge of the active user? Send them a message? If so, rather than trying to identify the correct "active" user why not send it to all currently logged in users.
-
-
Hunter.S.Thompson over 6 years"I know I could use
w
orwho
orusers
to get the currently logged in users." @dessert OP wants to know which users are active and not idle -
dessert over 6 years@Hunter.S.Thompson OP does not speak of multiple users but rather explicitly just of the user name and the active one.
w
orwho
orusers
don't do the same as whoami, plus even if OP meant another thing (and needs to clarify the question then) other people actually searching forwhoami
will probably land here. -
rugk over 6 yearsI clarified the question, so of course it is easy to get the currently active user, when the script I run runs under that user. But my script runs e.g. as a cron job in a different context, so I cannot just ask "Who am I?"…
-
dessert over 6 years@rugk Instead of downvoting my answer you should add that information to your question and furthermore clarify how a user using the desktop is defined exactly.
-
rugk over 6 yearsThat returns
gdm
in my case… -
rugk over 6 yearsIt's already there. First sentence/paragraph.
-
Bachi over 5 yearsI wonder if there's a way to find out if a graphical session is locked or 'open'. In both cases "State=active" is displayed as it seems.
-
Stephen Kitt about 5 years@Bachi see
LockedHint
, described in the updated answer. -
Vir over 4 years
loginctl lock-session 3; sleep 10; loginctl show-session -p LockedHint 3
printsLockedHint=no
. Do you know whether this hint depends on the desktop environment, or did I simply hit a bug? -
Vir over 4 yearsyes.
loginctl unlock-session 3
also unlocks it again -
Stephen Kitt over 4 years@Vir from the looks of things,
loginctl
doesn’t set the hint; the hint only changes in response to d-bus messages from the DE.