Home Command line utilities How To Switch Between TTYs Without Using Function Keys In Linux
switch between TTYs

How To Switch Between TTYs Without Using Function Keys In Linux

By sk
10484 Views

This brief guide describes how to switch between TTYs without using function keys in Linux operating systems. Before going further, we will see what TTY is. As mentioned in an answer in AskUbuntu forum, the word TTY came from TeleTYpewriter. Back in the early days of Unix, the user terminals connected to computers were electromechanical teleprinters or teletypewriters( tty in short). Since then, the name TTY has continued to be used for text-only consoles. Nowadays, all text consoles represents virtual consoles, not physical consoles. The TTY command prints the file name of the terminal connected to standard input.

Switch Between TTYs In Linux

By default, there are 7 ttys in Linux. They are known as tty1, tty2..... tty7. The 1 to 6 ttys are command line only. The 7th tty is GUI (your X desktop session). You can switch between different TTYs by using CTRL+ALT+Fn keys. For example to switch to tty1, we type CTRL+ALT+F1.

This is how tty1 looks in Ubuntu 18.04 LTS server.

tty1

tty1 in Ubuntu

If your system has no X session, just type Alt+Fn key. You don't need to use CTRL.

In some Linux editions (Eg. from Ubuntu 17.10 onwards), the login screen now uses virtual console 1 . So, you need to press CTRL+ALT+F3 up to CTRL+ALT+F6 for accessing the virtual consoles. To go back to desktop environment, press CTRL+ALT+F2 or CTRL+ALT+F7 on Ubuntu 17.10 and later.

You know now we can easily switch between TTYs using CTRL+ALT+Function_Key(F1-F7). However, if you don't want to use the functions keys for any reason, there is a simple command named "chvt" in Linux.

The "chvt N" command allows you to switch to foreground terminal N, the same as pressing CTRL+ALT+Fn. The corresponding screen is created if it did not exist yet.

Let us see print the current tty:

$ tty

Sample output from my Ubuntu 18.04 LTS server.

tty command output

Print current tty

Now let us switch to tty2. To do so, type:

$ sudo chvt 2

Remember you need to use "sudo" with chvt command.

Now, check the current tty using command:

$ tty

You will see that the tty has changed now.

Similarly, you can switch to tty3 using "sudo chvt 3", tty4 using "sudo chvt 4" and so on.

Chvt command can be useful when any one of your function keys doesn't work.

To view the total number of active virtual consoles, run:

$ fgconsole
2

As you can see, there are two active VTs in my system.

You can see the next unallocated virtual terminal using command:

$ fgconsole --next-available
3

A virtual console is unused if it is not the foreground console, and no process has it open for reading or writing, and no text has been selected on its screen.

To get rid of unused VTs, just type:

$ deallocvt

The above command deallocates kernel memory and data structures for all unused virtual consoles. To put this simply, this command will free all resources connected to the unused virtual consoles.

For more details, refer the respective command's man pages.

$ man tty
$ man chvt
$ man fgconsole
$ man deallocvt

Suggested read:


And, that's all for now. Hope this was useful.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

1 comment

Eric HAMON August 22, 2018 - 7:12 pm

Hi,
Great to learn that. I did’nt know ‘chvt’ command.
I rather use ‘screen’ or ‘tmux’ to stay in the same tty. sudo is useless in this case. (screen and tmux have to be installed before, i admit)
Regards

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More