Linux: Difference between /dev/console , /dev/tty and /dev/tty0
Solution 1
From the Linux Kernel documentation on Kernel.org:
/dev/tty Current TTY device
/dev/console System console
/dev/tty0 Current virtual console
In the good old days /dev/console
was System Administrator console. And TTYs were users' serial devices attached to a server.
Now /dev/console
and /dev/tty0
represent current display and usually are the same. You can override it for example by adding console=ttyS0
to grub.conf
. After that your /dev/tty0
is a monitor and /dev/console
is /dev/ttyS0
.
An exercise to show the difference between /dev/tty
and /dev/tty0
:
Switch to the 2nd console by pressing Ctrl+Alt+F2. Login as root
. Type sleep 5; echo tty0 > /dev/tty0
. Press Enter and switch to the 3rd console by pressing Alt+F3.
Now switch back to the 2nd console by pressing Alt+F2. Type sleep 5; echo tty > /dev/tty
, press Enter and switch to the 3rd console.
You can see that tty
is the console where process starts, and tty0
is a always current console.
Solution 2
-
/dev/console
is a virtual set of devices which can be set as a parameter at boot time. It might be redirected to a serial device or a virtual console and by default points to/dev/tty0
. When multipleconsole=
options are passed to the kernel, the console output will go to more than one device; -
/dev/tty[0-N]
(N
is the highest attributed TTY number, e.g. 63) is one of the virtual consoles you switch to with control-alt-F1 and so on; -
/dev/tty0
is also by default virtual console; -
/dev/tty
is kind of an alias to the console (physical, virtual or pseudo device, if any) associated to the process that open it. Unlike the other devices, you do not need root privileges to write to it. Note also that processes like the ones launched bycron
and similar batch processes have no usable/dev/tty
, as they aren't associated with any. These processes have a?
in theTTY
column ofps -ef
output.
Solution 3
/dev/console
https://github.com/torvalds/linux/blob/master/Documentation/admin-guide/serial-console.rst
On Linux, the kernel console can be configured using the console=
boot option. Kernel code which calls printk()
may write messages to it, e.g. when a device is loaded or an error occurs. These messages are also buffered by the kernel. (See also dmesg
). When a console device is found and started, it receives all the previously buffered messages.
You can pass console=
multiple times to configure multiple consoles, and messages will be written to all of them. Apparently you can only select one console of each "type": you can't use both console=ttyS0
and console=ttyS1
.
The kernel documentation specifies /dev/console
as a character device numbered (5,1)
. Opening this character device opens the "main" console, which is the last tty in the list of consoles. The first non-kernel process, called init
or "PID 1", is started with /dev/console
connected to standard output, standard error, and standard input.
If none of the consoles are a tty, then opening /dev/console
returns the error ENODEV
("No such device"). The kernel will print log a message, and start init
regardless. For an example of a kernel console which is not a tty device, see netconsole
, or my favourite console the line printer.
You can also see a list of tty consoles by reading /sys/class/tty/console/active
. systemd documentation points out that the first device shown is the main console. The list is actually in reverse order of the kernel command line. The current kernel documentation incorrectly states that the last device shown is the main or "active" console. For some reason it is possible to poll this file for changes (in case console devices are removed?).
Inside a systemd-nspawn
container, the standard /dev/console
file is replaced with a pseudo-terminal device (PTY). These would be best described as virtual terminal devices. They are created dynamically and are also used to implement graphical terminal emulators like GNOME Terminal, and for remote access like ssh
.
/dev/tty0
The Linux TTY device nodes tty1
through tty63
are virtual terminals. They are also referred to as VTs, or as virtual consoles. They simulate multiple consoles on top of the physical console device driver. Only one virtual console is shown and controlled at a time. The active terminal can be switched, e.g. using chvt
, or Ctrl+Alt+F1 through however many function keys you have.
You can also read and write to the current VT using /dev/tty0
. tty0
is the usual kernel console, e.g. if you did not select one explicitly. "The system first looks for a VGA card [which is what VTs run on] and then for a serial port". You can also set the console to a specific VT, e.g. console=tty1
.
"If you don't have a VGA card in your system, the first serial port will automatically become the console." A "serial console" like ttyS0
is probably the most common alternative to tty0
. It is not possible to use the VT system on top of a serial console.
/dev/tty
/dev/tty
is one of the three standard device files specified by POSIX (/dev/
is one of the three directory names specified by POSIX). Opening it is equivalent to opening the controlling terminal of the current process. The controlling terminal is set when a process first opens a terminal, at least on Linux. For example, in init
, it would refer to /dev/console
.
Detaching from the controlling terminal is one of the steps traditionally required to start a background process, for example a system logging daemon. The steps to become a background process are horribly intricate, but to be specific, the step which detaches from the controlling terminal is the setsid system call. In more modern systems, the init system e.g. systemd starts the service without any controlling terminal in the first place.
Axel Fontaine
Axel Fontaine is a serial entrepreneur, public speaker and software development expert based in Munich. He is the founder of CloudCaptain (previously known as Boxfuse). Their self-service cloud deployment platform enables hundreds of small and medium size companies to focus on development, while they take care of infrastructure and operations. CloudCaptain reliably and securely provisions our customers' applications in the AWS cloud and updates them with zero downtime thanks to their unique immutable infrastructure with minimal fully baked machine images approach. They also take care of all supporting resources including elastic IPs, load balancers, databases, security groups, DNS and more so that their customers can focus their attention where it matters most: delighting their users. In 2010 Axel also created Flyway, and grew it into the world's most popular database migration tool. By late 2017 he extended the project beyond its open-source roots and turned it into a very successful business, acquiring many of the world's largest companies and public institutions as customers for the new commercial pro and enterprise editions we launched. After just under two years of very rapid growth, he sold the company to Redgate in 2019. In the past he has also spoken regularly at many large international conferences including JavaOne, Devoxx, Jfokus, JavaZone, JAX and more about a wide range of topics including modular monoliths, immutable infrastructure and continuous delivery. As part of this he received the JavaOne RockStar speaker award and thanks to his broad industry contributions he was elected Java Champion by Oracle.
Updated on September 18, 2022Comments
-
Axel Fontaine almost 2 years
On a Linux system, what is the difference between
/dev/console
,/dev/tty
and/dev/tty0
?What is their respective use and how do they compare?
-
Admin over 11 yearsYou may also be interested in this
-
-
seriously over 11 yearsnice exercise! Ubuntu locks root, so one way to reproduce this on Ubuntu is:
$ sudo sh -c "sleep5; echo tty0 > /dev/tty0"
-
André Laszlo over 10 years@SFun28, I always used
sudo -i
, and voila - a root shell. -
Peter Cordes almost 10 yearsone idiom for writing to files that require root privs is echo stuff | sudo tee /dev/tty0 >/dev/null;
-
Incnis Mrsi almost 9 yearsDammit. When Ī̲ wrote unix.stackexchange.com/a/229598/80483 , Ī̲ was unaware of this answer!
-
Ron Vince almost 9 yearsWhat do you mean by querying it from this statement "/dev/tty is the console used by the process querying it"?
-
jlliagre almost 9 years@RonVince I mean
/dev/tty
can be a different device, if any, depending on the process that open it. Answer updated. -
Ron Vince almost 9 yearsThanks. May I know are processes actually write/read to/from /dev/tty instead of directly to/from device file associated to them?
-
jlliagre almost 9 years@RonVince I'm afraid I'm not sure what you are asking. Could you rephrase it?
-
Ron Vince almost 9 yearsIf a user's process wants to show something on the user's monitor, is it by Linux standard the process has to write to /dev/tty? Or is it by Linux standard to directly write to /dev/tty[1, .., N] associated to the process? The same for reading input from tty file (keyboard input). Maybe another perspective for my question is does a process need to know its associated /dev/tty[1, .., N] or just read and write to /dev/tty without knowing its own /dev/tty[1, .., N] because /dev/tty is an alias to /dev/tty[1, .., N] associated to itself?
-
jlliagre almost 9 years@RonVince Opening /dev/tty (not /dev/tty[1,..,n]) is the Unix (and Linux) standard for a process to write something to the user's terminal. Directly writing to whatever other device is non portable, more complex and less reliable. A process doesn't need to know what is the actual device the process terminal is connected to, the kernel knows it already.
-
CMCDragonkai over 6 yearsWhat happens when you write to /dev/console? Does it has anything to do with the kernel console that can be viewed with
dmesg
? -
dotbit over 4 yearsis it possible to echo into any of these? like echo "where does it come out?" >> /dev/ttt...yyy...
-
user2948306 over 4 years@dotbit Write a question. You can link to this answer. You can also put a temporary comment under here, to send me a link to your question, if you like.
-
dotbit over 4 yearsin short: tty = alias , console = set , tty3 = virtual con meaning a set also! contradictive!
-
dotbit over 4 yearsecho this_works > /dev/tty but the others are all permission denied .
-
jlliagre over 4 years@dotbit Yes. That's what I mean about /dev/tty when I write Unlike the other devices, you do not need root privileges to write to it.
-
dafnahaktana about 3 yearsI have ubuntu 18.04. the command
sleep5; echo tty0 > /dev/tty0
didn't show the stringtty0
on the other terminal -
LGD over 2 years/dev/tty is related to the
controlling terminal
of a process. If a process without acontrolling terminal
, open /dev/tty will fail. -
jlliagre over 2 years@LGD Yes, that's what I meant with processes like the ones launched by cron and similar batch processes have no usable /dev/tty, as they aren't associated with any.