How to map "console" to pertaining tty

Whereas previously, the w command output was like…

anyone@localhost:~> /usr/bin/w
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
anyone   tty7     :0               Sun12    5days 10:36   0.09s /bin/sh /usr/bin/startkde
anyone   pts/0    :0               Sun13    3.00s 24.60s  0.00s /usr/bin/w

…as of tumbleweed current at least as of 4/19/2021, it’s like…

anyone@localhost:~> /usr/bin/w
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
anyone   console  :0               Mon19    3days  0.00s  0.14s /usr/sbin/lightdm
anyone   pts/1    :0               Tue19    3.00s  2.18s  0.02s /usr/bin/w

The point is that the TTY field shows “console” instead of e.g. “tty7”. This breaks a script that was relying on real tty info.

The who command seems to have the same problem.

Is there a reliable, straight forward way to determine the actual tty pertaining to “console”?

I’m not seeing that on my Tumbleweed 20210623 system, tty’s and pts’s are being listed:

 **kosmos1:~ #** w
 11:56:06 up  3:41,  8 users,  load average: 0.55, 0.40, 0.38
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
tumb2    tty1     -                11:53    2:16   0.07s  0.07s -bash
michael  tty7     :0               08:15    3:41m  4:01   0.37s /usr/bin/startplasma-x11
michael  pts/0    :0               08:15    3:02   0.15s  0.15s /usr/bin/zsh
michael  pts/2    :0               08:15    7.00s 20.01s 19.77s ssh -X backup@localhost
michael  pts/1    :0               08:15    3:40m  0.12s  0.12s /usr/bin/zsh
michael  pts/3    :0               08:15    0.00s  0.37s  0.12s /usr/bin/zsh
backup   pts/4    ::1              10:21    7.00s  1.81s  1.81s -zsh
tumb1    tty2     -                11:45   10:44   0.06s  0.06s -bash

I do see that loginctl output lacks the desktop user’s tty (but w and who does):

**kosmos1:~ #** loginctl list-sessions                              
SESSION  UID USER    SEAT  TTY  
      2  500 michael seat0       
      3  502 sysbak        pts/4
      4 1000 tumb1   seat0 tty2  
      5 1004 tumb2   seat0 tty1  

4 sessions listed.

From some googling, I found https://www.baeldung.com/linux/monitor-keyboard-drivers, which reveals I can get from /dev/console to tty7:

**kosmos1:~ #**  cat /sys/devices/virtual/tty/console/active
tty0
​**kosmos1:~ #** cat /sys/devices/virtual/tty/tty0/active
tty7

Does that help?

I am currently seeing that, except it is with Leap 15.3 rather than with Tumbleweed. I am using “lightdm” as display manager, and I started a process with the command

xconsole &

Hmm, I rebooted, and tried again. This time, I did not run “xconsole”. And I still got that output. Perhaps it is just the way that “lightdm” works. Perhaps try a different displaymanager.

w and who show static record from /run/utmp. It is irrelevant what you run (or not run) as part of your session, it does not change utmp entry.

Perhaps it is just the way that “lightdm” works.

Not quite. The entry that we see is created by sessreg call in /etc/X11/xdm/Xstartup. utmp handling in lightdm is disabled in openSUSE (for years).

I compared with Leap 15.2 and I see the same behavior. So it does not look like regression. I do not have TW with lightdm, so it is quite possible that other display managers overwrite entry created by Xstartup or create additional entries or simply do not call default Xstartup at all.

In any case, there is no well defined API covering utmp content, so it is unwise to rely on it. Today systemd-logind provides defined API which allows you to also query terminal where session is active.