6

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 or who or users 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 use last 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.

closed as unclear what you're asking by Romeo Ninov, Anthon, Ulrich Schwarz, roaima, G-Man Sep 27 '17 at 19:59

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    My 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? – roaima Sep 27 '17 at 8:57
  • 1
    If, for example, your Linux uses systemd and lightdm (a setup typical for many modern stock Linuxes), you could examine the output of systemctl status lightdm and look for the line "session opened for user <user name> by <user id>". – wvxvw Sep 27 '17 at 11:37
  • 1
    what do you mean by active user? on linux you can have any number of active users. – rsm Sep 27 '17 at 15:09
  • @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 Sep 28 '17 at 9:33
  • @wvxvw Thanks, but this only works with lightdm. gdm e.g. does not show me this information. – rugk Sep 28 '17 at 9:39
11

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.

  • I 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. – Bachi Feb 11 at 17:17
  • @Bachi see LockedHint, described in the updated answer. – Stephen Kitt Apr 24 at 14:15
0

I'm using a bash function like the following

function Xowner() {
    for pid in $(ps -houid --ppid $(ps -hoppid $(pgrep X))) ; do
        [ "$pid" = "0" ] && continue
        id -n -u $pid
        break
    done
}

The intention with that function is basically to pick "the first" non-root UID of the sibling processes of the Xserver. See man ps for details. I suppose, if your use case potentially involves multiple X servers, you'll need a better focus on which of them to start from.

  • That returns gdm in my case… – rugk Sep 28 '17 at 10:09
0

On most Unix-like systems (says Wikipedia) the command

$ whoami

gives you the name of the current user running the command, e.g.

$ whoami
dessert

$ sudo whoami
root
  • 1
    "I know I could use w or who or users to get the currently logged in users." @dessert OP wants to know which users are active and not idle – Hunter.S.Thompson Sep 27 '17 at 10:25
  • 1
    @Hunter.S.Thompson OP does not speak of multiple users but rather explicitly just of the user name and the active one. w or who or users 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 for whoami will probably land here. – dessert Sep 27 '17 at 10:43
  • I 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?"… – rugk Sep 28 '17 at 9:41
  • 1
    @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. – dessert Sep 28 '17 at 10:00
  • 1
    It's already there. First sentence/paragraph. – rugk Sep 28 '17 at 11:33

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