4

How to determine, as root, which user owns X display :0? (on a Fedora 18 system with defaults)

Clarification: I want to know which user has original permissions to connect to :0 and grant other users permission to connect. Per current answers below, it seems that root owns the X process but the user who launched it may have permissions.

4
  • 3
    Is your question actually is "who opened a session on display :0?" ? If this is the case, you should consider edit your question. Here, I understand the question as "who launched the process that created the display :0?".
    – lgeorget
    May 4 '13 at 3:11
  • And if the question is "which user is is connected to display :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.
    – dirkt
    Jun 15 '19 at 13:48
  • @lgeorget no, that's not the question. I have added a clarification Sep 18 '19 at 9:24
  • @dirkt added a clarification to my question Sep 18 '19 at 9:24
1

ps -ef|grep X will give you the info.

For example, on my computer:

user001  2721  2705  0 02:35 tty1     00:00:00 xinit /home/user001/.xinitrc -- /etc/X11/xinit/xserverrc :0 -auth /home/user001/.serverauth.2705
root      2722  2721 13 02:35 tty7     00:10:10 /usr/bin/X -nolisten tcp :0 -auth /home/user001/.serverauth.2705
user001  3475  3462  0 03:51 pts/0    00:00:00 grep X

Here, we can see that user001 launched xinit and uses the display :0. However, root is the owner of the X process.

On most installations, users don't use the xinit command (or startx which internally calls xinit). The X server is launched by root during the bootup process. In that case, root is the "owner" of the display.

4
  • no xinit shows up on my system, fedora 18 with defaults May 4 '13 at 2:41
  • Ok, but you see a X process owned by root? That means that the X server is launched and owned by root. So, you have your answer. I'll edit my answer to deal with that case.
    – lgeorget
    May 4 '13 at 3:05
  • hmm.. yes, but i did not login as root on the graphical user and the display is not owned by root. i think the "-auth /home/user0001/..." line might help though May 4 '13 at 4:32
  • 1
    If your question is "how to know who is logged in", try the w command. And please reformulate your question, it's a bit ambiguous ;-).
    – lgeorget
    May 4 '13 at 12:09
1

loginctl from systemd to the rescue:

loginctl --no-legend list-sessions \
  | cut -d ' ' -f 1 \
  | while read sessionid; do \
    [ $(loginctl --property Type --value show-session $sessionid) = 'x11' ] \
      && loginctl --property Name --value show-session $sessionid; \
  done

Get the list of session IDs, see which one is of type x11 and print the user name. For Wayland the type is wayland and for MIR is mir according to the logind D-Bus API Documentation.

Interactive use:

[ciupicri@titan /]$ loginctl list-sessions 
SESSION  UID USER     SEAT  TTY
      2 5000 ciupicri seat0    
      4 6000 somebody             
      9 6000 somebody             

3 sessions listed.
[ciupicri@titan /]$ loginctl show-session 2
Id=2
User=5000
Name=ciupicri
Timestamp=Thu 2019-09-12 16:24:12 EEST
TimestampMonotonic=46881112
VTNr=1
Seat=seat0
Display=:0
Remote=no
Service=lightdm-autologin
Desktop=xfce
Scope=session-2.scope
Leader=2072
Audit=2
Type=x11
Class=user
Active=yes
State=active
IdleHint=no
IdleSinceHint=0
IdleSinceHintMonotonic=0
LockedHint=no
0

To answer your clarification: It depends.

It's possible for a user to start an X server.

However, most distros use a display manager (DM) to start the X server. This is a system service owned by root, which starts the X server as root, display a login screen, and then grant this particular user access to this display (e.g. :0) via xauth.

As to "which user can grant other users permissions to connect": Again, it depends. By default, modern distros use xauth, and restrict the X server to local user. Then, any user who is already connected can use xauth to obtain a valid MIT authorization cookie, and pass this cookie on to another user, with then in turn can connect after adding this cookie.

Unless the distro has taken additional measures to make sure only one user can connect the unix domain socket that corresponds to that display (I wouldn't be surprised if some distros do that).

1
  • I think Xorg would also listen on an abstract Unix domain socket, so anyone can connect to that socket? Although Xorg may refuse to give any further X11 protocol response, but connect() syscall will succeed for any user. Sep 18 '19 at 11:32
0

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).

-1

This is the exact command:

ps ho user $(pgrep X)
2
  • 1
    not tested but seems intelligent and in the right ballpark. upvoting to negate an uncommented downvote. Sep 18 '19 at 9:21
  • Does not work for me on a Debian system with Lightdm and Mate. The owner of the Xorg process is root, so the result is always root Mar 14 '21 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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