One example that works on Ubuntu 18.04 with GDM (did not try lightdm).
So for this scenario it does answer that questions. Just if someone wants to delete the answer again.... (how to find users of a display).
For GDM on Ubuntu 18.04
loginctl show-session 2 -p Display
will print Display=
although being the session using the display. The solutions below worked though.
~$ who
user :1 2020-03-02 07:28 (:1)
user tty3 2020-03-03 09:39
user pts/1 2020-03-03 10:19 (some IP)
or
~$ w
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
user tty7 :0 15:12 1:48m 1:50 0.23s /usr/lib/gnome-
user pts/2 SomeIP 16:58 1:39 0.04s 0.04s -bash
As you said since the Xserver is started by the DM it will run as root or similar user (as it is allowed to access HW devices of the seat). To allow another session to use an authority file is created for the User.
Another solution I found on Best way to identify logged-in users and their DISPLAY? (but will fail as soon as multiple users run on the same X display I think) is:
#!/bin/bash
declare -A disps usrs
usrs=()
disps=()
for i in $(users);do
[[ $i = root ]] && continue
usrs[$i]=1
done # unique names
#Use .*DISPLAY=(\w*:[0-9.]+).* if you want to see DISPLAY of form :0.1
#or localhost:10.0 too
for u in "${!usrs[@]}"; do
for i in $(sudo ps e -u "$u" | sed -rn 's/.* DISPLAY=(:[0-9]*).*/\1/p');do
disps[$i]=$u
done
done
for d in "${!disps[@]}";do
echo "User: ${disps[$d]}, Display: $d"
done
It basically queries the env of all processes of a user and saves for each display which user did belong to it. (Did work for my VNC displays too).
:0
", there is no good answer: Multiple user can be connected to:0
, and, with networking enabled on the X server, even users that don't exist on the local machine. Yes, today mostly a single user logs in to X, but that isn't how the architecture works.