What is `/dev/console` used for?











up vote
10
down vote

favorite
2












From this answer to Linux: Difference between /dev/console , /dev/tty and /dev/tty0




From the
documentation:



/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.




By "System console", /dev/console seems like the device file of a text physical terminal, just like /dev/tty{1..63} are device files for the virtual consoles.



By "/dev/console and /dev/tty0 represent current display and usually are the same", /dev/console seems to me that it can also be the device file of a
virtual console. /dev/console seems more like /dev/tty0 than like /dev/tty{1..63} (/dev/tty0 is the currently active virtual console, and can be any of /dev/tty{1..63}).



What is /dev/console? What is it used for?



Does /dev/console play the same role for Linux kernel as /dev/tty for a process? (/dev/tty is the controlling terminal of the process session of the process, and can be a pts, /dev/ttyn where n is from 1 to 63, or more?)



The other reply mentions:




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.




Does "the list of consoles" mean all the console='s in the boot option?



By "/dev/console as a character device numbered 5:1", does it mean that /dev/console is the device file of a physical text terminal i.e. a system console? (But again, the first reply I quoted above says /dev/console can be the same as /dev/tty0 which is not a physical text terminal, but a virtual console)



Thanks.










share|improve this question
























  • I created some replacements for those manual pages that went missing years ago: jdebp.eu./Proposals/linux-kvt-manual-pages.html
    – JdeBP
    Dec 1 at 7:44

















up vote
10
down vote

favorite
2












From this answer to Linux: Difference between /dev/console , /dev/tty and /dev/tty0




From the
documentation:



/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.




By "System console", /dev/console seems like the device file of a text physical terminal, just like /dev/tty{1..63} are device files for the virtual consoles.



By "/dev/console and /dev/tty0 represent current display and usually are the same", /dev/console seems to me that it can also be the device file of a
virtual console. /dev/console seems more like /dev/tty0 than like /dev/tty{1..63} (/dev/tty0 is the currently active virtual console, and can be any of /dev/tty{1..63}).



What is /dev/console? What is it used for?



Does /dev/console play the same role for Linux kernel as /dev/tty for a process? (/dev/tty is the controlling terminal of the process session of the process, and can be a pts, /dev/ttyn where n is from 1 to 63, or more?)



The other reply mentions:




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.




Does "the list of consoles" mean all the console='s in the boot option?



By "/dev/console as a character device numbered 5:1", does it mean that /dev/console is the device file of a physical text terminal i.e. a system console? (But again, the first reply I quoted above says /dev/console can be the same as /dev/tty0 which is not a physical text terminal, but a virtual console)



Thanks.










share|improve this question
























  • I created some replacements for those manual pages that went missing years ago: jdebp.eu./Proposals/linux-kvt-manual-pages.html
    – JdeBP
    Dec 1 at 7:44















up vote
10
down vote

favorite
2









up vote
10
down vote

favorite
2






2





From this answer to Linux: Difference between /dev/console , /dev/tty and /dev/tty0




From the
documentation:



/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.




By "System console", /dev/console seems like the device file of a text physical terminal, just like /dev/tty{1..63} are device files for the virtual consoles.



By "/dev/console and /dev/tty0 represent current display and usually are the same", /dev/console seems to me that it can also be the device file of a
virtual console. /dev/console seems more like /dev/tty0 than like /dev/tty{1..63} (/dev/tty0 is the currently active virtual console, and can be any of /dev/tty{1..63}).



What is /dev/console? What is it used for?



Does /dev/console play the same role for Linux kernel as /dev/tty for a process? (/dev/tty is the controlling terminal of the process session of the process, and can be a pts, /dev/ttyn where n is from 1 to 63, or more?)



The other reply mentions:




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.




Does "the list of consoles" mean all the console='s in the boot option?



By "/dev/console as a character device numbered 5:1", does it mean that /dev/console is the device file of a physical text terminal i.e. a system console? (But again, the first reply I quoted above says /dev/console can be the same as /dev/tty0 which is not a physical text terminal, but a virtual console)



Thanks.










share|improve this question















From this answer to Linux: Difference between /dev/console , /dev/tty and /dev/tty0




From the
documentation:



/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.




By "System console", /dev/console seems like the device file of a text physical terminal, just like /dev/tty{1..63} are device files for the virtual consoles.



By "/dev/console and /dev/tty0 represent current display and usually are the same", /dev/console seems to me that it can also be the device file of a
virtual console. /dev/console seems more like /dev/tty0 than like /dev/tty{1..63} (/dev/tty0 is the currently active virtual console, and can be any of /dev/tty{1..63}).



What is /dev/console? What is it used for?



Does /dev/console play the same role for Linux kernel as /dev/tty for a process? (/dev/tty is the controlling terminal of the process session of the process, and can be a pts, /dev/ttyn where n is from 1 to 63, or more?)



The other reply mentions:




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.




Does "the list of consoles" mean all the console='s in the boot option?



By "/dev/console as a character device numbered 5:1", does it mean that /dev/console is the device file of a physical text terminal i.e. a system console? (But again, the first reply I quoted above says /dev/console can be the same as /dev/tty0 which is not a physical text terminal, but a virtual console)



Thanks.







linux ubuntu tty console pts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 30 at 14:42

























asked Nov 30 at 13:50









Tim

25.4k74244447




25.4k74244447












  • I created some replacements for those manual pages that went missing years ago: jdebp.eu./Proposals/linux-kvt-manual-pages.html
    – JdeBP
    Dec 1 at 7:44




















  • I created some replacements for those manual pages that went missing years ago: jdebp.eu./Proposals/linux-kvt-manual-pages.html
    – JdeBP
    Dec 1 at 7:44


















I created some replacements for those manual pages that went missing years ago: jdebp.eu./Proposals/linux-kvt-manual-pages.html
– JdeBP
Dec 1 at 7:44






I created some replacements for those manual pages that went missing years ago: jdebp.eu./Proposals/linux-kvt-manual-pages.html
– JdeBP
Dec 1 at 7:44












2 Answers
2






active

oldest

votes

















up vote
14
down vote



accepted










/dev/console exists primarily to expose the kernel’s console to userspace. The Linux kernel’s documentation on devices now says




The console device, /dev/console, is the device to which system messages should be sent, and on which logins should be permitted in single-user mode. Starting with Linux 2.1.71, /dev/console is managed by the kernel; for previous versions it should be a symbolic link to either /dev/tty0, a specific virtual console such as /dev/tty1, or to a serial port primary (tty*, not cu*) device, depending on the configuration of the system.




/dev/console, the device node with major 5 and minor 1, provides access to whatever the kernel considers to be its primary means of interacting with the system administrator; this can be a physical console connected to the system (with the virtual console abstraction on top, so it can use tty0 or any ttyN where N is between 1 and 63), or a serial console, or a hypervisor console, or even a Braille device. Note that the kernel itself doesn’t use /dev/console: devices nodes are for userspace, not for the kernel; it does, however, check that /dev/console exists and is usable, and sets init up with its standard input, output and error pointing to /dev/console.



As described here, /dev/console is a character device with a fixed major and minor because it’s a separate device (as in, a means of accessing the kernel; not a physical device), not equivalent to /dev/tty0 or any other device. This is somewhat similar to the situation with /dev/tty which is its own device (5:0) because it provides slightly different features than the other virtual console or terminal devices.



The “list of consoles” is indeed the list of consoles defined by the console= boot parameters (or the default console, if there are none). You can see the consoles defined in this way by looking at /proc/consoles. /dev/console does indeed provide access to the last of these:




You can specify multiple console= options on the kernel command line. Output will appear on all of them. The last device will be used when you open /dev/console.







share|improve this answer























  • Comments are not for extended discussion; this conversation has been moved to chat.
    – terdon
    Dec 1 at 16:25


















up vote
4
down vote













"What is /dev/console?" is answered in the previous answer. Perhaps that answer is more clear when you know the answers to the other two questions.



Q1. "What is the device file representing the physical terminal itself?"



There is no such device file.



Q2. "What is /dev/console used for?"



On Linux, /dev/console is used to show messages during startup (and shutdown). It is also used for "single user mode", as pointed out in Stephen Kitt's answer. There is not much else it makes sense to use it for.



"In the good old days" of Unix, /dev/console was a dedicated physical device. But this is not the case in Linux.



Related evidence



1. "What is the device file representing the physical terminal itself?"




Let me try to understand this way. /dev/tty{1..63} and /dev/pts/n are device files representing devices themselves (although they are emulations), not in relation to process or kernel. /dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?). /dev/tty represents the controlling terminal currently used by a process session. /dev/console represents the terminal currently used by the kernel?



What is the device file representing the physical terminal itself, not in relation to kernel or process?




The underlying device(s) for /dev/tty{1..63} are struct con_driver. To see all the possible drivers, check out https://elixir.bootlin.com/linux/v4.19/ident/do_take_over_console



There is no device file for these underlying device(s)!





There is only a minimal userspace interface to manage them.



$ head /sys/class/vtconsole/*/name
==> /sys/class/vtconsole/vtcon0/name <==
(S) dummy device

==> /sys/class/vtconsole/vtcon1/name <==
(M) frame buffer device


If you really want to know more, the (M) stands for module. I.e. the dummy console device is not provided by a loadable kernel module; it is part of the initial kernel image (aka "builtin").



Secondly, the bind file in each subdirectory of /sys/class/vtconsole appears to tell you which vtconsole device is active. If I write 0 to the active one, it appears to switch to the dummy one. (GUI VTs seem unaffected, but text VTs stop working). Writing 1 for the dummy one does not activate it. Either method works to switch back to the real one. If I read the code correctly, the trick is that echo 1 > bind is only supposed to work for console drivers which are built as a module (?!).



For framebuffer consoles specifically, there is some more information about binding different framebuffer devices (/dev/fb0...) to specific virtual consoles in https://kernel.org/doc/Documentation/fb/fbcon.txt . This involves a kernel option fbcon:map= or a command called con2fbmap.



Of course the details can vary with different kernel versions, architectures, firmwares, devices, drivers, etc. I've never really had to use any of the interfaces above. The kernel just lets i915 / inteldrmfb / whatever you want to call it take over when it loads, replacing e.g. vgacon.



It looks like my EFI machine never has vgacon. So firstly it uses a dummy console, and secondly after 1.2 seconds it switches to fbcon, running on top of efifb. But so far I haven't had to care what the details are; it just works.



$ dmesg | grep -C2 [Cc]onsole
[ 0.230822] rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=4
[ 0.233164] NR_IRQS: 65792, nr_irqs: 728, preallocated irqs: 16
[ 0.233346] Console: colour dummy device 80x25
[ 0.233571] console [tty0] enabled
[ 0.233585] ACPI: Core revision 20180810
[ 0.233838] clocksource: hpet: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 133484882848 ns
--
[ 1.228393] efifb: scrolling: redraw
[ 1.228396] efifb: Truecolor: size=8:8:8:8, shift=24:16:8:0
[ 1.230393] Console: switching to colour frame buffer device 170x48
[ 1.232090] fb0: EFI VGA frame buffer device
[ 1.232110] intel_idle: MWAIT substates: 0x11142120
--
[ 3.595838] checking generic (e0000000 408000) vs hw (e0000000 10000000)
[ 3.595839] fb: switching to inteldrmfb from EFI VGA
[ 3.596577] Console: switching to colour dummy device 80x25
[ 3.596681] [drm] Replacing VGA console driver
[ 3.597159] [drm] ACPI BIOS requests an excessive sleep of 20000 ms, using 1500 ms instead
[ 3.599830] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013).
--
[ 3.657050] e1000e 0000:00:19.0 eth0: MAC: 11, PHY: 12, PBA No: FFFFFF-0FF
[ 3.657869] e1000e 0000:00:19.0 eno1: renamed from eth0
[ 4.711453] Console: switching to colour frame buffer device 170x48
[ 4.734356] i915 0000:00:02.0: fb0: inteldrmfb frame buffer device
[ 4.778813] Loading iSCSI transport class v2.0-870.


2. "What is /dev/console used for?"



You can use /dev/console as a TTY device. Writing to it, for example, will write to a specific underlying device, which will also have a character device number of its own.



Often /dev/console is tied to /dev/tty0, but sometimes it may be tied to a different device.



So in this case writing to /dev/console will write to /dev/tty0. And in turn, writing to /dev/tty0 is equivalent to writing to whichever /dev/ttyN device is currently active.



But this raises an interesting question. Accessing tty0 will access different virtual consoles, depending on which is currently active. What do people actually use tty0 for, and similarly what is console used for on Linux?




  1. Technically, you can read and write from console / tty0, for example running a getty to allow logging in on tty0. But this is only useful as a quick hack. Because it means you cannot take advantage of Linux's multiple virtual consoles.


  2. systemd looks in sysfs for an attribute associated with the /dev/console device, to detect the underlying TTY device. This allows systemd to automatically spawn a getty and allow logging in on e.g. a serial console, when the user set up a kernel console by booting with console=ttyS0. This is convenient; it avoids the need to configure this console in two different places. Again, see man systemd-getty-generator. However, systemd does not actually open /dev/console for this.


  3. During system bootstrap, you might not even have sysfs mounted yet. But you want to be able to show error and progress messages as soon as possible! So we circle around to point 1). The kernel starts PID 1 with stdin/stdout/stderr connected to /dev/console. It's very nice to have this simple mechanism set up right from the start.



  4. Inside a Linux container, the file at /dev/console may be created as something different - not the character device number 5:1. Instead, it may be created as a PTS device file. Then it would make sense to log in through this /dev/console file. systemd inside a container will allow logging in on such a device; see man systemd-getty-generator.



    This mechanism is used when you run a container with the systemd-nspawn command. (I think only when you run systemd-nspawn on a TTY, although I can't tell from searching the man page).



    systemd-nspawn creates the container's /dev/console as a bind mount of a PTS device from the host. This means that this PTS device is not visible inside /dev/pts/ inside the container.



    PTS devices are local to a specific devpts mount. PTS devices are an exception to the normal rule, that devices are identified by their device number. PTS devices are identified by the combination of their device number, and their devpts mount.




  5. You can write urgent messages to console / tty0, to write to the user's current virtual console. This could be useful for urgent userspace error messages, similar to urgent kernel messages which are printed to the console (see man dmesg). However it is not common to do this, at least once the system has finished booting.



    rsyslog has one example on this page, which prints kernel messages to /dev/console; this is pointless on Linux because the kernel will already do so by default. One example which I cannot find again says that it's not a good idea to use this for non-kernel messages because there are just too many syslog messages, you flood your console and it gets in the way too much.



    systemd-journald similarly has options to forward all logs to the console. In principle this might be useful for debugging in a virtual environment. Although, for debugging we usually forward to /dev/kmsg instead. This saves them in the kernel log buffer so you can read them with dmesg. Like messages generated by the kernel itself, these messages may be echo'ed to the console depending on the current kernel configuration.








share|improve this answer























  • As a complement to point 2, it used to be common (not just on Linux) to run xconsole to keep track of what was going on ;-). (And to avoid having messages written directly to the framebuffer, e.g. on SPARCstations.)
    – Stephen Kitt
    Nov 30 at 16:39










  • @StephenKitt when I first used SUSE Linux, I think it started xconsole, or ste it up so you could start it from the desktop main menu, or something - I don't remember exactly but I have seen it :-).
    – sourcejedi
    Nov 30 at 16:55












  • Let me try to understand this way. /dev/tty{1..63} and /dev/pts/n are device files representing devices themselves (although they are emulations), not in relation to process or kernel. /dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?). /dev/tty represents the controlling terminal currently used by a process session. /dev/console represents the terminal currently used by the kernel? What is the device file representing the physical terminal itself, not in relation to kernel or process?
    – Tim
    Nov 30 at 20:07












  • @Tim "/dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?)" - kernel.
    – sourcejedi
    Nov 30 at 20:22










  • What is the device file representing the physical terminal itself, not in relation to kernel or process?
    – Tim
    Nov 30 at 20:24











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485156%2fwhat-is-dev-console-used-for%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
14
down vote



accepted










/dev/console exists primarily to expose the kernel’s console to userspace. The Linux kernel’s documentation on devices now says




The console device, /dev/console, is the device to which system messages should be sent, and on which logins should be permitted in single-user mode. Starting with Linux 2.1.71, /dev/console is managed by the kernel; for previous versions it should be a symbolic link to either /dev/tty0, a specific virtual console such as /dev/tty1, or to a serial port primary (tty*, not cu*) device, depending on the configuration of the system.




/dev/console, the device node with major 5 and minor 1, provides access to whatever the kernel considers to be its primary means of interacting with the system administrator; this can be a physical console connected to the system (with the virtual console abstraction on top, so it can use tty0 or any ttyN where N is between 1 and 63), or a serial console, or a hypervisor console, or even a Braille device. Note that the kernel itself doesn’t use /dev/console: devices nodes are for userspace, not for the kernel; it does, however, check that /dev/console exists and is usable, and sets init up with its standard input, output and error pointing to /dev/console.



As described here, /dev/console is a character device with a fixed major and minor because it’s a separate device (as in, a means of accessing the kernel; not a physical device), not equivalent to /dev/tty0 or any other device. This is somewhat similar to the situation with /dev/tty which is its own device (5:0) because it provides slightly different features than the other virtual console or terminal devices.



The “list of consoles” is indeed the list of consoles defined by the console= boot parameters (or the default console, if there are none). You can see the consoles defined in this way by looking at /proc/consoles. /dev/console does indeed provide access to the last of these:




You can specify multiple console= options on the kernel command line. Output will appear on all of them. The last device will be used when you open /dev/console.







share|improve this answer























  • Comments are not for extended discussion; this conversation has been moved to chat.
    – terdon
    Dec 1 at 16:25















up vote
14
down vote



accepted










/dev/console exists primarily to expose the kernel’s console to userspace. The Linux kernel’s documentation on devices now says




The console device, /dev/console, is the device to which system messages should be sent, and on which logins should be permitted in single-user mode. Starting with Linux 2.1.71, /dev/console is managed by the kernel; for previous versions it should be a symbolic link to either /dev/tty0, a specific virtual console such as /dev/tty1, or to a serial port primary (tty*, not cu*) device, depending on the configuration of the system.




/dev/console, the device node with major 5 and minor 1, provides access to whatever the kernel considers to be its primary means of interacting with the system administrator; this can be a physical console connected to the system (with the virtual console abstraction on top, so it can use tty0 or any ttyN where N is between 1 and 63), or a serial console, or a hypervisor console, or even a Braille device. Note that the kernel itself doesn’t use /dev/console: devices nodes are for userspace, not for the kernel; it does, however, check that /dev/console exists and is usable, and sets init up with its standard input, output and error pointing to /dev/console.



As described here, /dev/console is a character device with a fixed major and minor because it’s a separate device (as in, a means of accessing the kernel; not a physical device), not equivalent to /dev/tty0 or any other device. This is somewhat similar to the situation with /dev/tty which is its own device (5:0) because it provides slightly different features than the other virtual console or terminal devices.



The “list of consoles” is indeed the list of consoles defined by the console= boot parameters (or the default console, if there are none). You can see the consoles defined in this way by looking at /proc/consoles. /dev/console does indeed provide access to the last of these:




You can specify multiple console= options on the kernel command line. Output will appear on all of them. The last device will be used when you open /dev/console.







share|improve this answer























  • Comments are not for extended discussion; this conversation has been moved to chat.
    – terdon
    Dec 1 at 16:25













up vote
14
down vote



accepted







up vote
14
down vote



accepted






/dev/console exists primarily to expose the kernel’s console to userspace. The Linux kernel’s documentation on devices now says




The console device, /dev/console, is the device to which system messages should be sent, and on which logins should be permitted in single-user mode. Starting with Linux 2.1.71, /dev/console is managed by the kernel; for previous versions it should be a symbolic link to either /dev/tty0, a specific virtual console such as /dev/tty1, or to a serial port primary (tty*, not cu*) device, depending on the configuration of the system.




/dev/console, the device node with major 5 and minor 1, provides access to whatever the kernel considers to be its primary means of interacting with the system administrator; this can be a physical console connected to the system (with the virtual console abstraction on top, so it can use tty0 or any ttyN where N is between 1 and 63), or a serial console, or a hypervisor console, or even a Braille device. Note that the kernel itself doesn’t use /dev/console: devices nodes are for userspace, not for the kernel; it does, however, check that /dev/console exists and is usable, and sets init up with its standard input, output and error pointing to /dev/console.



As described here, /dev/console is a character device with a fixed major and minor because it’s a separate device (as in, a means of accessing the kernel; not a physical device), not equivalent to /dev/tty0 or any other device. This is somewhat similar to the situation with /dev/tty which is its own device (5:0) because it provides slightly different features than the other virtual console or terminal devices.



The “list of consoles” is indeed the list of consoles defined by the console= boot parameters (or the default console, if there are none). You can see the consoles defined in this way by looking at /proc/consoles. /dev/console does indeed provide access to the last of these:




You can specify multiple console= options on the kernel command line. Output will appear on all of them. The last device will be used when you open /dev/console.







share|improve this answer














/dev/console exists primarily to expose the kernel’s console to userspace. The Linux kernel’s documentation on devices now says




The console device, /dev/console, is the device to which system messages should be sent, and on which logins should be permitted in single-user mode. Starting with Linux 2.1.71, /dev/console is managed by the kernel; for previous versions it should be a symbolic link to either /dev/tty0, a specific virtual console such as /dev/tty1, or to a serial port primary (tty*, not cu*) device, depending on the configuration of the system.




/dev/console, the device node with major 5 and minor 1, provides access to whatever the kernel considers to be its primary means of interacting with the system administrator; this can be a physical console connected to the system (with the virtual console abstraction on top, so it can use tty0 or any ttyN where N is between 1 and 63), or a serial console, or a hypervisor console, or even a Braille device. Note that the kernel itself doesn’t use /dev/console: devices nodes are for userspace, not for the kernel; it does, however, check that /dev/console exists and is usable, and sets init up with its standard input, output and error pointing to /dev/console.



As described here, /dev/console is a character device with a fixed major and minor because it’s a separate device (as in, a means of accessing the kernel; not a physical device), not equivalent to /dev/tty0 or any other device. This is somewhat similar to the situation with /dev/tty which is its own device (5:0) because it provides slightly different features than the other virtual console or terminal devices.



The “list of consoles” is indeed the list of consoles defined by the console= boot parameters (or the default console, if there are none). You can see the consoles defined in this way by looking at /proc/consoles. /dev/console does indeed provide access to the last of these:




You can specify multiple console= options on the kernel command line. Output will appear on all of them. The last device will be used when you open /dev/console.








share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 1 at 14:49

























answered Nov 30 at 14:53









Stephen Kitt

161k24358436




161k24358436












  • Comments are not for extended discussion; this conversation has been moved to chat.
    – terdon
    Dec 1 at 16:25


















  • Comments are not for extended discussion; this conversation has been moved to chat.
    – terdon
    Dec 1 at 16:25
















Comments are not for extended discussion; this conversation has been moved to chat.
– terdon
Dec 1 at 16:25




Comments are not for extended discussion; this conversation has been moved to chat.
– terdon
Dec 1 at 16:25












up vote
4
down vote













"What is /dev/console?" is answered in the previous answer. Perhaps that answer is more clear when you know the answers to the other two questions.



Q1. "What is the device file representing the physical terminal itself?"



There is no such device file.



Q2. "What is /dev/console used for?"



On Linux, /dev/console is used to show messages during startup (and shutdown). It is also used for "single user mode", as pointed out in Stephen Kitt's answer. There is not much else it makes sense to use it for.



"In the good old days" of Unix, /dev/console was a dedicated physical device. But this is not the case in Linux.



Related evidence



1. "What is the device file representing the physical terminal itself?"




Let me try to understand this way. /dev/tty{1..63} and /dev/pts/n are device files representing devices themselves (although they are emulations), not in relation to process or kernel. /dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?). /dev/tty represents the controlling terminal currently used by a process session. /dev/console represents the terminal currently used by the kernel?



What is the device file representing the physical terminal itself, not in relation to kernel or process?




The underlying device(s) for /dev/tty{1..63} are struct con_driver. To see all the possible drivers, check out https://elixir.bootlin.com/linux/v4.19/ident/do_take_over_console



There is no device file for these underlying device(s)!





There is only a minimal userspace interface to manage them.



$ head /sys/class/vtconsole/*/name
==> /sys/class/vtconsole/vtcon0/name <==
(S) dummy device

==> /sys/class/vtconsole/vtcon1/name <==
(M) frame buffer device


If you really want to know more, the (M) stands for module. I.e. the dummy console device is not provided by a loadable kernel module; it is part of the initial kernel image (aka "builtin").



Secondly, the bind file in each subdirectory of /sys/class/vtconsole appears to tell you which vtconsole device is active. If I write 0 to the active one, it appears to switch to the dummy one. (GUI VTs seem unaffected, but text VTs stop working). Writing 1 for the dummy one does not activate it. Either method works to switch back to the real one. If I read the code correctly, the trick is that echo 1 > bind is only supposed to work for console drivers which are built as a module (?!).



For framebuffer consoles specifically, there is some more information about binding different framebuffer devices (/dev/fb0...) to specific virtual consoles in https://kernel.org/doc/Documentation/fb/fbcon.txt . This involves a kernel option fbcon:map= or a command called con2fbmap.



Of course the details can vary with different kernel versions, architectures, firmwares, devices, drivers, etc. I've never really had to use any of the interfaces above. The kernel just lets i915 / inteldrmfb / whatever you want to call it take over when it loads, replacing e.g. vgacon.



It looks like my EFI machine never has vgacon. So firstly it uses a dummy console, and secondly after 1.2 seconds it switches to fbcon, running on top of efifb. But so far I haven't had to care what the details are; it just works.



$ dmesg | grep -C2 [Cc]onsole
[ 0.230822] rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=4
[ 0.233164] NR_IRQS: 65792, nr_irqs: 728, preallocated irqs: 16
[ 0.233346] Console: colour dummy device 80x25
[ 0.233571] console [tty0] enabled
[ 0.233585] ACPI: Core revision 20180810
[ 0.233838] clocksource: hpet: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 133484882848 ns
--
[ 1.228393] efifb: scrolling: redraw
[ 1.228396] efifb: Truecolor: size=8:8:8:8, shift=24:16:8:0
[ 1.230393] Console: switching to colour frame buffer device 170x48
[ 1.232090] fb0: EFI VGA frame buffer device
[ 1.232110] intel_idle: MWAIT substates: 0x11142120
--
[ 3.595838] checking generic (e0000000 408000) vs hw (e0000000 10000000)
[ 3.595839] fb: switching to inteldrmfb from EFI VGA
[ 3.596577] Console: switching to colour dummy device 80x25
[ 3.596681] [drm] Replacing VGA console driver
[ 3.597159] [drm] ACPI BIOS requests an excessive sleep of 20000 ms, using 1500 ms instead
[ 3.599830] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013).
--
[ 3.657050] e1000e 0000:00:19.0 eth0: MAC: 11, PHY: 12, PBA No: FFFFFF-0FF
[ 3.657869] e1000e 0000:00:19.0 eno1: renamed from eth0
[ 4.711453] Console: switching to colour frame buffer device 170x48
[ 4.734356] i915 0000:00:02.0: fb0: inteldrmfb frame buffer device
[ 4.778813] Loading iSCSI transport class v2.0-870.


2. "What is /dev/console used for?"



You can use /dev/console as a TTY device. Writing to it, for example, will write to a specific underlying device, which will also have a character device number of its own.



Often /dev/console is tied to /dev/tty0, but sometimes it may be tied to a different device.



So in this case writing to /dev/console will write to /dev/tty0. And in turn, writing to /dev/tty0 is equivalent to writing to whichever /dev/ttyN device is currently active.



But this raises an interesting question. Accessing tty0 will access different virtual consoles, depending on which is currently active. What do people actually use tty0 for, and similarly what is console used for on Linux?




  1. Technically, you can read and write from console / tty0, for example running a getty to allow logging in on tty0. But this is only useful as a quick hack. Because it means you cannot take advantage of Linux's multiple virtual consoles.


  2. systemd looks in sysfs for an attribute associated with the /dev/console device, to detect the underlying TTY device. This allows systemd to automatically spawn a getty and allow logging in on e.g. a serial console, when the user set up a kernel console by booting with console=ttyS0. This is convenient; it avoids the need to configure this console in two different places. Again, see man systemd-getty-generator. However, systemd does not actually open /dev/console for this.


  3. During system bootstrap, you might not even have sysfs mounted yet. But you want to be able to show error and progress messages as soon as possible! So we circle around to point 1). The kernel starts PID 1 with stdin/stdout/stderr connected to /dev/console. It's very nice to have this simple mechanism set up right from the start.



  4. Inside a Linux container, the file at /dev/console may be created as something different - not the character device number 5:1. Instead, it may be created as a PTS device file. Then it would make sense to log in through this /dev/console file. systemd inside a container will allow logging in on such a device; see man systemd-getty-generator.



    This mechanism is used when you run a container with the systemd-nspawn command. (I think only when you run systemd-nspawn on a TTY, although I can't tell from searching the man page).



    systemd-nspawn creates the container's /dev/console as a bind mount of a PTS device from the host. This means that this PTS device is not visible inside /dev/pts/ inside the container.



    PTS devices are local to a specific devpts mount. PTS devices are an exception to the normal rule, that devices are identified by their device number. PTS devices are identified by the combination of their device number, and their devpts mount.




  5. You can write urgent messages to console / tty0, to write to the user's current virtual console. This could be useful for urgent userspace error messages, similar to urgent kernel messages which are printed to the console (see man dmesg). However it is not common to do this, at least once the system has finished booting.



    rsyslog has one example on this page, which prints kernel messages to /dev/console; this is pointless on Linux because the kernel will already do so by default. One example which I cannot find again says that it's not a good idea to use this for non-kernel messages because there are just too many syslog messages, you flood your console and it gets in the way too much.



    systemd-journald similarly has options to forward all logs to the console. In principle this might be useful for debugging in a virtual environment. Although, for debugging we usually forward to /dev/kmsg instead. This saves them in the kernel log buffer so you can read them with dmesg. Like messages generated by the kernel itself, these messages may be echo'ed to the console depending on the current kernel configuration.








share|improve this answer























  • As a complement to point 2, it used to be common (not just on Linux) to run xconsole to keep track of what was going on ;-). (And to avoid having messages written directly to the framebuffer, e.g. on SPARCstations.)
    – Stephen Kitt
    Nov 30 at 16:39










  • @StephenKitt when I first used SUSE Linux, I think it started xconsole, or ste it up so you could start it from the desktop main menu, or something - I don't remember exactly but I have seen it :-).
    – sourcejedi
    Nov 30 at 16:55












  • Let me try to understand this way. /dev/tty{1..63} and /dev/pts/n are device files representing devices themselves (although they are emulations), not in relation to process or kernel. /dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?). /dev/tty represents the controlling terminal currently used by a process session. /dev/console represents the terminal currently used by the kernel? What is the device file representing the physical terminal itself, not in relation to kernel or process?
    – Tim
    Nov 30 at 20:07












  • @Tim "/dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?)" - kernel.
    – sourcejedi
    Nov 30 at 20:22










  • What is the device file representing the physical terminal itself, not in relation to kernel or process?
    – Tim
    Nov 30 at 20:24















up vote
4
down vote













"What is /dev/console?" is answered in the previous answer. Perhaps that answer is more clear when you know the answers to the other two questions.



Q1. "What is the device file representing the physical terminal itself?"



There is no such device file.



Q2. "What is /dev/console used for?"



On Linux, /dev/console is used to show messages during startup (and shutdown). It is also used for "single user mode", as pointed out in Stephen Kitt's answer. There is not much else it makes sense to use it for.



"In the good old days" of Unix, /dev/console was a dedicated physical device. But this is not the case in Linux.



Related evidence



1. "What is the device file representing the physical terminal itself?"




Let me try to understand this way. /dev/tty{1..63} and /dev/pts/n are device files representing devices themselves (although they are emulations), not in relation to process or kernel. /dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?). /dev/tty represents the controlling terminal currently used by a process session. /dev/console represents the terminal currently used by the kernel?



What is the device file representing the physical terminal itself, not in relation to kernel or process?




The underlying device(s) for /dev/tty{1..63} are struct con_driver. To see all the possible drivers, check out https://elixir.bootlin.com/linux/v4.19/ident/do_take_over_console



There is no device file for these underlying device(s)!





There is only a minimal userspace interface to manage them.



$ head /sys/class/vtconsole/*/name
==> /sys/class/vtconsole/vtcon0/name <==
(S) dummy device

==> /sys/class/vtconsole/vtcon1/name <==
(M) frame buffer device


If you really want to know more, the (M) stands for module. I.e. the dummy console device is not provided by a loadable kernel module; it is part of the initial kernel image (aka "builtin").



Secondly, the bind file in each subdirectory of /sys/class/vtconsole appears to tell you which vtconsole device is active. If I write 0 to the active one, it appears to switch to the dummy one. (GUI VTs seem unaffected, but text VTs stop working). Writing 1 for the dummy one does not activate it. Either method works to switch back to the real one. If I read the code correctly, the trick is that echo 1 > bind is only supposed to work for console drivers which are built as a module (?!).



For framebuffer consoles specifically, there is some more information about binding different framebuffer devices (/dev/fb0...) to specific virtual consoles in https://kernel.org/doc/Documentation/fb/fbcon.txt . This involves a kernel option fbcon:map= or a command called con2fbmap.



Of course the details can vary with different kernel versions, architectures, firmwares, devices, drivers, etc. I've never really had to use any of the interfaces above. The kernel just lets i915 / inteldrmfb / whatever you want to call it take over when it loads, replacing e.g. vgacon.



It looks like my EFI machine never has vgacon. So firstly it uses a dummy console, and secondly after 1.2 seconds it switches to fbcon, running on top of efifb. But so far I haven't had to care what the details are; it just works.



$ dmesg | grep -C2 [Cc]onsole
[ 0.230822] rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=4
[ 0.233164] NR_IRQS: 65792, nr_irqs: 728, preallocated irqs: 16
[ 0.233346] Console: colour dummy device 80x25
[ 0.233571] console [tty0] enabled
[ 0.233585] ACPI: Core revision 20180810
[ 0.233838] clocksource: hpet: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 133484882848 ns
--
[ 1.228393] efifb: scrolling: redraw
[ 1.228396] efifb: Truecolor: size=8:8:8:8, shift=24:16:8:0
[ 1.230393] Console: switching to colour frame buffer device 170x48
[ 1.232090] fb0: EFI VGA frame buffer device
[ 1.232110] intel_idle: MWAIT substates: 0x11142120
--
[ 3.595838] checking generic (e0000000 408000) vs hw (e0000000 10000000)
[ 3.595839] fb: switching to inteldrmfb from EFI VGA
[ 3.596577] Console: switching to colour dummy device 80x25
[ 3.596681] [drm] Replacing VGA console driver
[ 3.597159] [drm] ACPI BIOS requests an excessive sleep of 20000 ms, using 1500 ms instead
[ 3.599830] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013).
--
[ 3.657050] e1000e 0000:00:19.0 eth0: MAC: 11, PHY: 12, PBA No: FFFFFF-0FF
[ 3.657869] e1000e 0000:00:19.0 eno1: renamed from eth0
[ 4.711453] Console: switching to colour frame buffer device 170x48
[ 4.734356] i915 0000:00:02.0: fb0: inteldrmfb frame buffer device
[ 4.778813] Loading iSCSI transport class v2.0-870.


2. "What is /dev/console used for?"



You can use /dev/console as a TTY device. Writing to it, for example, will write to a specific underlying device, which will also have a character device number of its own.



Often /dev/console is tied to /dev/tty0, but sometimes it may be tied to a different device.



So in this case writing to /dev/console will write to /dev/tty0. And in turn, writing to /dev/tty0 is equivalent to writing to whichever /dev/ttyN device is currently active.



But this raises an interesting question. Accessing tty0 will access different virtual consoles, depending on which is currently active. What do people actually use tty0 for, and similarly what is console used for on Linux?




  1. Technically, you can read and write from console / tty0, for example running a getty to allow logging in on tty0. But this is only useful as a quick hack. Because it means you cannot take advantage of Linux's multiple virtual consoles.


  2. systemd looks in sysfs for an attribute associated with the /dev/console device, to detect the underlying TTY device. This allows systemd to automatically spawn a getty and allow logging in on e.g. a serial console, when the user set up a kernel console by booting with console=ttyS0. This is convenient; it avoids the need to configure this console in two different places. Again, see man systemd-getty-generator. However, systemd does not actually open /dev/console for this.


  3. During system bootstrap, you might not even have sysfs mounted yet. But you want to be able to show error and progress messages as soon as possible! So we circle around to point 1). The kernel starts PID 1 with stdin/stdout/stderr connected to /dev/console. It's very nice to have this simple mechanism set up right from the start.



  4. Inside a Linux container, the file at /dev/console may be created as something different - not the character device number 5:1. Instead, it may be created as a PTS device file. Then it would make sense to log in through this /dev/console file. systemd inside a container will allow logging in on such a device; see man systemd-getty-generator.



    This mechanism is used when you run a container with the systemd-nspawn command. (I think only when you run systemd-nspawn on a TTY, although I can't tell from searching the man page).



    systemd-nspawn creates the container's /dev/console as a bind mount of a PTS device from the host. This means that this PTS device is not visible inside /dev/pts/ inside the container.



    PTS devices are local to a specific devpts mount. PTS devices are an exception to the normal rule, that devices are identified by their device number. PTS devices are identified by the combination of their device number, and their devpts mount.




  5. You can write urgent messages to console / tty0, to write to the user's current virtual console. This could be useful for urgent userspace error messages, similar to urgent kernel messages which are printed to the console (see man dmesg). However it is not common to do this, at least once the system has finished booting.



    rsyslog has one example on this page, which prints kernel messages to /dev/console; this is pointless on Linux because the kernel will already do so by default. One example which I cannot find again says that it's not a good idea to use this for non-kernel messages because there are just too many syslog messages, you flood your console and it gets in the way too much.



    systemd-journald similarly has options to forward all logs to the console. In principle this might be useful for debugging in a virtual environment. Although, for debugging we usually forward to /dev/kmsg instead. This saves them in the kernel log buffer so you can read them with dmesg. Like messages generated by the kernel itself, these messages may be echo'ed to the console depending on the current kernel configuration.








share|improve this answer























  • As a complement to point 2, it used to be common (not just on Linux) to run xconsole to keep track of what was going on ;-). (And to avoid having messages written directly to the framebuffer, e.g. on SPARCstations.)
    – Stephen Kitt
    Nov 30 at 16:39










  • @StephenKitt when I first used SUSE Linux, I think it started xconsole, or ste it up so you could start it from the desktop main menu, or something - I don't remember exactly but I have seen it :-).
    – sourcejedi
    Nov 30 at 16:55












  • Let me try to understand this way. /dev/tty{1..63} and /dev/pts/n are device files representing devices themselves (although they are emulations), not in relation to process or kernel. /dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?). /dev/tty represents the controlling terminal currently used by a process session. /dev/console represents the terminal currently used by the kernel? What is the device file representing the physical terminal itself, not in relation to kernel or process?
    – Tim
    Nov 30 at 20:07












  • @Tim "/dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?)" - kernel.
    – sourcejedi
    Nov 30 at 20:22










  • What is the device file representing the physical terminal itself, not in relation to kernel or process?
    – Tim
    Nov 30 at 20:24













up vote
4
down vote










up vote
4
down vote









"What is /dev/console?" is answered in the previous answer. Perhaps that answer is more clear when you know the answers to the other two questions.



Q1. "What is the device file representing the physical terminal itself?"



There is no such device file.



Q2. "What is /dev/console used for?"



On Linux, /dev/console is used to show messages during startup (and shutdown). It is also used for "single user mode", as pointed out in Stephen Kitt's answer. There is not much else it makes sense to use it for.



"In the good old days" of Unix, /dev/console was a dedicated physical device. But this is not the case in Linux.



Related evidence



1. "What is the device file representing the physical terminal itself?"




Let me try to understand this way. /dev/tty{1..63} and /dev/pts/n are device files representing devices themselves (although they are emulations), not in relation to process or kernel. /dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?). /dev/tty represents the controlling terminal currently used by a process session. /dev/console represents the terminal currently used by the kernel?



What is the device file representing the physical terminal itself, not in relation to kernel or process?




The underlying device(s) for /dev/tty{1..63} are struct con_driver. To see all the possible drivers, check out https://elixir.bootlin.com/linux/v4.19/ident/do_take_over_console



There is no device file for these underlying device(s)!





There is only a minimal userspace interface to manage them.



$ head /sys/class/vtconsole/*/name
==> /sys/class/vtconsole/vtcon0/name <==
(S) dummy device

==> /sys/class/vtconsole/vtcon1/name <==
(M) frame buffer device


If you really want to know more, the (M) stands for module. I.e. the dummy console device is not provided by a loadable kernel module; it is part of the initial kernel image (aka "builtin").



Secondly, the bind file in each subdirectory of /sys/class/vtconsole appears to tell you which vtconsole device is active. If I write 0 to the active one, it appears to switch to the dummy one. (GUI VTs seem unaffected, but text VTs stop working). Writing 1 for the dummy one does not activate it. Either method works to switch back to the real one. If I read the code correctly, the trick is that echo 1 > bind is only supposed to work for console drivers which are built as a module (?!).



For framebuffer consoles specifically, there is some more information about binding different framebuffer devices (/dev/fb0...) to specific virtual consoles in https://kernel.org/doc/Documentation/fb/fbcon.txt . This involves a kernel option fbcon:map= or a command called con2fbmap.



Of course the details can vary with different kernel versions, architectures, firmwares, devices, drivers, etc. I've never really had to use any of the interfaces above. The kernel just lets i915 / inteldrmfb / whatever you want to call it take over when it loads, replacing e.g. vgacon.



It looks like my EFI machine never has vgacon. So firstly it uses a dummy console, and secondly after 1.2 seconds it switches to fbcon, running on top of efifb. But so far I haven't had to care what the details are; it just works.



$ dmesg | grep -C2 [Cc]onsole
[ 0.230822] rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=4
[ 0.233164] NR_IRQS: 65792, nr_irqs: 728, preallocated irqs: 16
[ 0.233346] Console: colour dummy device 80x25
[ 0.233571] console [tty0] enabled
[ 0.233585] ACPI: Core revision 20180810
[ 0.233838] clocksource: hpet: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 133484882848 ns
--
[ 1.228393] efifb: scrolling: redraw
[ 1.228396] efifb: Truecolor: size=8:8:8:8, shift=24:16:8:0
[ 1.230393] Console: switching to colour frame buffer device 170x48
[ 1.232090] fb0: EFI VGA frame buffer device
[ 1.232110] intel_idle: MWAIT substates: 0x11142120
--
[ 3.595838] checking generic (e0000000 408000) vs hw (e0000000 10000000)
[ 3.595839] fb: switching to inteldrmfb from EFI VGA
[ 3.596577] Console: switching to colour dummy device 80x25
[ 3.596681] [drm] Replacing VGA console driver
[ 3.597159] [drm] ACPI BIOS requests an excessive sleep of 20000 ms, using 1500 ms instead
[ 3.599830] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013).
--
[ 3.657050] e1000e 0000:00:19.0 eth0: MAC: 11, PHY: 12, PBA No: FFFFFF-0FF
[ 3.657869] e1000e 0000:00:19.0 eno1: renamed from eth0
[ 4.711453] Console: switching to colour frame buffer device 170x48
[ 4.734356] i915 0000:00:02.0: fb0: inteldrmfb frame buffer device
[ 4.778813] Loading iSCSI transport class v2.0-870.


2. "What is /dev/console used for?"



You can use /dev/console as a TTY device. Writing to it, for example, will write to a specific underlying device, which will also have a character device number of its own.



Often /dev/console is tied to /dev/tty0, but sometimes it may be tied to a different device.



So in this case writing to /dev/console will write to /dev/tty0. And in turn, writing to /dev/tty0 is equivalent to writing to whichever /dev/ttyN device is currently active.



But this raises an interesting question. Accessing tty0 will access different virtual consoles, depending on which is currently active. What do people actually use tty0 for, and similarly what is console used for on Linux?




  1. Technically, you can read and write from console / tty0, for example running a getty to allow logging in on tty0. But this is only useful as a quick hack. Because it means you cannot take advantage of Linux's multiple virtual consoles.


  2. systemd looks in sysfs for an attribute associated with the /dev/console device, to detect the underlying TTY device. This allows systemd to automatically spawn a getty and allow logging in on e.g. a serial console, when the user set up a kernel console by booting with console=ttyS0. This is convenient; it avoids the need to configure this console in two different places. Again, see man systemd-getty-generator. However, systemd does not actually open /dev/console for this.


  3. During system bootstrap, you might not even have sysfs mounted yet. But you want to be able to show error and progress messages as soon as possible! So we circle around to point 1). The kernel starts PID 1 with stdin/stdout/stderr connected to /dev/console. It's very nice to have this simple mechanism set up right from the start.



  4. Inside a Linux container, the file at /dev/console may be created as something different - not the character device number 5:1. Instead, it may be created as a PTS device file. Then it would make sense to log in through this /dev/console file. systemd inside a container will allow logging in on such a device; see man systemd-getty-generator.



    This mechanism is used when you run a container with the systemd-nspawn command. (I think only when you run systemd-nspawn on a TTY, although I can't tell from searching the man page).



    systemd-nspawn creates the container's /dev/console as a bind mount of a PTS device from the host. This means that this PTS device is not visible inside /dev/pts/ inside the container.



    PTS devices are local to a specific devpts mount. PTS devices are an exception to the normal rule, that devices are identified by their device number. PTS devices are identified by the combination of their device number, and their devpts mount.




  5. You can write urgent messages to console / tty0, to write to the user's current virtual console. This could be useful for urgent userspace error messages, similar to urgent kernel messages which are printed to the console (see man dmesg). However it is not common to do this, at least once the system has finished booting.



    rsyslog has one example on this page, which prints kernel messages to /dev/console; this is pointless on Linux because the kernel will already do so by default. One example which I cannot find again says that it's not a good idea to use this for non-kernel messages because there are just too many syslog messages, you flood your console and it gets in the way too much.



    systemd-journald similarly has options to forward all logs to the console. In principle this might be useful for debugging in a virtual environment. Although, for debugging we usually forward to /dev/kmsg instead. This saves them in the kernel log buffer so you can read them with dmesg. Like messages generated by the kernel itself, these messages may be echo'ed to the console depending on the current kernel configuration.








share|improve this answer














"What is /dev/console?" is answered in the previous answer. Perhaps that answer is more clear when you know the answers to the other two questions.



Q1. "What is the device file representing the physical terminal itself?"



There is no such device file.



Q2. "What is /dev/console used for?"



On Linux, /dev/console is used to show messages during startup (and shutdown). It is also used for "single user mode", as pointed out in Stephen Kitt's answer. There is not much else it makes sense to use it for.



"In the good old days" of Unix, /dev/console was a dedicated physical device. But this is not the case in Linux.



Related evidence



1. "What is the device file representing the physical terminal itself?"




Let me try to understand this way. /dev/tty{1..63} and /dev/pts/n are device files representing devices themselves (although they are emulations), not in relation to process or kernel. /dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?). /dev/tty represents the controlling terminal currently used by a process session. /dev/console represents the terminal currently used by the kernel?



What is the device file representing the physical terminal itself, not in relation to kernel or process?




The underlying device(s) for /dev/tty{1..63} are struct con_driver. To see all the possible drivers, check out https://elixir.bootlin.com/linux/v4.19/ident/do_take_over_console



There is no device file for these underlying device(s)!





There is only a minimal userspace interface to manage them.



$ head /sys/class/vtconsole/*/name
==> /sys/class/vtconsole/vtcon0/name <==
(S) dummy device

==> /sys/class/vtconsole/vtcon1/name <==
(M) frame buffer device


If you really want to know more, the (M) stands for module. I.e. the dummy console device is not provided by a loadable kernel module; it is part of the initial kernel image (aka "builtin").



Secondly, the bind file in each subdirectory of /sys/class/vtconsole appears to tell you which vtconsole device is active. If I write 0 to the active one, it appears to switch to the dummy one. (GUI VTs seem unaffected, but text VTs stop working). Writing 1 for the dummy one does not activate it. Either method works to switch back to the real one. If I read the code correctly, the trick is that echo 1 > bind is only supposed to work for console drivers which are built as a module (?!).



For framebuffer consoles specifically, there is some more information about binding different framebuffer devices (/dev/fb0...) to specific virtual consoles in https://kernel.org/doc/Documentation/fb/fbcon.txt . This involves a kernel option fbcon:map= or a command called con2fbmap.



Of course the details can vary with different kernel versions, architectures, firmwares, devices, drivers, etc. I've never really had to use any of the interfaces above. The kernel just lets i915 / inteldrmfb / whatever you want to call it take over when it loads, replacing e.g. vgacon.



It looks like my EFI machine never has vgacon. So firstly it uses a dummy console, and secondly after 1.2 seconds it switches to fbcon, running on top of efifb. But so far I haven't had to care what the details are; it just works.



$ dmesg | grep -C2 [Cc]onsole
[ 0.230822] rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=4
[ 0.233164] NR_IRQS: 65792, nr_irqs: 728, preallocated irqs: 16
[ 0.233346] Console: colour dummy device 80x25
[ 0.233571] console [tty0] enabled
[ 0.233585] ACPI: Core revision 20180810
[ 0.233838] clocksource: hpet: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 133484882848 ns
--
[ 1.228393] efifb: scrolling: redraw
[ 1.228396] efifb: Truecolor: size=8:8:8:8, shift=24:16:8:0
[ 1.230393] Console: switching to colour frame buffer device 170x48
[ 1.232090] fb0: EFI VGA frame buffer device
[ 1.232110] intel_idle: MWAIT substates: 0x11142120
--
[ 3.595838] checking generic (e0000000 408000) vs hw (e0000000 10000000)
[ 3.595839] fb: switching to inteldrmfb from EFI VGA
[ 3.596577] Console: switching to colour dummy device 80x25
[ 3.596681] [drm] Replacing VGA console driver
[ 3.597159] [drm] ACPI BIOS requests an excessive sleep of 20000 ms, using 1500 ms instead
[ 3.599830] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013).
--
[ 3.657050] e1000e 0000:00:19.0 eth0: MAC: 11, PHY: 12, PBA No: FFFFFF-0FF
[ 3.657869] e1000e 0000:00:19.0 eno1: renamed from eth0
[ 4.711453] Console: switching to colour frame buffer device 170x48
[ 4.734356] i915 0000:00:02.0: fb0: inteldrmfb frame buffer device
[ 4.778813] Loading iSCSI transport class v2.0-870.


2. "What is /dev/console used for?"



You can use /dev/console as a TTY device. Writing to it, for example, will write to a specific underlying device, which will also have a character device number of its own.



Often /dev/console is tied to /dev/tty0, but sometimes it may be tied to a different device.



So in this case writing to /dev/console will write to /dev/tty0. And in turn, writing to /dev/tty0 is equivalent to writing to whichever /dev/ttyN device is currently active.



But this raises an interesting question. Accessing tty0 will access different virtual consoles, depending on which is currently active. What do people actually use tty0 for, and similarly what is console used for on Linux?




  1. Technically, you can read and write from console / tty0, for example running a getty to allow logging in on tty0. But this is only useful as a quick hack. Because it means you cannot take advantage of Linux's multiple virtual consoles.


  2. systemd looks in sysfs for an attribute associated with the /dev/console device, to detect the underlying TTY device. This allows systemd to automatically spawn a getty and allow logging in on e.g. a serial console, when the user set up a kernel console by booting with console=ttyS0. This is convenient; it avoids the need to configure this console in two different places. Again, see man systemd-getty-generator. However, systemd does not actually open /dev/console for this.


  3. During system bootstrap, you might not even have sysfs mounted yet. But you want to be able to show error and progress messages as soon as possible! So we circle around to point 1). The kernel starts PID 1 with stdin/stdout/stderr connected to /dev/console. It's very nice to have this simple mechanism set up right from the start.



  4. Inside a Linux container, the file at /dev/console may be created as something different - not the character device number 5:1. Instead, it may be created as a PTS device file. Then it would make sense to log in through this /dev/console file. systemd inside a container will allow logging in on such a device; see man systemd-getty-generator.



    This mechanism is used when you run a container with the systemd-nspawn command. (I think only when you run systemd-nspawn on a TTY, although I can't tell from searching the man page).



    systemd-nspawn creates the container's /dev/console as a bind mount of a PTS device from the host. This means that this PTS device is not visible inside /dev/pts/ inside the container.



    PTS devices are local to a specific devpts mount. PTS devices are an exception to the normal rule, that devices are identified by their device number. PTS devices are identified by the combination of their device number, and their devpts mount.




  5. You can write urgent messages to console / tty0, to write to the user's current virtual console. This could be useful for urgent userspace error messages, similar to urgent kernel messages which are printed to the console (see man dmesg). However it is not common to do this, at least once the system has finished booting.



    rsyslog has one example on this page, which prints kernel messages to /dev/console; this is pointless on Linux because the kernel will already do so by default. One example which I cannot find again says that it's not a good idea to use this for non-kernel messages because there are just too many syslog messages, you flood your console and it gets in the way too much.



    systemd-journald similarly has options to forward all logs to the console. In principle this might be useful for debugging in a virtual environment. Although, for debugging we usually forward to /dev/kmsg instead. This saves them in the kernel log buffer so you can read them with dmesg. Like messages generated by the kernel itself, these messages may be echo'ed to the console depending on the current kernel configuration.









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 30 at 23:22

























answered Nov 30 at 16:26









sourcejedi

22.5k43499




22.5k43499












  • As a complement to point 2, it used to be common (not just on Linux) to run xconsole to keep track of what was going on ;-). (And to avoid having messages written directly to the framebuffer, e.g. on SPARCstations.)
    – Stephen Kitt
    Nov 30 at 16:39










  • @StephenKitt when I first used SUSE Linux, I think it started xconsole, or ste it up so you could start it from the desktop main menu, or something - I don't remember exactly but I have seen it :-).
    – sourcejedi
    Nov 30 at 16:55












  • Let me try to understand this way. /dev/tty{1..63} and /dev/pts/n are device files representing devices themselves (although they are emulations), not in relation to process or kernel. /dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?). /dev/tty represents the controlling terminal currently used by a process session. /dev/console represents the terminal currently used by the kernel? What is the device file representing the physical terminal itself, not in relation to kernel or process?
    – Tim
    Nov 30 at 20:07












  • @Tim "/dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?)" - kernel.
    – sourcejedi
    Nov 30 at 20:22










  • What is the device file representing the physical terminal itself, not in relation to kernel or process?
    – Tim
    Nov 30 at 20:24


















  • As a complement to point 2, it used to be common (not just on Linux) to run xconsole to keep track of what was going on ;-). (And to avoid having messages written directly to the framebuffer, e.g. on SPARCstations.)
    – Stephen Kitt
    Nov 30 at 16:39










  • @StephenKitt when I first used SUSE Linux, I think it started xconsole, or ste it up so you could start it from the desktop main menu, or something - I don't remember exactly but I have seen it :-).
    – sourcejedi
    Nov 30 at 16:55












  • Let me try to understand this way. /dev/tty{1..63} and /dev/pts/n are device files representing devices themselves (although they are emulations), not in relation to process or kernel. /dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?). /dev/tty represents the controlling terminal currently used by a process session. /dev/console represents the terminal currently used by the kernel? What is the device file representing the physical terminal itself, not in relation to kernel or process?
    – Tim
    Nov 30 at 20:07












  • @Tim "/dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?)" - kernel.
    – sourcejedi
    Nov 30 at 20:22










  • What is the device file representing the physical terminal itself, not in relation to kernel or process?
    – Tim
    Nov 30 at 20:24
















As a complement to point 2, it used to be common (not just on Linux) to run xconsole to keep track of what was going on ;-). (And to avoid having messages written directly to the framebuffer, e.g. on SPARCstations.)
– Stephen Kitt
Nov 30 at 16:39




As a complement to point 2, it used to be common (not just on Linux) to run xconsole to keep track of what was going on ;-). (And to avoid having messages written directly to the framebuffer, e.g. on SPARCstations.)
– Stephen Kitt
Nov 30 at 16:39












@StephenKitt when I first used SUSE Linux, I think it started xconsole, or ste it up so you could start it from the desktop main menu, or something - I don't remember exactly but I have seen it :-).
– sourcejedi
Nov 30 at 16:55






@StephenKitt when I first used SUSE Linux, I think it started xconsole, or ste it up so you could start it from the desktop main menu, or something - I don't remember exactly but I have seen it :-).
– sourcejedi
Nov 30 at 16:55














Let me try to understand this way. /dev/tty{1..63} and /dev/pts/n are device files representing devices themselves (although they are emulations), not in relation to process or kernel. /dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?). /dev/tty represents the controlling terminal currently used by a process session. /dev/console represents the terminal currently used by the kernel? What is the device file representing the physical terminal itself, not in relation to kernel or process?
– Tim
Nov 30 at 20:07






Let me try to understand this way. /dev/tty{1..63} and /dev/pts/n are device files representing devices themselves (although they are emulations), not in relation to process or kernel. /dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?). /dev/tty represents the controlling terminal currently used by a process session. /dev/console represents the terminal currently used by the kernel? What is the device file representing the physical terminal itself, not in relation to kernel or process?
– Tim
Nov 30 at 20:07














@Tim "/dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?)" - kernel.
– sourcejedi
Nov 30 at 20:22




@Tim "/dev/tty0 reprsents the one in /dev/tty{1..63} which is currently used by something (maybe kernel or shell process?)" - kernel.
– sourcejedi
Nov 30 at 20:22












What is the device file representing the physical terminal itself, not in relation to kernel or process?
– Tim
Nov 30 at 20:24




What is the device file representing the physical terminal itself, not in relation to kernel or process?
– Tim
Nov 30 at 20:24


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485156%2fwhat-is-dev-console-used-for%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei