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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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.
Method 3
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0