2

In linux I can use the key combinations Alt-Ctrl-F1,Alt-Ctrl-F2... to switch to different tty1 as I could use man chvt.

using the command tty I also get displayed the teletype/linux virtual console I am on.

However if I am in xterm or in gnome-terminal tty will display the relevant pseude-terminal.

Given all that:

  • How can I tell which is the currently "active" tty (meaning it being displayed on the screen)?

This would be somewhat the solution (however it seems contorted and relies on loginctl logind, there must be a non-systemd to find out):

for sessionid in $(loginctl list-sessions --no-legend | awk '{ print $1 }'); do loginctl show-session -p State -p TTY  $sessionid; done | grep "State=active" -B1 | head -n 1 | sed 's/.*=//g'
fraleone
  • 737
  • 2
  • 9
  • 21
  • I'm sorry, but I'm not sure I understand the question. Are you asking which of the virtual ttys (`tty1` through `tty12`) is the one being shown to the console user? If so, what would you expect to be reported when a GUI is running on (say) `tty8`? What if the GUI is running remotely as an X Windows session over XDMCP and the user isn't local? – roaima Nov 28 '19 at 11:28
  • 1
    @roaima thanks for the comment. There will be only local GUIs on ttys. I would like to get the tty for which `loginctl show-session -p State {SESSION_ID}` would yield `State=active`. Hence the `tty` on which either `wayland compositor` or `Xorg server` are running on if they are displayed. No remote GUIs. – fraleone Nov 28 '19 at 11:32
  • @KamilMaciorowski **:) Yes,** that seems to work. `cat /sys/devices/virtual/tty/tty*/active` yields `tty2` which is the tty wayland-compositor is running on currently – fraleone Nov 28 '19 at 12:16

1 Answers1

3
cat /sys/devices/virtual/tty/tty0/active

tty0 refers to the current virtual console (compare this answer). By reading /sys/devices/virtual/tty/tty0/active you can learn which console this is.

"Current" means what you see, not where cat is running. E.g. if you start this loop

while sleep 1; do
   cat /sys/devices/virtual/tty/tty0/active
done

let in run, manually switch to another console, wait few seconds and switch back, then you will see the other console was reported when you looked at it.

Kamil Maciorowski
  • 19,242
  • 1
  • 50
  • 94
  • A while back I wrote improved manual pages covering this: http://jdebp.uk./Proposals/linux-kvt-manual-pages.html http://jdebp.uk./Softwares/nosh/guide/commands/linux-vt.xml – JdeBP Nov 28 '19 at 16:08