chvt Command in Linux



If your graphical interface is unresponsive, you can use chvt to switch to a non-graphical terminal and perform troubleshooting steps. For users who rely on screen readers or other assistive technologies, chvt can be used to switch to a terminal with the appropriate software running.

The chvt command in Linux is a handy tool for navigating between virtual consoles, also known as TTYs (TeleTYpewriters). These virtual consoles act like separate terminal screens you can switch between, each potentially running different processes or serving different purposes.

Table of Contents

Here is a comprehensive guide to the options available with the chvt command −

Understanding chvt Command in Linux

The primary function of chvt is to switch you from your current virtual console to a different one.

Unlike some commands with a multitude of flags and arguments, chvt offers a very focused set of capabilities. The real power of chvt lies in specifying the target virtual console number as an argument (without any options). For instance, the command chvt 7 would switch you to virtual console number 7.

The core functionality of chvt lies in specifying the target virtual console number as an argument (without any options). For instance, chvt 7 would switch you to virtual console number 7.

Install chvt Command in Linux

The chvt command is most likely already installed on your Linux system. It's a core utility included in most distributions by default. Here's why you probably don't need to install it −

To confirm if chvt is present, open a terminal and run −

chvt --version
Install chvt Command in Linux

If it's installed, this command should display the version information for chvt.

In the rare case where chvt is missing, the installation method depends on your Linux distribution's package manager −

For Debian/Ubuntu

These distributions typically include chvt within the util-linux package. If it's missing for some reason, you can install it using −

sudo apt install util-linux

For RedHat/CentOS/Fedora

On these RedHat-based systems, chvt might be part of the coreutils package. You can install it with −

sudo yum install coreutils  # For yum-based systems (CentOS/RHEL)
sudo dnf install coreutils  # For dnf-based systems (Fedora)

Note − Refer to your distribution's documentation for the specific package manager commands to install chvt (if needed). In most cases, you won't need to install chvt separately as it's a core utility preinstalled on Linux systems.

How to use chvt Command in Linux?

The chvt command is simple yet powerful, and its syntax is straightforward. It's important to note that this command requires root privileges to execute, ensuring that only authorized users can switch between terminals.

Once you've confirmed or installed chvt, you can use it to change the group ownership of files and directories. The chvt command in Linux has a very simple syntax −

chvt <number>

In the above syntax,

  • chvt − The command itself is used to switch between virtual consoles.
  • <number> − This is the required argument that specifies the virtual console number you want to switch to.

Valid numbers typically range from 1 to 6 (although it can vary depending on your system configuration).

Note − Remember that the typical range for virtual consoles is usually 1 to 6 (though it can vary depending on your system configuration). If you provide a number outside this range, you might end up on a blank screen. To return from such a screen, you can often use Alt + F7 (the specific key combination might differ).

Here's a breakdown of the different options available with chvt. The options available with chvt are −

Options Descriptions
-V or --version This option displays the version information of the chvt program and exits the command.
-h or --help This option displays a brief help message explaining the syntax and usage of chvt and then exits.

Important − While chvt typically doesn't require root privileges to function, some environments might have restrictions. It's generally safe to try using it without sudo.

Let's delve into some practical examples of how the chvt command can be utilized −

Examples of chvt Command in Linux

Let’s explore a few examples of chvt command in Linux −

  • Switching to a Specific Virtual Console
  • Prints the version information of the chvt program
  • Displays a Brief Help Message
  • Automating Terminal Switches

Switching to a Specific Virtual Console

This is the most common usage of switching to a specific virtual console. Replace <number> with the virtual console number you want to switch to. The valid numbers typically range from 1 to 6 (although it can vary depending on your system configuration) −

chvt 2 

This command would switch you from your current virtual console to virtual console number 2:

Switching to Specific Virtual Console

Note − Replace 2 with the virtual console number you want to access. Commonly, these numbers range from 1 to 6, although it can vary depending on your system configuration.

To switch to TTY 1

chvt 1

To switch to TTY 3

chvt 3

Prints the version information of the chvt program

chvt -V is a shortcut for chvt --version. It's a command-line instruction in Linux that prints the current version of the chvt program itself −

chvt -V
Prints version information of the chvt program

Displays a Brief Help Message

Users can display a brief help message explaining the syntax and usage of the chvt command and then exit. For this, typing chvt --help in your Linux terminal brings up a detailed explanation of how to use the chvt command. This includes available options and proper usage for switching between virtual terminals.

chvt --help
Displays a Brief Help Message

Note − chvt itself doesn't have a built-in way to directly return to the previous console.

However, some desktop environments or terminal emulators might provide keyboard shortcuts for this purpose. Common shortcuts include −

Ctrl + Alt + Left Arrow
Alt + Left Arrow (less common)

Automating Terminal Switches

The chvt command can be combined with other Linux commands to create more complex scripts. For example, you could create a script that cycles through all available terminals.

Suppose you have a script that needs to execute a command in a different terminal. You could use chvt to switch to that terminal, run the command, and then switch back. For example −

#!/bin/bash
chvt 5 && Hello
chvt 1

This script switches to terminal 5, runs Hello, and then returns to terminal 1.

Alternatives of chvt Command in Linux

While chvt is the traditional way to switch between virtual consoles (VTs) in Linux, there are a couple of alternatives you might find useful depending on your environment −

1. Keyboard Shortcuts

This is often the most convenient method, especially if you frequently switch between consoles. The specific key combinations might vary depending on your system configuration and desktop environment.

Here are some common shortcuts −

  • Ctrl + Alt + <Fn Key> (where <Fn Key> is a function key numbered 1 to typically 6) - Switch to the corresponding virtual console (e.g., Ctrl + Alt + F2 for VT2).
  • Alt + <Fn Key> (less common) - Similar to the above, but might be used in some environments.

2. TTY Login

If you're comfortable with the command line, you can directly log in to a specific virtual console using the login command followed by the VT number. This method is particularly helpful if you're working remotely via SSH and need to access a specific console on the server.

Conclusion

In essence, chvt is a simple yet valuable tool for quickly switching between virtual consoles in Linux, providing a way to manage multiple terminal sessions efficiently. If multiple users are logged in on different virtual consoles, chvt will only affect the session of the user who issued the command.

The most common way to switch between VT consoles is using the keyboard shortcut Ctrl + Alt + F<N> (where N is the virtual terminal number). This is generally more convenient than using chvt.

Advertisements