Ubuntu – `/dev/console` used for

consolelinuxptsttyUbuntu

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.

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