What is a TTY in Linux? (and how to use the tty command).
What does the tty command do? Print the name of the terminal you are using. TTY stands for “teletypewriter.” What is the story behind the command’s name? That requires a bit more explanation.
19th century teleprinters
In the 1830s and 1840s, the machines known as . These machines could send typed messages “over the wire” to distant locations. The messages were written by the sender on a kind of keyboard. They were printed on paper at the receiving end. They were an evolutionary step on , which had previously been based on , and similar code.
Messages were encrypted and transmitted, then received, decoded, and printed. Various techniques were used to encode and decode the messages. The most famous and one of the most prolific was patented in 1874 by , for whom it is named. His character encoding scheme is older than 89 years.
Baudot coding eventually became the closest thing to a standard in teleprinter coding and was adopted by most manufacturers. Baudot’s original hardware design had only five keys, similar to the keys on a piano. The operator was required to learn a particular key combination for each letter. Eventually, the Baudot coding system was coupled to a traditional keyboard layout.
To mark that advance, the machines were called teletypes. This was reduced to teletypes and eventually TTY. So that’s where we get the TTY from, but what does telegraphy have to do with computing?
ASCII and telex
When ASCII arrived in 1963, it was adopted by teletype manufacturers. Despite the invention and widespread use of the telephone, teletypes were still going strong.
It was a worldwide teletype network that allowed written messages to be sent to the entire world. They were the main means of transmission of written messages in the period after World War II until the 1980s.
Computers were also evolving. They were becoming capable of interacting with users in real time and supporting multiple users. The old batch method became insufficient. People did not want to wait 24 hours or more for their results. Making stacks of punch cards and waiting overnight for the results was no longer acceptable.
People needed a device that would allow them to enter instructions and receive the results. People wanted efficiency.
The repurposed ticker
The teletype was the perfect candidate for an input/output device. After all, it was a device designed to allow messages to be written, encoded, sent, received, decoded, and printed.
What did the teletype care if the device on the other end of the connection wasn’t another teletype? As long as it spoke the same coding language and could receive messages and send messages back, the teletype was happy.
And, of course, he used a more or less standard keyboard.
Hardware emulated tickers
Teletypes became the default means of interacting with the large mini and mainframe computers of that time.
They were eventually replaced by devices that emulated those electromechanical machines that used electronics. These had (CRT) instead of paper rolls. They did not tremble when delivering answers from the computer. They allowed previously impossible functions, such as moving the cursor around the screen, clearing the screen, making text bold, etc.
The was an early example of a virtual teletype, and an ancestor of the famous . Millions of DEC VT100s were sold.
Software emulated tickers
On the Linux desktop environment and other Unix-like operating systems such as macOS, the terminal window and applications such as and are examples of virtual tickers. But these are completely emulated in software. They are called pseudo-teletypes. This came down to PTS.
And that’s where tty comes in.
What can tty tell us?
On Linux, there is a pseudo-teletyper multiplexer that handles the connections of all pseudo-Terminal Window Teletypes (PTS). The multiplexer is the master and the PTS are the slaves. The multiplexer is addressed by the kernel via the device file located at /dev/ptmx.
The tty command will print the name of the device file that your slave pseudo-teletype is using to interact with the master. And that, effectively, is the number of your terminal window.
Let’s see what tty reports for our terminal window:
The response shows that we are connected to the device file at /dev/pts/0.
Our terminal window, which is a software emulation of a teletypewriter (TTY), interfaces with the pseudo-teletype multiplexer as a pseudo-teletypewriter (PTS). And it turns out to be the number zero.
The silent option
The -s (silent) option causes tty to generate no output.
However, it produces an output value:
0: Whether the standard input is coming from a physical, emulated, or TTY device.
1 – If the standard input is not coming from a TTY device.
2: Syntax error, wrong command line parameters were used.
3: A write error has occurred.
This is probably most useful in Bash scripts. But, even on the command line, we can demonstrate how to run a command only if it’s running in a terminal window (a TTY or PTS session).
tty -s && echo “In a tty”
Since we are running in a TTY session, our exit code is 0 and the second command is executed.
The one who rules
Other commands may reveal your TTY number. The who command will display information for all registered users, including you.
Alec and Mary are remotely connected to the Linux computer. They are connected to PTS one and two.
The user dave is shown connected to “:0”.
This represents the screen and keyboard physically connected to the computer. Although the display and keyboard are hardware devices, they are still connected to the multiplexer via a device file. tty reveals that it is /dev/pts/2.
Access a TTY
You can access a full screen TTY session by holding down Ctrl + Alt and pressing one of the function keys.
Ctrl + Alt + F3 will bring up the tty3 login prompt.
If you log in and issue the tty command, you’ll see that you’re connected to /dev/tty3.
This is not a pseudo-teletype (emulated in software); it is a virtual teletype (emulated in hardware). You are using the screen and keyboard connected to your computer, to emulate a virtual teletype like the DEC VT100 used to do.
You can use the Ctrl + Alt function keys with the F3 to F6 function keys and have four open TTY sessions if you want. For example, you can log in to tty3 and press Ctrl + Alt + F6 to go to tty6.
To return to your graphical desktop environment, press Ctrl + Alt + F2.
Pressing Ctrl + Alt + F1 will take you back to the login prompt of your graphical desktop session.
At one point, Ctrl + Alt + F1 through Ctrl + Alt + F6 would bring up the full screen TTY consoles, and Ctrl + Alt + F7 would return you to your graphical desktop environment. If you are running an older Linux distribution, this could be the behavior of your system.
This was tested on current versions of Manjaro, Ubuntu and Fedora and they all behaved like this:
Ctrl + Alt + F1 – Returns you to the graphical desktop environment login screen.
Ctrl + Alt + F2 – Returns you to the graphical desktop environment.
Ctrl + Alt + F3: Open TTY 3.
Ctrl + Alt + F4: Open TTY 4.
Ctrl + Alt + F5: Open TTY 5.
Ctrl + Alt + F6: Open TTY 6.
Having access to these full screen consoles allows people using Linux installations solely from the command line, and many Linux servers are configured this way, to have multiple consoles available.
Have you ever been working on a Linux machine with a graphical desktop environment and something caused your session to freeze? You can now jump to one of the TTY console sessions so that you can try to rectify the situation.
You can use top and ps to try to identify the failed application, then use kill to kill it, or just use shutdown to try to shut down as gracefully as the state of the computer allows.
Three little letters with a lot of history
The tty command gets its name from a device from the late 19th century, appeared in Unix in 1971, and is part of Linux and Unix-like operating systems to this day.
The little guy has a big story behind him.