What does the Linux tty command do? It displays the name of the terminal you’re using.
Origin of Linux tty
What’s the origin of the term “tty”? Well, that’s a bit more difficult to explain…
Teleprinters from the yore
During 1830s and 1840s, machines called teleprinters were developed. These devices could transmit typed messages “down the wire” to other places. The sender used a keyboard-like device to type the message, and it was printed on paper at the receiving end. They were an evolutionary step in telegraphy, which had previously been reliant on Morse.
Messages were encrypted and sent, then decoded and printed. They were encoded using a variety of techniques. The most famous, as well as one of the most prolific, was patented in 1874 by Émile Baudot, whom the “baud rate” is named after. His character encoding method preceded ASCII by 89 years.
Baudot’s coding eventually became the de facto standard for teleprinter encoding, and it was adopted by most manufacturers. Baudot’s original hardware design consisted of just five keys, similar to the piano. For each letter, the operator was required to memorize a specific key sequence. The Baudot encoding system was later linked with a traditional keyboard layout.
To commemorate that progress, the machines were dubbed teletypewriters. The word teletypes was abbreviated to TTYs, which eventually became its common name. So what’s the deal with telegraphy and computers?
ASCII and Telex
The teletype manufacturers adopted it in 1963, and it became the industry standard for text transmission. Despite the invention and widespread usage of the telephone, teletypes were still prevalent.
Telex was a worldwide teletype network that allowed written communications to be sent across the world. It was the major means of sending written information throughout the post-World War II era up until the fax machine boom in the 1980s.
Computers were also improving. They were able to engage with end users in real time and support many people. The old batch approach to working proved insufficient. People didn’t want to wait 24 hours or more for their results. Making stacks of punch cards and waiting overnight for results was no longer acceptable.
The advent of computers and the development of a new technology has created a need for software that would allow users to send and receive data. We wanted efficiency.
The teletype repurposed
The teletype was an ideal candidate for an input/output device because it was a machine that permitted messages to be typed, encoded, transmitted, received, decoded, and printed.
The teletype didn’t care if the device at the other end of the link wasn’t another teletype. The teletype was okay as long as it could speak the same encoding language. It used a more-or-less standard keyboard with a slightly altered layout.
Hardware emulated teletypes
Teletypes became the de facto way to interact with large mini and mainframe computers of that era, replacing telegraph machines.
These were eventually supplanted by devices that simulated them using electronics, with Cathode Ray Tubes (CRTs) instead of paper rolls. They didn’t shake as they delivered answers from the computer. They enabled previously impossible features like moving the cursor around the screen, clearing the screen, and bolding text, among other things.
The DEC VT05 was an early virtual teletype, and its descendant is the renowned DEC VT100. The DEC VT100 has sold millions of units.
Software emulated teletypes
The terminal window and applications like x-term and Konsole are virtual teletypes in the Linux desktop environment and other Unix-like operating systems, such as macOS. However, they are entirely simulated. They’re called fake teletypes. The acronym was shortened to PTS.
And that’s where Linux’s tty command comes in.
Speaking of MacOS, would you like to install Linux on your iPad?
What can tty do on Linux?
There is a “pseudo-teletype multiplexor” in Linux that handles all of the terminal window PTS connections. The master is the multiplexor, and the PTS are its slaves. The kernel communicates with the multiplexor through its device file at /dev/ptmx
The tty command will print the name of the device file that your pseudo-teletype slave is using to interface to the master. And that, effectively, is the number of your terminal window.
Let’s see what Linux’s tty reports:
tty
The response shows we are connected to the device file at /dev/pts/0.
The terminal window, which is a software imitation of a teletype, is linked to the pseudo-teletype multiplexor as a PTS. And it’s zero.
The silent option
The -s (silent) option causes tty to generate no output.
tty -s
It does produce an exit value if:
0: if standard input is coming from a TTY device, emulated or physical.
1: if standard input is not coming from a TTY device.
2: Syntax error, incorrect command line parameters were used.
3: A write error has occurred.
This will be most useful in Bash scripts. However, even on the command line, we may show you how to have a command carried out if you are in a terminal window (a TTY or a PTS session).
tty -s && echo "In a tty"
Because we are running in a TTY session, our exit code is 0, and the second command is executed.
Related: You need to check out these 189 Linux terminal commands
The who command
The “who” command will list all logged in users. User2 and User3 are remotely connected to the Linux computer. They are connected to PTS one and two. User1 is shown as connected to “:0”. The device file of the screen and keyboard is connected to the computer’s multiplexor. Although the screen and keyboard are hardware devices, they are still linked to the multiplexor via a device file. tty indicates that it is /dev/pts/2.
who
Accessing a TTY
By holding down the Ctrl+Alt keys and pressing one of the function keys, you may access a full-screen TTY session. Ctrl+Alt+F3 will bring up the login prompt of tty3. If you log in and issue the tty command, you’ll see you are connected to /dev/tty3.
This isn’t a fake teletype (emulated in software); it’s a virtual teletype (emulated in hardware). It is emulating a virtual teletype, like the DEC VT100, with the screen and keyboard connected to your computer. If you want, you can establish four Linux TTY sessions by using function keys with the Ctrl+Alt combination. If you select this option, for example, and are logged into tty3, press Ctrl+Alt+F6 to go to tty6.
To get back to your graphical desktop environment, press Ctrl+Alt+F2. If you’re using a graphical desktop environment, pressing Ctrl+Alt+F1 will take you back to the login screen of your graphical desktop session. The full-screen Linux TTY consoles are accessible at one time using the Ctrl+Alt+F1 to Ctrl+Alt+F6 keys, and returning to your graphical desktop environment is possible via Ctrl+Alt+F7. If you’re using an older Linux distribution, this may be how it works for you.
This was tested on current releases of Manjaro, Ubuntu, and Fedora and they all behaved like this:
Ctrl+Alt+F1: Returns you to the graphical desktop environment log in screen.
Ctrl+Alt+F2: Returns you to the graphical desktop environment.
Ctrl+Alt+F3: Opens TTY 3.
Ctrl+Alt+F4: Opens TTY 4.
Ctrl+Alt+F5: Opens TTY 5.
Ctrl+Alt+F6: Opens TTY 6.
Having access to these full-screen consoles allows users utilizing command-line only installations of Linux, and many Linux servers are configured this way, to have numerous consoles accessible.
Have you ever been working on a Linux system with a graphical user interface, and something froze your session? You may now go over to one of the TTY console sessions to attempt to fix the problem.
Use top
and ps
to look for the failed application, then terminate it with kill or simply shut down as gracefully as possible.
The 3 little letters that have a long history
The Linux’s tty command gets its name from an obsolete device from the late 1800s, which was first used in Unix in 1971, and it is still part of Linux and other Unix-like operating systems. This little chap has quite a tale behind him.