Linux Virtual Consoles

Last updated — Aug 06, 2023
First published — Aug 06, 2023
#init

Linux, text mode, virtual console, terminal, tty, ctrl+alt+f keys.

Table of Contents

Introduction

In a typical modern GNU/Linux distribution, everything has a GUI. Both the installer and the installed system automatically boot into the graphical environment (called X11, or X).

However, many GNU/Linux distributions offer a text-mode installer, and almost all of them offer a textual (non-graphical) interface out of the box. That just typically isn’t visible because the X display starts automatically during boot and hides the text consoles.

This article explains virtual consoles in Linux and how to use them.

Virtual Consoles

Virtual consoles are equivalent to hardware terminals, but implemented in software in the Linux kernel. Keyboard and mouse are are always redirected to the console which is currently active, and the output is always directed to the screen attached to the computer.

Each console provides a standalone user terminal, independent of other consoles.

On Linux there are 24 separate consoles that can be accessed easily – the first 12 on keys Alt+F1 to Alt+F12, and the second 12 on keys AltGr+F1 to AltGr+F12.

The actual number is configurable, and usually not that high. The standard used to be to configure 6 virtual consoles on numbers 1 to 6, leaving the rest uninitialized. Nowadays with graphics being prevalent, the standard may be to start only one or two consoles on numbers 1 and 2.

Both virtual consoles and X displays use the same interface and take up available numbers from 1 onwards. If the first 6 VTs are configured for consoles, then when when the first X display starts it looks for the first free slot and starts on tty7.

When graphics is started, if the process does not have the permission to open a new console, it reuses the text console, making the virtual console inaccessible while graphics is running.

Try it

From your Linux GUI, press Ctrl+Alt+F1 to switch to the 1st virtual console.

You should be greeted with “Login: " prompt on a black background. (Text terminals which you might have seen in the X environment are very similar to consoles, except that they already run authenticated and so do not need to ask for a login and password.)

You can type your username and password in the console and you will be logged in into the system. Then you can type w or who -a to see all logged in users. In the output, compare the line related to your console session with lines for other terminals you may have running in X.

Switching Consoles

As a local user, you can switch between consoles using Alt+F keys. For example, try keys from Alt+F1 to Alt+F8. It is likely that one of them will get you back to the graphical environment. (Once there, you need to additionally press Ctrl, e.g. Ctrl+Alt+F1, to get back to consoles.)

If a particular virtual console has been allocated, you will see a program running on it, usually some version of getty that asks for login and password. If a console is uninitialized, switching to it would just display an empty, black screen with no cursor.

You can circle between consoles using keys Alt+ArrowLeft and Alt+ArrowRight. This method only rotates between allocated consoles; uninitialized ones aren’t in the list and aren’t visited.

To make things interesting, you could create additional users with a command such as sudo adduser USERNAME, and then log in on different consoles as different users. Then type who -a to check the results.

Console Management

In addition to keys for navigation between consoles, there are also commands available as follows:

  • openvt initializes a new console and runs specified program on it

  • chvt changes current foreground terminal, i.e. switches to the specified VT number, similar to [Ctrl+]Alt+F keys

  • deallocvt removes all consoles that don’t have active programs running on them

  • clear_console clears the console screen, similarly to command clear or key shortcut Ctrl+l

  • fgconsole or tty report the number or name of the currently active console

Console Settings

The fun with virtual consoles does not end there. The following commands, mostly from the package kbd, affect or query console look and input:

  • showconsolefont and setfont manage console font and size

  • dumpkeys and loadkeys manage keyboard translation table, e.g. us for English and de for German

  • showkey, getkeycodes, setkeycodes, and setmetamode manage kernel scancode-to-keycode mappings

  • kbdinfo, kbd_mode, and kbdrate manage console status, keyboard mode, and repeate rate

  • setupcon and setvtrgb set font, keyboard, and color map on multiple consoles (or current console) at once

  • setleds sets the keyboard flags and LEDs

  • vcstime requires root privileges and keeps date and time displayed on the top-right of the console

  • screendump dumps the visible part of the screen, usually to a file, such as with screendump > /tmp/myscreen

Terminal Settings

In addition to the above two sections of console-specific commands, consoles are also subject to standard terminal commands, such as:

  • setterm sets terminal attributes, such as setterm --blank 0 to disable screensaver, or setterm --blength 0 to disable beeps

  • stty changes or prints terminal line settings. It is a somewhat lower-level command

Console Mouse

As funny and useful as it sounds, there are programs that implement mouse support in the console.

The standard program for console mouse is called GPM (General Purpose Mouse). You can install it, and if you are asked any questions by Debconf, simply confirm default answers.

sudo apt install gpm

After installation, start moving the mouse and you should see the cursor moving over the screen.

You can copy and paste just like when using the PRIMARY buffer in X. Left mouse button is used for copying, and middle button for pasting.

Session Recording

It is possible to record console and terminal sessions using the following basic programs:

  • script starts recording your whole session to a file. The file includes control characters so it is not pure ASCII text, but it is still comfortably viewable in a text editor later if needed. As usual, press Ctrl+d to signal End of Input and end the script session.

  • scriptreplay is scripts’s opposite pair, which allows replaying script sessions as if they are happening in real time. This command requires both the script and timing file to replay the session. In other words, script should be started with e.g. script -ttiming, and then it can be replayed with scriptreplay timing.

A more advanced (and Internet-enabled) program for the same purpose as script is called asciinema.

Writing to Consoles

In Unix almost all devices are represented as files, and so are virtual consoles. Typing tty will print the name of the current console device file.

We could mention two possible uses of this feature.

System Logs

It is often useful to dedicate a certain console, say tty12, as the one to which all system logs will be printed. This can be done by installing rsyslog (if it is not already installed), updating /etc/rsyslog.conf to send a copy of all messages to console 12, and restarting the rsyslog service:

sudo apt update
sudo apt install rsyslog
echo "*.* -/dev/tty12" > /etc/rsyslog.d/tty12.conf
invoke-rc.d rsyslog restart

Pressing Alt+F12 would switch to /dev/tty12 and system logs should be visible on it.

You could also go back to an interactive terminal and run logger "This is a TEST.". Then switching to tty12 would show that message appearing in system logs.

Data Exchange

All consoles on which you are logged in exist as special device files and run under your username. That means it is possible to exchange text between them by using the standard read and write system calls.

For example, log in to two consoles, tty1 and tty2.

Then on tty1 type:

echo "This is a test" >> /dev/tty2

After switching to tty2, you will see the message printed there. (This should not to be confused with the previous example for syslog. The underlying logic is completely different.)

In the same way that you can write to your other consoles, you can also read from them. For example, on tty1 you could write cat /dev/tty1 to return all input back to yourself. (You can exit with Ctrl+c, as usual.) Similarly, you could run cat /dev/tty2 to read any input typed on the 2nd console. However, reading is not as useful as writing since it basically hijacks the normal input into the terminal and just prints it to the screen, making the terminal not having its usual function.

Misc Examples

Since consoles are files, possible uses multiply indefinitely. For example:

You can keep standard output of programs on tty1 but print errors on tty2. E.g. you could run find which will surely produce both results and errors, and then watch normal results printed directly to the current terminal, and errors redirected to tty2:

chvt 1
find / 2> /dev/tty2

Or, you could run a command with output going to the current terminal while also being copied to another console:

chvt 1
find / | tee /dev/tty2

These final examples aren’t overly useful, but help point out that consoles are compatible with all other Unix interfaces which use read and write system calls.