Find the Active Linux Virtual Terminal with fgconsole

Your desktop has frozen and someone tells you to switch virtual terminals, but fgconsole is what tells you which one you are already on. It also reports when the machine is using a serial console instead of a screen. The examples use fgconsole from kbd 2.6.4-2ubuntu2, installed here as fgconsole 2.6.4. Allow about five minutes. You need a shell on a Linux system with the kbd package installed.

This is a read-only check. It does not switch terminals, stop a service or change console configuration. You normally do not need sudo.

1. Check which command you are using

Confirm the executable and package version before relying on its output. These commands are ordinary, unprivileged checks:

$ command -v fgconsole
/usr/bin/fgconsole
$ dpkg-query -W -f='${Package} ${Version}\n' kbd
kbd 2.6.4-2ubuntu2
$ fgconsole --version
fgconsole from kbd 2.6.4

The version string describes the kbd utilities, while the package query describes the distribution package. Other distributions may report the same utility version through a different package manager. If command -v prints nothing, install kbd through your normal system-management process rather than copying a binary from another host.

Checkpoint: you should know the path and version of the command that will run. If the version differs, read that host's fgconsole(1) page before putting an option into a script.

2. Print the active virtual terminal number

Run fgconsole with no option:

$ fgconsole
3

If the active console device is /dev/tty3, the command prints 3 on standard output. The number is suitable for a shell variable or a status message:

active_vt=$(fgconsole) || {
    status=$?
    printf 'fgconsole failed with status %s\n' "$status" >&2
    exit "$status"
}
printf 'active virtual terminal: %s\n' "$active_vt"

Do not parse a device name from the output. The documented result is the number only, so use the value as a number or display it alongside /dev/tty when you need a device path.

3. Understand a serial-console result

A system can have a serial console active instead of a virtual terminal. In that case, the command prints the word serial:

$ fgconsole
serial

This is a successful result with a different meaning. A script that assumes every successful output is an integer will mishandle it. Branch on the value before constructing a path:

case "$(fgconsole)" in
    serial) printf '%s\n' 'The active console is serial.' ;;
    ''|*[!0-9]*) printf '%s\n' 'Unexpected console result.' >&2; exit 1 ;;
    *) printf 'The active VT is /dev/tty%s\n' "$(fgconsole)" ;;
esac

That compact example runs the command twice in the numeric case. For a script where the console state can change or where command failures need precise reporting, capture the result once, save the exit status, then inspect the saved value. Do not confuse the word serial with a terminal number.

4. Find the next unallocated virtual terminal

Use --next-available when you need the number of the next unallocated VT rather than the active one:

$ fgconsole --next-available
8

The kbd manual describes a common arrangement in which six VTs are allocated and number 7 is used for X, making 8 the next available number. That is an example, not a fixed promise. The result depends on the current console allocation on your host. Do not reserve the number merely because it appeared in an earlier check.

This option reports a candidate; it does not allocate or switch to that VT. If another process allocates a terminal after the check, the result can become stale. Treat it as advisory in scripts and verify the state again immediately before an operation that depends on it.

5. Diagnose the most confusing failure

fgconsole needs access to the Linux console. A shell reached through SSH, a container, a graphical terminal emulator or a non-console service may not have a file descriptor for the active console. On this machine, running it from such a shell reports:

$ fgconsole
Couldn't get a file descriptor referring to the console.
$ printf 'exit status: %s\n' "$?"
exit status: 1

The wording is a diagnostic, not an active VT number. First check the exit status immediately, before running another command. Then decide whether the check belongs on a local Linux virtual console rather than an SSH session. Adding sudo may help only when permissions are the specific problem; it cannot give a container or remote pseudo-terminal the host console device. Avoid granting broad device access just to make a monitoring check pass.

Ask for built-in usage when checking a different kbd release:

$ fgconsole --help
Usage: fgconsole [option...]
Options:
  -C, --console=DEV      the console device to be used.
  -n, --next-available   print number of next unallocated VT.
  -V, --version          print version number.
  -h, --help             print this usage message.

Options exposed by the installed command include --help, --version and --next-available. Keep scripts to options documented by the installed manual page and verify them after package upgrades.

Done means