fgconsole
Print or set the active virtual console
TLDR
Print current terminal number or serial if the terminal is serial
Print the next unallocated virtual terminal
SYNOPSIS
fgconsole [options]
PARAMETERS
-h, --help
Display usage help and exit
-V, --version
Output version information and exit
DESCRIPTION
The fgconsole command outputs the number (1-based) of the currently active foreground virtual console on Linux systems with virtual terminal support.
Linux kernels provide multiple virtual consoles (VTs), typically accessed via Ctrl+Alt+F1 to F12, mapped to /dev/tty1 through /dev/tty12. The foreground console is the one shown on the physical display. Graphical sessions like X11 or Wayland usually run on higher-numbered VTs (e.g., VT7).
fgconsole determines this by opening /dev/tty0 (representing all VTs) and issuing the kernel ioctl TIOCL_GETFGCONSOLE. It prints a single integer to stdout, such as 1 for tty1 or 7 for a graphical session.
This utility is handy in shell scripts for console-aware logic, like applying text-mode settings only on low-numbered VTs or logging display status. Part of util-linux, it fails gracefully if VTs are unavailable (e.g., in SSH, containers, or headless systems).
CAVEATS
Requires read/write access to /dev/tty0 (usually granted to 'tty' group members). Fails on systems without VT support, like Docker containers, WSL, or remote sessions. Output undefined if no FG console active.
EXAMPLE
$ fgconsole
7
$ sudo chvt 3 && fgconsole
3
EXIT STATUS
0 if successful, 1 on error (e.g., no VT access or ioctl failure).
HISTORY
Introduced in util-linux around 2000s by Andi Kleen for VT management. Evolved with kernel VT ioctl support; current in util-linux 2.40+ uses sysfs fallback if ioctl unavailable.


